C Programming[Solved]
-
#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
-
#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
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 togets
will read that as an empty string and continue on to request the age. Add a call togets(xxx)
after thescanf
call, wherexxx
is a dummy buffer, e.g.char xxx[5];
. -
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 togets
will read that as an empty string and continue on to request the age. Add a call togets(xxx)
after thescanf
call, wherexxx
is a dummy buffer, e.g.char xxx[5];
. -
Richard MacCutchan wrote:
Add a call to
gets(xxx)
after thescanf
call, wherexxx
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?
-
Richard MacCutchan wrote:
Add a call to
gets(xxx)
after thescanf
call, wherexxx
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?
i dont know what happened but i used scanf instead of gets and it worked. Love to know how it happened tho.
-
i dont know what happened but i used scanf instead of gets and it worked. Love to know how it happened tho.