Getline() function taking all inputs

Ok, so I have been following the tutorial on this website, and I have been trying to use the getline() function input with cin, as it appears on the C++ tutorial.
The problem I have is that when I follow the code example exactly, both the inputs are taken at the first input, rather than taking one the the other. the code I'm using is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
	string mystr;		//define input string
	float price=0;		//initialise float "price"
	int quantity=0;		//initialise int "quantity"

	cout<<"Enter price: ";
	getline (cin,mystr);				//input string
	stringstream(mystr) >> price;		//return value of string and assign to float "price"

	cout<<endl<<"Enter quantity: ";
	getline (cin,mystr);				//input string
	stringstream(mystr) >> quantity;	//return value of string and assign to int "quantity"

	cout<<endl<<"Total price = "<<price*quantity<<endl;

	return 0;
}

and the output I get is
1
2
3
4
5
6
Enter price: 45
5

Enter quantity: 2

Total price = 225

Clearly there is something wrong, but being a complete beginner to programmar I don't know what the problem is.
Thanks in advance.
45 * 5 = 225


It looks like the second value for price you entered (5) is being read as the quantity.

Try running it again and enter the numbers more carefully.
@ Galik: If that were the case wouldn't line 17 overwrite the '5' that was strored in mystr? I've seen this issue with this tutorial once or twice before I think it was an issue with certain compilers, the answer was to flush the input buffer after you assign mystr to price or set the char delimeter in the getline() function. Either one of which should probably be present in the tutorial to begin with as it is good practice.
The 5 is on a separate line so I don't think it would be read with the first getline(). So I think line 17 would then read in the 5.
Ok, I could see that. The way he wrote his output implied to me that he had a chance to enter the '2'.
I couldn't see anything in the tutorial to flush the input buffer.
As for the program inputs and outputs, I should have been more clear:
when the program is run, it says enter price. You type in a value and hit return, and then you have to enter another value... this was not expected. once you've done that, the quantity pops up and you enter a third value. The total price is then calculated from the first two values, which is wrong.
The compiler I'm using is MS Visual C++ 6.
What would be the best way to solve this?
Cheers
Then perhaps you need to output your endl before doing the getline?

Or you could use flush:

 
std::cout << flush;
Neither of those work I'm afraid. I even tried assigning different strings for each input, but that didn't work either. I shal continue to play around.
Thanks guys.

Edit:
Ok, I put a cout<<"1" after the getline() and a cout<<"2" after the next line to try and idetify where the problem was, and they were both displayed after the second 'wrong' input. I'm thinking it's to do with how the compiler handles the function.
Last edited on
I didn't have any problems with that at all. Here was my output. I'm not sure why in the OPs run he was able to enter the extra 2. Weird.
Enter price: 45

Enter quantity: 5

Total price = 225


The way that the OP used the endl is weird. In general use it after the text that you need to show up. If you don't you may or may not see it in the console until some other event occurs and it will vary by environment. In mine the text "Enter quantity" showed up after the first getline statement executed but it didn't cause me any problems.
Yeah, it worked fine for me, straight to the compiler... no probs.

But.... You say you got that unmodified from a tutorial???

I mean... really??

That's atrocious! I can't believe that a tutorial would tell people to use getline() and stringstream() in C++ like that! If that's really true, its a bad tutorial...

It's utterly pointless and counter-productive to use stringstream and getline, when you can just use cin >>

Anyway, here is my version of it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
using namespace std;

int main() {
	string mystr;
	float price;
	int quantity;

	cout << "Enter price: ";
	cin >> price;
	cout << "Enter quantity: ";
	cin >> quantity;
	cout << "\nTotal price = " << price * quantity << endl;
	return 0;
}



EDIT: I suppose it would be ok if the tutorial was specifically trying to demonstrate how to use getline and stringstream...
Last edited on
Yeah it was. That and because it would have problems if you type another character rather than a number.
I downloaded a free compiler, and it works. I think perhaps my iostream.h was out of date or lacking in something crucial, but I still would rather use the program I'm used to! =P
I talked to a friend who knows C, and he said that even in C, getline() had it's problem, and that it was worth building your own.

@Ryan: It did seem to be alot of hassel for such a tiny program, but the tutorial is here "http://www.cplusplus.com/doc/tutorial/basic_io/", last one on the page. Even on this website! =P

Anyway. Cheers guys.
Topic archived. No new replies allowed.