Cin not working, help please

When I ask the user to input their friends name, the output completely skips the option to enter any data.

Another issue is that when I select either m/f for male or female, my code doesn't implement the title= "Mr." or title = "Mrs." instead the output will just put m or f instead of Mr. or Mrs.
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 <iomanip>
#include <string>

using namespace std;

int main()
{
	string fullname;
	string friendname;
	char gender;
	string title;

	cout << "Please enter your fullname: " << endl;
	getline(cin, fullname);

	cout << fullname << " please enter your gender (m/f)?" << endl;
	cin >> gender;

	cout << gender << " " << fullname << " please enter your friend's name: " << endl;
	getline(cin, friendname);

	if (gender == 'm' || gender == 'M')
	{
		title = "Mr.";

	}
	else if (gender == 'f' || gender == 'F')
	{
		title = "Mrs.";
	}

	

	cout << "Hello Mr/Mrs " << friendname << ", " << gender << " " << fullname << " considered you as a friend!!" << endl;

	system("PAUSE");
	return 0;



}
The main reason you shouldn't mix and match operator >> with getline is that operator >> leaves a newline in the buffer and getline reads until the newline. Before line 21 you need to add an ignore statement to ignore the newline left in the buffer. Something like
1
2
3
4
5
6
7
std::cin.ignore();

//or better
std::cin.ignore(1024, '\n');

//or even better
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');



By the way shouldn't it be "Ms" and not "Mrs" or are assuming all the females are married?

Oh and the second problem is because you are outputting the gender and not title. The compiler doesn't know what you want it to do unless you tell it to do that.
Last edited on
after the input of a character in line 18, cin>>gender , the '\n' character is left in the buffer. It is this '\n' that is inputted in the getline .
you can fix this by using the standard function ignore() just after the input.

1
2
3
 
cin>>gender;
cin.ignore();  //ignore any data in the buffer. 


About, the displaying 'm' and not "Mr", this is coz you used gender in your string literal instead of title .
So the reason why I couldn't use getline right after another is because the info stored in the first getline doesn't get overwritten so you have to use cin.ignore to completely nullify the memory of the first getline?
No, when you use std::cin >> variable; it leaves a newline in the buffer (from hitting enter). So before reading the buffer looks like 'm''\n' then after it looks like '\n' and std::getline(std::cin, variable); extracts until the delimiter is found (in this case the delimiter will be a newline - '\n') and then ignores the delimiter so in other words it extracts nothing and discards the newline since it is left in the buffer. You must ignore it first.

http://www.cplusplus.com/forum/general/1477/
Topic archived. No new replies allowed.