a weird behavior of gets()

when I execute it skeeps the name extraction like this way

Enter the information of student nbr
First name : Age :
if you notice I can not enter the name
thanks for reading

int main()
{
studentInfo *sInfo;
int sNbr;
cout << "Enter your the number of the students -> ";cin >> sNbr;
system("cls");

sInfo = new studentInfo[sNbr];

for(int i=0;i<sNbr;i++,sInfo++)
{
cout << "Enter the information of student nbr " << i+1 << endl;
cout << "First name : " ;gets(sInfo->name);
cout << "Age : " ;cin >> sInfo->age;
cout << "Gender : " ;cin >> sInfo->gender;cout << endl;
}sInfo-=sNbr;
for(int i=0;i<sNbr;i++,sInfo++)
{
cout << "Information of student nbr " << i+1 << endl;
cout << "First name is : " << sInfo->name << endl;
cout << "Age is : " << sInfo->age << endl;
cout << "Gender is : " << sInfo->gender << endl << endl;
}
delete sInfo;
return 0;
}
I forgot the head of the program
thans again

# include <iostream>
# include <cstdio>
# include <cstdlib>
using namespace std;

struct studentInfo
{
char name[20];
int age;
char gender;
};
No solutions ?!
gets will read from stdin until it finds a newline. You are leaving a newline there from previous operations, so it reads that in straightaway without you having to put any new data into the input buffer.

If you compile your code using g++, you even get warned about how bad gets is:
warning: the `gets' function is dangerous and should not be used.


Bad solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# include <iostream>
# include <cstdio>
# include <cstdlib>
using namespace std;

struct studentInfo
{
char name[20];
int age;
char gender;
};


int main()
{
studentInfo *sInfo;
int sNbr;
cout << "Enter your the number of the students -> ";cin >> sNbr;


sInfo = new studentInfo[sNbr];

for(int i=0;i<sNbr;i++,sInfo++)
{
cout << "Enter the information of student nbr " << i+1 << endl;
cout << "First name : " ;

 int ch;
while ((ch = getchar()) != '\n' && ch != EOF);

gets(sInfo->name);
cout << "Age : " ;cin >> sInfo->age;
cout << "Gender : " ;cin >> sInfo->gender;cout << endl;
}sInfo-=sNbr;
for(int i=0;i<sNbr;i++,sInfo++)
{
cout << "Information of student nbr " << i+1 << endl;
cout << "First name is : " << sInfo->name << endl;
cout << "Age is : " << sInfo->age << endl;
cout << "Gender is : " << sInfo->gender << endl << endl;
}
delete sInfo;
return 0;
}


Good solution: DON'T USE gets
Last edited on
Thank you so much reading

but can I ask what those lines do

int ch;
while ((ch = getchar()) != '\n' && ch != EOF);
int ch; Create a new int variable, named ch.

while ((ch = getchar()) != '\n' && ch != EOF);
Read in using the getchar function and store the input into the variable ch nutil it's the end of a line or the end of a file.

Don't use gets.
sorry to ask but i'd like to ask you Moschops that why did u use EOF when there is no file issue in the program just !='\n' is sufficient.
It's generic input buffer flushing code. Since it's a bad solution anyway, it didn't really seem important.
It's important anyway: a user could enter EOF from keyboard and get this program stuck in an endless loop (although abusing gets() to read OP's passwords would be a more profitable use of the input access)
┬┐where is that endless loop? EOF will break the while
@Moschops, please indent your code.
@ne555 I meant that in response to @ankit2313's "why did u use EOF [...] just !='\n' is sufficient" but yes, I was thinking C++ for a bit there. getchar()'ing a EOF'd stdin does not always return immediately (like cin.get()'ting a eof'd cin) : the outcome depends on the OS and probably terminal settings.
Without the EOF check, it loops forever on my Solaris, but reads the following characters on my AIX or Linux.
Last edited on
The program works perfectly well , thanks to you Moschops ,but I have couple of questions:

the first one is : I have a hard time to understand the relation between the while loop and gets() ,
I think the while loop dose not ends until It gets a caractere different than '\n' or EOF,then gets the name , am I right?
Topic archived. No new replies allowed.