Hi guys, this is my first time posting and I recently purchased jumping into C++ and need help on the first practice problem of Chapter 4.
Q: Ask the user for two user's ages, and include who is older; behave differently if both are over 100.
So this is what I have coded:
Code:
#include <iostream>
#include <string>
usingnamespacestd;
int main ()
{
string name; // x
string name_2; // y
int x;
int y;
cout << "Hello User.. This program intend's to ask for the age and name of 2 people\n";
cout << "Please enter the first name:\n";
getline(cin, name, '\n');
cout << "Good.. now enter the age of " << name << ":\n";
cin >> x;
cout << "Next I need you to enter the name of person number 2:\n";
getline(cin, name_2, '\n'); // This line doesn't work when I run it.
cout << "Alright.. now finally please enter the age of " << name_2 << ":\n";
cin >> y;
if (x > y)
cout << name << " is older than " << name_2 << "\n";
else if (y > x)
cout << name_2 << " is older than " << name << "\n";
else if ((x && y) > 100)
cout << "Your both crazy old!!!\n";
else
return 0;
}
Below is what has ran:
[Program]
Hello User.. This program intend's to ask for the age and name of 2 people
Please enter the first name:
Jonathan
Good.. now enter the age of Jonathan:
21
Next I need you to enter the name of person number 2:
Alright.. now finally please enter the age of :
(Did it skip?)
Jonathan is older than
Program ended with exit code: 0
[/Program]
2 Problems come from this:
I can't get the second "getline" to show input for the user.
I can't get the last statement if/else statement to work.
Anyone know what I'm doing wrong? Also is their anything like a downloadable PDF on this site that shows the answer key to the problem sets of the book? I can learn so much faster if I can compare it myself so I don't always have to bother people about that much in forums.
Any help will be greately appreciated! Thanks! : )