Getting some errors with this code

closed account (D4NbpfjN)
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  #include<iostream>
using namespace 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.


Hi,

Using cpp.sh with all 3 warnings on it complied and worked fine for me. Although if you are going to use std::string, then #include <string>

I am getting some errors with my code.


Post those errors / warnings here verbatim. It's good to see the actual error, rather than a description of them.
#include <string> is missing.
closed account (D4NbpfjN)

When i run the program these two end up on the same line and it will not let me input the animal. What am i doing wrong?


1
2
3
4
5
6
cout << "Please enter a type of animal:";
cin >> useranimal;

		cout << "Please enter your pets name:";
		cin >> userpetname;
	
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:

getline(cin >> ws, usercity);

closed account (D4NbpfjN)
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
one is to use the ignore() function between the extraction operator and the getline()


Post your current code.
closed account (D4NbpfjN)
This is my current code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
#include<string>
using namespace 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;
}



Topic archived. No new replies allowed.