Hi all, I'm new to the forum and am working threw Mr. Allain's book to refresh/re-learn from my previous programming classes that I took a few years ago.
In this simple end-of-chapter problem, the ages of two people are to be compared and determine who's older. The problem I'm experiencing is that the second user name input is being skipped and going straight to the second users age.
So while running the program it looks something like:
What is user ones name?
<input name>
How old are they?
<input age>
What is user twos name?
How old are they?
<input age>
<if statement result>
Here's my code:
Code:
#include <iostream>
#include <string>
using namespace std;
int user_one_age;
int user_two_age;
string user_one_name;
string user_two_name;
int main()
{
///Get the user names and their ages
cout << "What is user ones name?" << endl;
getline( cin, user_one_name, '\n' );
cout << "How old are they?" << endl;
cin >> user_one_age;
cout << "\nWhat is user twos name?" << endl;
getline( cin, user_two_name, '\n');
cout << "How old are they?" << endl;
cin >> user_two_age;
if ( user_one_age > user_two_age )
{
cout << "\n\n" << user_one_name << " is older than " << user_two_name << endl;
}
else
{
cout << "\n\n" << user_two_name << " is older than " << user_one_name << endl;
}
}