# Why is Getline() skipping user input?

This topic is 2258 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

When I run my program, a portion of the code( that involves getline()) is skipped. The first getline() works fine but the others don't receive user input and don't display otherthing either. I don't see the error in the code:

 #include <iostream> #include <string> using namespace std; struct CandyBar { string brand_nam; float weight; int calories; }; int main() { CandyBar * snack = new CandyBar[3]; // Dynamic Array holding structures cout << "What is the name of the brand: " << endl; getline(cin, snack[0].brand_nam); // First element cout << "What is the weight of the brand: " << endl; cin >> snack[0].weight; cout << "What is the calories of the brand: " << endl; cin >> snack[0].calories; cout << " " << endl; cout << "What is the name of the brand: " << endl; getline(cin, snack[1].brand_nam); cout << "What is the weight of the brand: " << endl; cin >> snack[1].weight; cout << "What is the calories of the brand: " << endl; cin >> snack[1].calories; cout << " " << endl; cout << "What is the name of the brand: " << endl; getline(cin, snack[2].brand_nam); cout << "What is the weight of the brand: " << endl; cin >> snack[2].weight; cout << "What is the calories of the brand: " << endl; cin >> snack[2].calories; cout << " " << endl; cout << " " << endl; cout << "First brand is: "<< snack[0].brand_nam << " which has " << snack[0].weight << " Ibs " << " and " << snack[0].calories << " Cal " << endl; cout << " " << endl; cout << "Second brand is: " << snack[1].brand_nam << " which has " << snack[1].weight << " Ibs " << " and " << snack[1].calories << " Cal " << endl; cout << " " << endl; cout << "Third brand is: " << snack[2].brand_nam << " which has " << snack[2].weight << " Ibs " << " and " << snack[2].calories << " Cal " << endl; delete []snack; system("Pause"); return 0; }  Edited by Czar05

##### Share on other sites
I haven't looked at your code carefully, but the problem you describe is usually the result of mixing calls to cin >> ...' and getline'. The former doesn't remove the '\n' at the end of the line, and the next getline just reads the '\n' as an empty line.

The easiest way to make your input robust to this type of thing is to always read whole lines of text with getline', and then parse them any way you want (e.g., using istringstream so you can still use operator >>). Edited by alvaro

##### Share on other sites
I am sorry I don't quite comprehend what you saying, can you elaborate more(with code perhaps) if you can. Thanks for the quick response by the way, alvaro.

##### Share on other sites
What he is saying is your are reading in everything up to, but not including the newline character(s) when you are calling getline, so your program is reading for the first one: "Some text I entered" and the second possibly is reading: "\n" and so on, or it may just have stopped due to the newline character altogether, I can't remember.

He also says you can do something like this:
cin >> snack[0].brand_nam;

An option to fix any of these input bugs that are creeping up on you is to clear out your input buffer after each call. I don't think this is necessary using the little code snippet I typed, but don't quote me on that.

EDIT: You also are making two different input calls, which was the first part of what Alvaro stated, where you use getline(cin, snack[0].brand_nam); and cin >> snack[0].weight; where the getline doesn't read in the newline. Edited by Dragonsoulj

##### Share on other sites
Try this code:
#include <iostream> #include <string> int main() { int i; std::cin >> i; std::cout << "You typed the integer " << i << '\n'; std::string line; std::getline(std::cin, line); std::cout << "And then you typed \"" << line <t'< "\"\n"; } 

The first cin >> ...' consumes only an integer. If there is anything else in the line you type, it will be caught by the call to getline'. If you only typed an integer and then <enter>, line' will be empty. That's probably the kind of thing that is going on in your program.

If you want to avoid that, make sure you read all your input in whole lines, using `getline'.

1. 1
2. 2
3. 3
Rutin
19
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 32
• ### Forum Statistics

• Total Topics
632629
• Total Posts
3007519
• ### Who's Online (See full list)

There are no registered users currently online

×