get line troubles in Basic Input/Output

Okay, when I do this it works:
--------------------
string mystr;
float cost=0;
int quantity=0;
cout << "\nEnter Cost: ";
getline (cin,mystr);
stringstream(mystr) >> cost;
cout << "\nEnter Quantity: ";
getline (cin,mystr);
stringstream(mystr) >> quantity;
cout << "\nTotal Cost: " << cost*quantity << endl;
--------------------

However, when I add it to the rest of the program, it skips the user input for cost and goes straight to quantity. I have tried taking each part out seperately to see what was doing it, but it did it regardless - only working when it is alone like above.
------------
{
cout << "First line\n";// The (\) doesn't show up
cout << "Second line\n" << "Third line\nFourth line" << endl; // "endl" and "\n" are *essentially* the same

int p;
float i, r;
cout << "\nTo calculate simple one-year interest, please enter initial amount: ";
cin >> p;
cout << "\nAnd the interest rate (in decimal form): ";
cin >> r;
i = p*r;
cout << "The interest of " << p << " at the rate of " << r << " is " << i <<".\n\n";
//Using "\n" twice doubles its function.

int a;
cout << "Bloodninja: Age? " << "\nYou: ";
cin >> a;
cout << "Bloodninja: Perfect..." << "\nYou: Why?" << "\nBloodninja: I put on my wizard hat and cloak." << endl;
cout << "You: Oh god not again!" << endl;

string mystr;
float cost=0;
int quantity=0;
cout << "\nEnter Price: ";
getline (cin,mystr);
stringstream(mystr) >> cost;
cout << "\nEnter Quantity: ";
getline (cin,mystr);
stringstream(mystr) >> quantity;
cout << "\nTotal Price: " << cost*quantity << endl;

return 0;
}
------------------
Any error messages or warnings?

Does it display the Enter Price message?
After every cin statement place a cin.ignore()

cin doesn't discard the newline character. When a getline is called it sees the newline in the stream and continues, thus getting no input.

The safest way around this is not to combine the two types of input. Instead use stringstreams.
http://www.cplusplus.com/forum/articles/6046/


Thanks again eker676. No doubt you'll see me around the beginner's forum for a while.
Topic archived. No new replies allowed.