CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2011
    Posts
    1

    Unable to input (cin) when using strings

    There is no error code, that's the problem, when I use the code below.
    Everything worked fine until I added the code that is between the @@@@@@@@@@

    #include <iostream>
    #include <string>
    #include <sstream>
    using namespace std;

    int main()
    {
    cout << "Hello World" << endl;
    int a=5, b=20, c, d, e, f, age;
    cout << "20-5=";
    cout << b-a << endl;
    string mystr;
    string mystr2;
    cout << "Hi, what's your first name?" << endl;
    getline (cin, mystr);
    cout << "Hi " << mystr << ", what a nice name!" << endl;
    cout << "How old are you?" << endl;
    cin >> age;
    c=age*365;
    d=c*24;
    e=d*60;
    f=e*60;
    cout << "You have lived about:" << endl << age << " years." << endl << c << " days." << endl << d << " hours." << endl << e << " minutes." << endl << f << " seconds." << endl;
    int i;
    cout << "Enter a number: ";
    cin >> i;
    cout << "The number you entered is " << i << " and it's double is " << i*2 << endl << endl;

    @@@@@@@@@@
    float price=0;
    int quantity=0;
    cout << "Enter price: ";
    getline (cin,mystr2);
    stringstream(mystr2) >> price;
    cout << "Enter quantity: ";
    getline (cin,mystr2);
    stringstream(mystr2) >> quantity;
    cout << "Total price: " << price*quantity <<
    endl;
    @@@@@@@@@@
    system("pause");
    return 0;
    }



    The code on itself works, tested in a seperate project using only that code, but when including it in this code the output is that when asking for the price and quantity I see:
    Price:Quantity:
    Then I can only input something after the quantity, meaning that the result (price) is always 0 as I just can't input to price.

    Anyone knows what I'm doing wrong?

    This is a test program, I know there are different ways to do this but just this on itself isn't working and I'm trying for hours..

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Unable to input (cin) when using strings

    You have ...

    Code:
    cin >> i;
    
    // other code ... and then
    
    getline (cin,mystr2);
    This is a common problem when mixing operator >> with getline.

    After the first cin (cin >> i), the ENTER ('\n') is still
    in the input stream buffer. The getline then reads an
    empty string (since it stops reading after the first '\n'
    that it encounters). One way around this is to do an ignore
    after the operator >> ...

    Code:
    cin >> i;
    cin.ignore(1000,'\n');
    In this case, why are you reading the price and quantity
    into strings and then using stringstream ? You can use
    operator >> directly ...
    Code:
    cin >> price;
    
    //
    
    cin >> quantity;

  3. #3
    Join Date
    Dec 2011
    Location
    Bucharest, Romania
    Posts
    29

    Re: Unable to input (cin) when using strings

    I run it and entered only one name. Why is that app of yours required and additional cr/lf line ?
    ======

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured