Why is Getline() skipping user input?

Started by
3 comments, last by alvaro 11 years, 9 months ago
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;
}

Advertisement
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 >>).
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.
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;

Instead of your getline code.

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.
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'.

This topic is closed to new replies.

Advertisement