Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C Programming[Solved]

C Programming[Solved]

Scheduled Pinned Locked Moved C / C++ / MFC
help
6 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Evolt_Pratom
    wrote on last edited by
    #1

    #include #include
    #include
    struct students {
    char name[20];
    int age;
    };

    void main(){

    struct students firstyear\_bex\[5\];
    for(int i=0;i<5;i++){
    	puts("Enter your name:");
    	gets(firstyear\_bex\[i\].name);
    	puts("\\n");
    	puts("Enter your age:");
    	scanf("%d",&firstyear\_bex\[i\].age);
    	
    }
    puts("\\n");
    for(int i=0;i<5;i++){
    	puts(firstyear\_bex\[i\].name);
    	puts("\\t");
    	printf("%d",firstyear\_bex\[i\].age);
    	puts("\\n");
    }
    

    }

    i am a real begineer and i wanted to ask for the name and the details and display it like it is. but when i run it, the outputs are a bit funny, i dont get the proper order of printouts and when i enter something more it does nothing, but if i keep on entering it will take it but show all of it at the end with not the proper order. please help. . i use mingw gcc compiler

    L 1 Reply Last reply
    0
    • E Evolt_Pratom

      #include #include
      #include
      struct students {
      char name[20];
      int age;
      };

      void main(){

      struct students firstyear\_bex\[5\];
      for(int i=0;i<5;i++){
      	puts("Enter your name:");
      	gets(firstyear\_bex\[i\].name);
      	puts("\\n");
      	puts("Enter your age:");
      	scanf("%d",&firstyear\_bex\[i\].age);
      	
      }
      puts("\\n");
      for(int i=0;i<5;i++){
      	puts(firstyear\_bex\[i\].name);
      	puts("\\t");
      	printf("%d",firstyear\_bex\[i\].age);
      	puts("\\n");
      }
      

      }

      i am a real begineer and i wanted to ask for the name and the details and display it like it is. but when i run it, the outputs are a bit funny, i dont get the proper order of printouts and when i enter something more it does nothing, but if i keep on entering it will take it but show all of it at the end with not the proper order. please help. . i use mingw gcc compiler

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Evolt_Pratom wrote:

      the outputs are a bit funny

      What does that actually mean? Please explain exactly what inputs you enter and what results you see; we cannot guess. OK, I see the problem. You are using scanf to read the age, which leaves a carriage return character in the input buffer. So your next call to gets will read that as an empty string and continue on to request the age. Add a call to gets(xxx) after the scanf call, where xxx is a dummy buffer, e.g. char xxx[5];.

      K 1 Reply Last reply
      0
      • L Lost User

        Evolt_Pratom wrote:

        the outputs are a bit funny

        What does that actually mean? Please explain exactly what inputs you enter and what results you see; we cannot guess. OK, I see the problem. You are using scanf to read the age, which leaves a carriage return character in the input buffer. So your next call to gets will read that as an empty string and continue on to request the age. Add a call to gets(xxx) after the scanf call, where xxx is a dummy buffer, e.g. char xxx[5];.

        K Offline
        K Offline
        k5054
        wrote on last edited by
        #3

        Richard MacCutchan wrote:

        Add a call to gets(xxx) after the scanf call, where xxx is a dummy buffer, e.g. char xxx[5];.

        I think adding a carriage return to the end of the scanf format string will also work, will it not?

        L E 2 Replies Last reply
        0
        • K k5054

          Richard MacCutchan wrote:

          Add a call to gets(xxx) after the scanf call, where xxx is a dummy buffer, e.g. char xxx[5];.

          I think adding a carriage return to the end of the scanf format string will also work, will it not?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I tried that myself and it did not. It's about time scanf was consigned to the trash where it belongs.

          1 Reply Last reply
          0
          • K k5054

            Richard MacCutchan wrote:

            Add a call to gets(xxx) after the scanf call, where xxx is a dummy buffer, e.g. char xxx[5];.

            I think adding a carriage return to the end of the scanf format string will also work, will it not?

            E Offline
            E Offline
            Evolt_Pratom
            wrote on last edited by
            #5

            i dont know what happened but i used scanf instead of gets and it worked. Love to know how it happened tho.

            L 1 Reply Last reply
            0
            • E Evolt_Pratom

              i dont know what happened but i used scanf instead of gets and it worked. Love to know how it happened tho.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              I explained how in my earlier message.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups