need help with the code

closed account (D4NbpfjN)
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.
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
  #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;
}
Last edited on
Replace all cin's except for the one on line 19 with getline.

line 33: change getline(cin >> ws, userpetname) to getline(cin, userpetname)
closed account (D4NbpfjN)
I a still not getting the correct thing. now it is printing city and college together on same line
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.
Last edited on
or after line 33, e.g in line 34 with cin.ignore() as Arslan said.
closed account (48T7M4Gy)
This works but is potentially flaky because of the mixture of lines of data and single strings.

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
47
48
49
50
51
#include<iostream>
#include<string>
#include <limits>

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;

    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)
Topic archived. No new replies allowed.