Some Basics - strings and Conversions

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;
}
 
R

red floyd

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".
[redacted]

What platform and compiler are you using? It looks OK source-wise,
and it works for me, compiling with 0 warnings, g++ 4.5.3, Cygwin.
 
I

Ian Collins

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".

It shouldn't. What platform are you using?

<snip>
 
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".
[redacted]

What platform and compiler are you using? It looks OK source-wise,
and it works for me, compiling with 0 warnings, g++ 4.5.3, Cygwin.
VS6.0, Windows XP SP3
No errors, just bizarre inconsistent behavior... 8<{{
 
I

Ian Collins

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".
[redacted]

What platform and compiler are you using? It looks OK source-wise,
and it works for me, compiling with 0 warnings, g++ 4.5.3, Cygwin.
VS6.0, Windows XP SP3

Nobody in their right mind uses VS6 these days!
 
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".
[redacted]

What platform and compiler are you using? It looks OK source-wise,
and it works for me, compiling with 0 warnings, g++ 4.5.3, Cygwin.
VS6.0, Windows XP SP3

Nobody in their right mind uses VS6 these days!
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Yes, I get that a lot. 8<{{
If I could switch to one of the free compilers, I would do so.
However, with over 1,000,000 lines of code with dependencies on STL and
other things that don't seem to be in the free versions, I can't. And
obviously, I can't afford to purchase a paid version... <sigh>
 
M

Miles Bader

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Yes, I get that a lot. 8<{{
If I could switch to one of the free compilers, I would do so.
However, with over 1,000,000 lines of code with dependencies on STL and
other things that don't seem to be in the free versions, I can't.

Wait, there are free C++ compilers without an STL implementation?!

-miles
 
M

Miles Bader

Miles Bader said:
Wait, there are free C++ compilers without an STL implementation?!

BTW, I suspect a bigger problem is that you've probably got 1,000,000
lines of code with dependencies on the wacky non-standard behavior of
VS6...

-miles
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top