while loop kicks me out after first pass

Hello everyone:

I am new to C++, I wrote this small program for a class assignment so I am looking for guidance. The program is supposed to take the info and ask the questions again while the sentinel !=Y. However, it is kicking me out after the first pass.

I can't figure out why.

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
 int main(){

	Person correction;
	
	bool more = true;
	while (more){

cout << "Please enter the employee name (Last Name, First Name): ";
	string last_name; //Variable holding the last name entry
	string first_name;//Variable holding the first name entry
	cin >>last_name >> first_name; //User entry

	string new_name = last_name + " " + first_name; //Concactenate the names
	correction.set_name(new_name); //Set the new name

cout<< "Please enter the employee's age):";

	int age; //Variable holding the employee's age
	cin >> age;

	int new_age = age;
	correction.set_age(new_age); //Set the new age

cout <<"You entered employee name: " << correction.get_name() <<"\n";
cout <<"The employee's age is: " << correction.get_age() << "\n";

	cout<<"Do you wish to enter another employee? (Y/N)";
		string response;
		getline(cin, response);

		 if (response !="Y")
				more = false;

	}

cout << "Thank you for entering the data" << endl;



system ("pause");
return 0;


}
cin >> age; Will left newline symbol in input buffer. Following getline will read everything in input buffer until it encounters neline symbol. As it is first thing it encounters, it will instantly ends.
To fix:
1
2
3
4
char response;
std::cin >> response;
if (response != 'Y')
/*...*/

Or just std::cin >> response; instead of getline()
Or clear input buffer before getting line...
Thank you. I know it was something simple. I learned something new.
Topic archived. No new replies allowed.