Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Some Basics - strings and Conversions

Reply
Thread Tools

Some Basics - strings and Conversions

 
 
Mike Copeland
Guest
Posts: n/a
 
      04-16-2012
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;
}
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      04-16-2012
On 4/16/2012 4:07 PM, Mike Copeland wrote:
> 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.

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


 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      04-17-2012
On 04/17/12 11:07 AM, Mike Copeland wrote:
> 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>

--
Ian Collins
 
Reply With Quote
 
Mike Copeland
Guest
Posts: n/a
 
      04-17-2012
In article <jmib8t$uvl$(E-Mail Removed)>, http://www.velocityreviews.com/forums/(E-Mail Removed)d
says...
> > 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<{{
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      04-17-2012
On 04/18/12 09:59 AM, Mike Copeland wrote:
> In article<jmib8t$uvl$(E-Mail Removed)>, (E-Mail Removed)d
> says...
>>> 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!

--
Ian Collins
 
Reply With Quote
 
Mike Copeland
Guest
Posts: n/a
 
      04-18-2012
In article <(E-Mail Removed)>, (E-Mail Removed)
says...
> >>> 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>
 
Reply With Quote
 
Miles Bader
Guest
Posts: n/a
 
      04-18-2012
(E-Mail Removed) (Mike Copeland) writes:
>> 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.


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

-miles

--
Alliance, n. In international politics, the union of two thieves who have
their hands so deeply inserted in each other's pockets that they cannot
separately plunder a third.
 
Reply With Quote
 
Miles Bader
Guest
Posts: n/a
 
      04-18-2012
Miles Bader <(E-Mail Removed)> writes:
> (E-Mail Removed) (Mike Copeland) writes:
>>> 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.

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

--
Happiness, n. An agreeable sensation arising from contemplating the misery of
another.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: some B&W landscape conversions Mr.T Digital Photography 19 10-15-2010 03:33 PM
Re: some B&W landscape conversions Noons Digital Photography 2 10-13-2010 04:07 PM
Understanding some Java basics kimiraikkonen Java 21 12-18-2007 03:04 PM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
Pointer conversions and Data types conversions vb@gmail.com C Programming 10 08-05-2005 09:51 AM



Advertisments