So i am having trouble with two of my lines. the one that has what type of animal and pets name. they keep printing on the same line. I need help figuring out how to get them to print on another line and it also want let me type what kind of animal.
#include<iostream>
#include<string>
usingnamespace std;
int main()
{
string username;
string userage;
string usercity;
string usercollege;
string userprofession;
string useranimal;
string userpetname;
cout << "Please enter your name:";
getline(cin, username);
cout << "Please enter your age:";
cin >> userage;
cout << "Please enter your city:";
cin >> usercity;
cout << "Please enter your college:";
cin >> usercollege;
cout << "Please enter your profession:";
cin >> userprofession;
cout << "Please enter a type of animal:";
cin >> useranimal;
cout << "Please enter your pets name:";
getline(cin >> ws, userpetname);
cout << "\n\nThere once was a person named " << username << " who lived in " << usercity << ".\n";
cout << "At the age of " << userage;
cout << ", " << username << " went to college at " << usercollege << ".\n ";
cout << username << " graduated and went to work as a " << userprofession << ".\n Then, ";
cout << username << " adopted a(n) " << useranimal << " named " << userpetname << ".\n";
cout << "They both lived happily ever after!";
return 0;
}
Why did you open a new topic when you already had an open topic for the same problem?
Why didn't you follow the advice given in that topic?
Now realize that the problem is when you are switching from the extraction operator and getline(). You need to consume the whitespace between the use of the extraction operator and the getline().
I a still not getting the correct thing. now it is printing city and college together on same line
The >> operator leaves a newline character in the stream. The getline after that is consuming it, and thus taking that as input. The way to fix this is to consume the newline character before switching to getline. You can do this by writing cin.ignore() after line 19.
#include<iostream>
#include<string>
#include <limits>
usingnamespace std;
int main()
{
string username;
string userage;
string usercity;
string usercollege;
string userprofession;
string useranimal;
string userpetname;
cout << "Please enter your name: ";
getline(cin, username);
cout << "Please enter your age: ";
cin >> userage;
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
cout << "Please enter your city: ";
cin >> usercity;
cout << "Please enter your college: ";
cin >> usercollege;
cout << "Please enter your profession: ";
cin >> userprofession;
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
cout << "Please enter a type of animal: ";
cin >> useranimal;
cout << "Please enter your pets name: ";
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
getline(cin >> ws, userpetname);
cout << "\n\nThere once was a person named " << username << " who lived in " << usercity << ".\n";
cout << "At the age of " << userage;
cout << ", " << username << " went to college at " << usercollege << ".\n ";
cout << username << " graduated and went to work as a " << userprofession << ".\n Then, ";
cout << username << " adopted a(n) " << useranimal << " named " << userpetname << ".\n";
cout << "They both lived happily ever after!";
return 0;
}
Please enter your name: Brian Brain
Please enter your age: 32
Please enter your city: Salem
Please enter your college: Witch
Please enter your profession: Dealer
Please enter a type of animal: dog
Please enter your pets name: Rover
There once was a person named Brian Brain who lived in Salem.
At the age of 32, Brian Brain went to college at Witch.
Brian Brain graduated and went to work as a Dealer.
Then, Brian Brain adopted a(n) dog named Rover.
They both lived happily ever after!
Exit code: 0 (normal program termination)