getline problem

Hello! The first time the while loop is ran, there are no problems. However, the second time the loop is ran, after having pressed "y", it skips the user's ability to enter the person's name. Any suggestions how to fix it?

If the question did not make sense, it will make sense once the loop is ran twice. You do not have to go far as running the program to help me out. But if you do, that would be so awesome. Thank you in advance!

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
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstring>
using namespace std; 

struct personalInfo
{
	string name; 
	string address;
	string phoneNumber; 
};

int main ()
{
	bool addPersonalInfo = true;
	string userInput;
	int i = 1;  

	while (addPersonalInfo)
	{
		personalInfo person [i];

		cout << "Person's Name: "; 
		getline (cin, person[i-1].name);
		cout << "Person's Address: ";
		getline (cin, person[i-1].address); 
		cout << "Person's phone number: "; 
		getline	(cin, person[i-1].phoneNumber);

		cout << "Would you like to add another person's information? y/n : "; 
		cin >> userInput; 
		if (userInput == "y")
		{
			i++;  
		}
		if (userInput == "n")
		{
			addPersonalInfo	= false; 
		}
	}

return 0; 
}
 
Last edited on


a newline will be left in the buffer.

use cin.ignore(1000, '\n');
Awesome. Thank you so much! :)

Your welcome :)
you are welcome*
or
you're welcome*

Oh hang on, thought this was a C++ website not an English or grammar lesson?
you were mistaken :b
Don't take it personally, I just don't understand how people get confused with "you are" and "your" so I correct them from time to time.
I was mistaken about what.. being a c++ website? - in your dreams sunshine! - If I wanted an English lesson I'd ask for it, until then keep it to yourself.
I was being sarcastic in my first sentence, that's why i put the smiley there.

Yeah sure, I didn't want to anger you, I'm sorry.
That being said, you could've said that a little bit more nicely.
Last edited on

Yeah I was a bit grouchy last night so apologies for that - someone rattled my cage at the pub (not that its an excuse as I should know better) and it appears you got the backlash of it - so apologies again for my unnecessary behavior.

it's okey, I though you had something else ruining your mood
Topic archived. No new replies allowed.