Understanding Stringstream using getline

Hello All,

I am practicing using strings in C++ and the example provided uses getline. I noticed when I use getline the compiler is not outputting the individual inputs of the user, which it should do. Currently the program outputs the correct total price.

Noticing this problem, I switched getline for cin which correctly outputs the individuals inputs of the user and the total price. I would like to know how to recreate these results with getline just to get comfortable with various ways to output.

I have also tried to use cin.ignore, cin.sync, cin.clear as well but no help as of yet.

My code:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

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

cout << "Enter price: ";
getline (cin,mystr);
stringstream(mystr) >> price;
cout << "Enter quantity: ";
getline (cin,mystr);
stringstream(mystr) >> quantity;
cout << "Total price: " << price*quantity <<
endl;
return 0;
}

Put the code you need help with here:

getline (cin,mystr);
stringstream(mystr) >> price;
cout << "Enter quantity: ";
getline (cin,mystr);
stringstream(mystr) >> quantity

I appreciate the time to answer the rookie question.
Thanks,
D Vudoo
Topic archived. No new replies allowed.