Very new only couple days

Hello,
I am brand new to programming. Most of the stuff I have figured out is through trial and error or research on internet, unfortunately I don't even now how to word this one. This is a question from a class that I thought I would try. When I run it it works until I have to enter more than one word. Example When asked what city were you born? If I answer Yuba goes onto next question just fine. If I answer Yuba City this is where problem occurs. I asks the next two questions. I don't need someone to give me answer but maybe point me in right direction on where to look. Thanks in advance for any tips.

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>
using namespace std;

int main()
{ 
	string	
		_Name,
		_City,
		_College,
		_Profession,
		_FavoriteAnimal,
		_NameOfPet;
	int _Age;

	cout << "What is your name? " << endl;
	cin >> _Name;

	cout << "How old are you? " << endl;
	cin >> _Age;
	
	cout << "What city were you born in? No need to type the word CITY " << endl; //When I enter a city with a single word code works great but if more then one word and I hit enter it asks two questions same time
	cin >> _City;

	cout << "Name of the College you attended? " << endl;
	cin >> _College;

	cout << "What do you do for a living? " << endl;
	cin >> _Profession;

	cout << "What is your favorite animal? " << endl;
	cin >> _FavoriteAnimal;

	cout << "What is the name of your pet? " << endl;
	cin >> _NameOfPet;

	cout << "There once was a person named " << _Name << " who lived in " << _City << ". At the age of" << endl;
	cout << _Age << ", " << _Name << " went to college at " << _College << ". " << _Name << " graduated and went to work" << endl;
	cout << "as a " << _Profession << ". Then, " << _Name << " adopted a " << _FavoriteAnimal << " named " << _NameOfPet << ". They " << endl;
	cout << "both lived happily ever after!";

		return 0;
}
Hello briancb2004,

The problem is because you may not understand the difference between formatted and unformatted input.

Formatted input like cin >> _City; will take what you type and store it in the input buffer until you press "Enter". Then it will extract from the input buffer until it finds a white space or new line (\n) whichever comes first. So when you enter "Yuba City" only "Yuba" is stored in "city" because it stops at the first space leaving "City" in the input buffer for the next read. On the next read it will not wait for keyboard input, but put "city" in the variable "_College" thus throwing everything off.

You could change line 22 to std::getline(std::cin, city); and this will keep any spaces in the string and extract the "\n" which it discards. Leaving the input buffer empty and storing everything that you have typed.

I would also suggest not starting your variable names with an underscore. This may be legal, but it is best left to the header files so there is no conflict between your variables and the header files. Also it is most common to start a regular variable with a lower case letter. You can use "camelCase", as you have, or use the underscore to separate words.

When mixing formatted and unformatted input you will need to follow the last "cin >>" with
 
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>. 

before a "getline" is used. Otherwise the "getline" will get the "\n" left in the input buffer.

Andy
Well the extraction operator>> stops processing entries when it encounters a whitespace character. If you want your strings to have whitespace you should investigate getline(). But be careful when mixing getline() with the extraction operator>> since the extraction operator>> leaves the new line character in the input buffer which will "confuse" getline() into thinking that character is the only entry into the string.

By the way I recommend you stop using leading underscores for your variable names since in many cases variables that start with a leading underscore are reserved for the implementation.
Thank you for the quick response. Looks liking I have some studying to do. Thank you all for pointing me in the right direction!!!

So reworked it and almost working but now it is getting rid of first word. So Yuba City is showing up as just city.

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>
#include <string>
using namespace std;

int main()
{
	string
		Name,
		City,
		College,
		Profession,
		FavoriteAnimal,
		NameOfPet;
	int Age;

	cout << "What is your name? " << endl;
	cin >> Name;

	cout << "How old are you? " << endl;
	cin >> Age;

	cout << "What city were you born in? " << endl; 
	cin >> City;
	std::getline(std::cin, City);

	cout << "Name of the College you attended? " << endl;
	cin >> College;
	std::getline(std::cin, College);

	cout << "What do you do for a living? " << endl;
	cin >> Profession;
	std::getline(std::cin, Profession);

	cout << "What is your favorite animal? " << endl;
	cin >> FavoriteAnimal;

	cout << "What is the name of your pet? " << endl;
	cin >> NameOfPet;
	cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

	cout << "There once was a person named " << Name << " who lived in " << City << ". At the age of" << endl;
	cout << Age << ", " << Name << " went to college at " << College << ". " << Name << " graduated and went to work" << endl;
	cout << "as a " << Profession << ". Then, " << Name << " adopted a " << FavoriteAnimal << " named " << NameOfPet << ". They " << endl;
	cout << "both lived happily ever after!";

	return 0;


What is your name?
Bob
How old are you?
35
What city were you born in?
Yuba City
Name of the College you attended?
United States of America
What do you do for a living?
Long board maker
What is your favorite animal?
Dog
What is the name of your pet?
Daisy
There once was a person named Bob who lived in  City. At the age of
35, Bob went to college at  States of America. Bob graduated and went to work
as a  board maker. Then, Bob adopted a Dog named Daisy. They
both lived happily ever after!
Last edited on
Hello briancb2004,

Part of your code:
1
2
3
4
5
6
7
8
9
10
11
cout << "What city were you born in? " << endl;
cin >> City;  // <--- Trying to enter as formatted input.
std::getline(std::cin, City);  // <--- trying to enter as unformatted input. One or the other, but not both.

cout << "Name of the College you attended? " << endl;
cin >> College;
std::getline(std::cin, College);

cout << "What do you do for a living? " << endl;
cin >> Profession;
std::getline(std::cin, Profession);

Line 2 will store in "city" up to the first white space and leave the rest in the input buffer.

Line 3 does not wait for any keyboard input, but will store what is left in the input buffer and discard the "\n" when it is found.

Give this a try:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
cout << "What is your name? " << endl;
std::getlinecin(std::cin, name);

cout << "How old are you? " << endl;
cin >> Age;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

cout << "What city were you born in? " << endl;
std::getline(std::cin, City);

cout << "Name of the College you attended? " << endl;
std::getline(std::cin, College);

cout << "What do you do for a living? " << endl;
std::getline(std::cin, Profession);

cout << "What is your favorite animal? " << endl;
std::getlinecin(std::cin, FavoriteAnimal);

cout << "What is the name of your pet? " << endl;
std::getlinecin(std::cin, NameOfPet;

This way other than "age" the "getline"s will store whatever you type into the given variables.

The reason that you can not use "getline" for "age" is that "getline" only works with strings.
http://www.cplusplus.com/reference/string/string/getline/

Andy
Thanks Andy I am getting some errors with the std:: getlinecin. But I am trying to work through it will let you know what I figure out.
briancb2004

Mixing stream extraction (>>) and getline is fraught with difficulties.

The former stops at the first white space and leaves the rest of the line in the stream for the next input to pick up.

The latter takes the whole line (by default - though it could be made to stop at anything). Importantly it doesn't leave the newline character in the stream.

It is probably simpler here to input everything using getline, then convert the only numerical value using stoi().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
using namespace std;

int main()
{ 
   string Name, City, College, Profession, FavoriteAnimal, NameOfPet;
   string temp;
   int Age;
   cout << "What is your name? "               ;   getline( cin, Name );
   cout << "How old are you? "                 ;   getline( cin, temp );   Age = stoi( temp );
   cout << "What city were you born in? "      ;   getline( cin, City );
   cout << "Name of the College you attended? ";   getline( cin, College );
   cout << "What do you do for a living? "     ;   getline( cin, Profession );
   cout << "What is your favorite animal? "    ;   getline( cin, FavoriteAnimal );
   cout << "What is the name of your pet? "    ;   getline( cin, NameOfPet );

   cout << "There once was a person named " << Name << " who lived in " << City << ". At the age of\n";
   cout << Age << ", " << Name << " went to college at " << College << ". " << Name << " graduated and went to work\n";
   cout << "as a " << Profession << ". Then, " << Name << " adopted a " << FavoriteAnimal << " named " << NameOfPet << ". They\n";
   cout << "both lived happily ever after!";
}
Last edited on
Topic archived. No new replies allowed.