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.
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...