M
Mike Copeland
In the following code, even though the program executes, the 2nd
prompt fails to process data. The 1st prompt requires 2 returns (why is
that?), but the 2nd doesn't: it prompts, waits for data, and goes on to
the last "cout" with no value in "quantity".
I know this is basic C++ stuff, but I'm trying to convert my massive
C code applications (particularly using C strings) to something more
acceptable here: basic::strings and directed I/o and conversions. Thus,
I'm learning this stuff for the first time... 8<{{ TIA
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main ()
{
string mystr;
float price = 0;
int quantity = 0;
cout << "Enter Price and Description: "; // numeric & text
getline (cin, mystr);
stringstream(mystr) >> price; // parse only the numeric portion
cout << "Enter quantity: ";
getline (cin, mystr);
stringstream(mystr) >> quantity;
cout << "Total price: " << price*quantity << endl;
return 0;
}
prompt fails to process data. The 1st prompt requires 2 returns (why is
that?), but the 2nd doesn't: it prompts, waits for data, and goes on to
the last "cout" with no value in "quantity".
I know this is basic C++ stuff, but I'm trying to convert my massive
C code applications (particularly using C strings) to something more
acceptable here: basic::strings and directed I/o and conversions. Thus,
I'm learning this stuff for the first time... 8<{{ TIA
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main ()
{
string mystr;
float price = 0;
int quantity = 0;
cout << "Enter Price and Description: "; // numeric & text
getline (cin, mystr);
stringstream(mystr) >> price; // parse only the numeric portion
cout << "Enter quantity: ";
getline (cin, mystr);
stringstream(mystr) >> quantity;
cout << "Total price: " << price*quantity << endl;
return 0;
}