data structure problem

Here is my basic code to understand the usage of data structure.

The problem is when I run it, on the code line 32 it prints
cout<<"fulname : "; and cout<<"sex: ";
without waiting for getline(cin,andy.fulname);


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
 
#include <iostream>
#include <sstream>
#include <string>

using namespace std;


struct  personalID{
			
	string fulname;
	string sex;
	short int age;

}kate,joa,andy;


int main()
{
	
	kate.fulname="kate katerina";
	kate.sex="f";
	kate.age=24;

	cout<<"fulname : ";
	getline(cin,joa.fulname);
	cout<<"sex: ";
	getline(cin,joa.sex);
	cout<<"age: ";
	cin>>joa.age;
		
	cout<<"fulname : ";
	getline(cin,andy.fulname);
	cout<<"sex: ";
	getline(cin,andy.sex);
	cout<<"age: ";
	cin>>andy.age;
	
		
	return 0;

}


the output is just like

fulname; joa can
sex;female 
age;34
fulname; sex; 
The problem is that when you read joa's age

cin>>joa.age;

the new line character after you pressed ENTER is present in buffer. So the next read with getline reads empty string.
You should remove or read the new line character before getline will be executed.
This is not an unusual problem. Can't we just create some functions that handles new lines "correctly" that the beginners can use? Here is my try.

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
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <sstream>
#include <string>
#include <limits>

template <typename T>
std::istream& readline(std::istream& is, T& elem)
{
	is >> elem;
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	return is;
}

template <>
std::istream& readline(std::istream& is, std::string& str)
{
	while (std::getline(is, str) && str.empty());
	
	return is;
}

using namespace std;

struct  personalID{
			
	string fulname;
	string sex;
	short int age;

}kate,joa,andy;


int main()
{
	kate.fulname="kate katerina";
	kate.sex="f";
	kate.age=24;

	cout<<"fulname : ";
	readline(cin,joa.fulname);
	cout<<"sex: ";
	readline(cin,joa.sex);
	cout<<"age: ";
	readline(cin, joa.age);
		
	cout<<"fulname : ";
	readline(cin,andy.fulname);
	cout<<"sex: ";
	readline(cin,andy.sex);
	cout<<"age: ";
	readline(cin, andy.age);

	return 0;
}

doesn't this work?

1
2
3
4
5
//etc
readline(cin, joa.age);
cin.clear();
cin.ignore();
//etc 
I appreciate for all your interest. I tried each of your suggestions but I couldn't work it because I'm unexperienced.
Finally I found smt in the tutorial and it works using stringstream.But I don't know the reason why it is work like this.Any idea?

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
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

struct  personalID{
			
	string fulname;
	string sex;
	short int age;

}kate,joa,andy;
	
void print(personalID who);
	
int main()
{
	string strage;

	kate.fulname="kate katerina";
	kate.sex="f";
	kate.age=24;

	cout<<"fulname : ";
	getline(cin,joa.fulname);
	cout<<"sex: ";
	getline(cin,joa.sex);
	cout<<"age: ";
	getline(cin,strage);
	stringstream(strage)>>joa.age;
	
	cout<<"fulname : ";
	getline(cin,andy.fulname);
	cout<<"sex: ";
	getline(cin,andy.sex);
	cout<<"age: ";
	getline(cin,strage);
	stringstream(strage)>>andy.age;	

	print(kate);
	print(joa);
	print(andy);
		
	int a;
	cin>>a;
	return 0;

}

void print(personalID who)
{
	cout<<who.fulname<<endl;
	cout<<who.sex<<endl;
	cout<<"("<<who.age<<")"<<endl;
}
This is not an unusual problem. Can't we just create some functions that handles new lines "correctly" that the beginners can use? Here is my try.


Hijacking a thread for that is probably not a good idea. (And it won't work in the general case.)


@langdon:

A stringstream (you should probably use an istringstream here) acts like an input or output stream. The content of the istringstream is the string you initialize it with. The reason you don't experience the same problems with the stray newline is because you're using line oriented input via getline which doesn't leave the trailing newline in the input stream.
Last edited on
Topic archived. No new replies allowed.