I am getting some errors with my code. My errors consist of the getline statement in the username. Another error is this >> in userage. The other errors are in the sentences at the end of the program. Line 37 the error is << before username. Next error is in line 38 << before username.Last two errors are in lines 40 and 41 the << before username.
#include<iostream>
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.";
getline(cin, usercity);
cout << "Please enter your college.";
cin.ignore(100, '\n');
getline(cin, usercollege);
cout << "Please enter your profession.";
getline(cin, userprofession);
cout << "Please enter a type of animal.";
getline(cin, useranimal);
cout << "Please enter your pets name:";
getline(cin, 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;
}
The program seems to compile without errors or warnings so, what exactly are the errors?
Be careful when switching between the extraction operator>> and getline(), the extraction operator leaves the new line character in the input buffer for the next input operation, while getline() extracts and discards the end of line character.
Re-read my first post. When that end of line character is left in the input buffer getline() extracts that character as the only entry. You need to remove that character. There are a couple ways of accomplishing this removal, one is to use the ignore() function between the extraction operator and the getline(), like you're doing on line 24, the other is to force getline() to skip leading whitespace:
were do i need to place that everything is working except the type of animal and pets name. I put the getline statement yo provided under both and then only put it under the animal line and it is still printing on the same line
#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;
}