Program is skipping input

I am writing a program that allows the input and output of personal data and tax information (filing status, income, total taxes, etc)...

Anyway, I am just working on the menus at the moment, and am getting a stupid error I can't figure out. Here's the relevant portion of 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
		switch (category)
		{
		case DATAINPUT:
			// This function accepts the input of the user's data

			cout << "\n\nPlease input the following user data.\n"; 
			cout << "Name: ";
			cin >> name;
			cout << "Address: ";
			cin >> address;
			cout << "Filing Status\n1)Single\n2)Married Filing Jointly\n3)Married Filing Seperately\n4)Head of Household\n";
			cout << "Please choose the category number: ";
			cin >> fileStatus;
			// validate menu selection.
			while (fileStatus < 1 || fileStatus > 4)
			{ 
				cout << "Please enter a valid option: ";
				cin >> fileStatus;
			}
			cout << "Income For Tax Year: ";
			cin >> income;
			cout << "Total Expenses: ";
			cin >> expenses;
			cout << "\nYour data has been saved.\n\n";
			
		}


When I test it, it allows for input of Name, but then skips address and goes straight to the filing status information. Here's an image that shows the problem:

Link to pic: http://img.photobucket.com/albums/v260/nymets1335/address_zps46671c8e.jpg

Anyone have an idea on why this would be happening? I feel like I'm missing something obvious and stupid.

Also, I was originally considering writing a function to accept all of this information instead called dataInput(). We haven't gotten far on functions yet (and this is for a class I'm in), and I'm uncertain if it's possible to have one function save so many different pieces (and types) of data and be accessible to the main function (where other functions would then access this data from and generate the reports and such). Is that possible, or am I better off inputting all of the data in main(), then using the functions to perform the rest of the actions?

Thanks!
Last edited on
@mikeybags

Because you declared 'Name' a string, ad input as cin >> name, it ends the input of name at the first space. Then the variable, address, accepts the rest of the input. But because it's not a number, ( address is an int, correct? ), that part of the code is skipped, and you wind up at the Filing Status: section. Use getline (cin,name); as a string input, and the whole string will be accepted.
Thank you! That makes sense, I forgot that cin only accepts up to the first space. Address is also a string (since it will have numbers and letters...this doesn't have to be so in depth that I have to separate it by street address/name, etc, so I'll have to use that there as well.
Actually that didn't quite work...maybe I'm using it wrong? I checked the page on this site about using that code but didn't see where I'm going wrong.

I changed it to

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
case DATAINPUT:
			
			cout << "\n\nPlease input the following user data.\n"; 
			cout << "Name: ";
			getline(cin, name);
			cout << "Address: ";
			getline(cin, address);
			cout << "Filing Status\n1)Single\n2)Married Filing Jointly\n3)Married Filing Seperately\n4)Head of Household\n";
			cout << "Please choose the category number: ";
			cin >> fileStatus;
			// validate menu selection.
			while (fileStatus < 1 || fileStatus > 4)
			{ 
				cout << "Please enter a valid option: ";
				cin >> fileStatus;
			}
			cout << "Income For Tax Year: ";
			cin >> income;
			cout << "Total Expenses: ";
			cin >> expenses;
			cout << "\nYour data has been saved.\n\n";
			


And now get

"Name: Address: "

According to http://www.cplusplus.com/reference/string/getline/ , I don't seem to need anything in between there, so what's happening there? It doesn't even get a chance for input before the issue arises.
Last edited on
Anyone have any input on this?
The problem is in your menu selection. Again, standard input only reads up to a certain point, which will give you problems when you combine strings and numbers in the form of missed newlines.
Last edited on
I'm a bit unclear on what you mean by that. My menu selection being where they choose filing status? Or the previous menu (which I didn't post the code for, but it is in the picture)? Is the code you posted a suggested solution or an example of what you're talking about?

Sorry, I'm definitely a bit lost here...

Edit: I ended up being able to get past it by using cin.ignore . But please explain the above still if you could.
Last edited on
Topic archived. No new replies allowed.