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:
@ 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.
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
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.
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 >>
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.