Loop skips first cin command while repeating?

Hi! Using Visual Studio and trying to write this code but it's giving me some serious grief tonight. It was all good--save a few resolved issues--until I got to the point of repeat. When I did get there I chose yes, and then it skipped right over name, and went straight to city. Any suggestions?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
	//Begin Loop
	while (nwDvr != 1)
	{

		//Input: Personal Info
		cout << "Enter the diver's name:\t";
		getline(cin, name); //allows full name
...
		switch (answer)
		{
		case 'Y': nwDvr = 0;
		case  'y': nwDvr = 0;
			break;
		case 'n': nwDvr = 1;
		case 'N': nwDvr = 1;
			break;
		default:
		cout << "Error: Enter a Y for yes, or an N for no." << endl;
			break;
		}
	}
}
Last edited on
At the start of your while loop add a line with std::cin.sync(). This is because inputting answer with the stream extraction operator (>>) will stop once it reaches a whitespace character. This will leave a linefeed sitting in the buffer, so when it loops the std::getline function will just take that and your program will keep going.

EDIT:
The other option is to add a std::cin.ignore() after line 66, to ignore that extra newline, but it will still cause your program to break if they type more than simply 'y' or 'n' (for example, typing 'yes').
Last edited on
Ah, you lifesaver you. Thanks again.
Topic archived. No new replies allowed.