Some Basics - strings and Conversions

Discussion in 'C++' started by Mike Copeland, Apr 17, 2012.

  1. 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;
    }
     
    Mike Copeland, Apr 17, 2012
    #1
    1. Advertising

  2. Mike Copeland

    red floyd Guest

    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;
    > }
     
    red floyd, Apr 17, 2012
    #2
    1. Advertising

  3. Mike Copeland

    Ian Collins Guest

    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
     
    Ian Collins, Apr 17, 2012
    #3
  4. In article <jmib8t$uvl$>, 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<{{
     
    Mike Copeland, Apr 17, 2012
    #4
  5. Mike Copeland

    Ian Collins Guest

    On 04/18/12 09:59 AM, Mike Copeland wrote:
    > In article<jmib8t$uvl$>, 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
     
    Ian Collins, Apr 17, 2012
    #5
  6. In article <>,
    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>
     
    Mike Copeland, Apr 18, 2012
    #6
  7. Mike Copeland

    Miles Bader Guest

    (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.
     
    Miles Bader, Apr 18, 2012
    #7
  8. Mike Copeland

    Miles Bader Guest

    Miles Bader <> writes:
    > (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.
     
    Miles Bader, Apr 18, 2012
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brendan Illingworth

    Book on VHDL basics and HDL based design

    Brendan Illingworth, Jan 13, 2006, in forum: VHDL
    Replies:
    2
    Views:
    2,091
    Ralf Hildebrandt
    Jan 13, 2006
  2. jeffc
    Replies:
    4
    Views:
    2,763
    jeffc
    Dec 9, 2004
  3. Replies:
    10
    Views:
    714
    Jasen Betts
    Aug 5, 2005
  4. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    787
    Malcolm
    Jun 24, 2006
  5. kimiraikkonen

    Understanding some Java basics

    kimiraikkonen, Dec 16, 2007, in forum: Java
    Replies:
    21
    Views:
    780
Loading...

Share This Page