confused with cin in C++

Discussion in 'C++' started by John Ruan, Apr 7, 2008.

  1. John Ruan

    John Ruan Guest

    This is a small program in C++. I want to try to type in real number instead
    of integer to see how the program runs.
    And I am confused. If I input integer for both variables, the program runs
    fine. But if I input a real number for the height (like 68.2), the second
    cin doesn't work at all!!

    I expected any real number will be truncated and that is my only
    expectation. But it seems that there are other problems. Can anybody try it?



    #include <iostream>
    using namespace std;

    int main(void)
    {
    const double INCHES_PER_METER = 39.37;
    const double POUNDS_PER_KG = 2.24;

    int height;
    int weight;

    cout << "METRIC CONVERTER" << endl << endl ;
    cout << "Enter your height in inches " ;
    cout << "(No fractions, please!) : " ;
    cin >> height;

    cout << "Enter your weight in pounds" ;
    cout << "(No fractions, please!)" ;
    cin >> weight;
    cout << endl ;

    double metric_height = height/INCHES_PER_METER;

    double metric_weight = weight/POUNDS_PER_KG;


    cout << "Your height is " << metric_height << " meters." << endl;
    cout << "Your weight is " << metric_weight << " kilograms." << endl;

    return 0;
    }
     
    John Ruan, Apr 7, 2008
    #1
    1. Advertising

  2. John Ruan

    Christopher Guest

    On Apr 7, 11:33 am, "John Ruan" <> wrote:
    > This is a small program in C++. I want to try to type in real number instead
    > of integer to see how the program runs.
    > And I am confused. If I input integer for both variables, the program runs
    > fine. But if I input a real number for the height (like 68.2), the second
    > cin doesn't work at all!!
    >
    > I expected any real number will be truncated and that is my only
    > expectation. But it seems that there are other problems. Can anybody try it?
    >
    > #include <iostream>
    > using namespace std;
    >
    > int main(void)
    > {
    > const double INCHES_PER_METER = 39.37;
    > const double POUNDS_PER_KG = 2.24;
    >
    > int height;
    > int weight;
    >
    > cout << "METRIC CONVERTER" << endl << endl ;
    > cout << "Enter your height in inches " ;
    > cout << "(No fractions, please!) : " ;
    > cin >> height;
    >
    > cout << "Enter your weight in pounds" ;
    > cout << "(No fractions, please!)" ;
    > cin >> weight;
    > cout << endl ;
    >
    > double metric_height = height/INCHES_PER_METER;
    >
    > double metric_weight = weight/POUNDS_PER_KG;
    >
    > cout << "Your height is " << metric_height << " meters." << endl;
    > cout << "Your weight is " << metric_weight << " kilograms." << endl;
    >
    > return 0;
    >
    > }


    Your assumption is incorrect. If a stream is unable to convert to the
    requested type it sets an error bit. I don't remember which one, but I
    believe you can check it with cin.bad(). I would of course refresh my
    memory by reading up on streams and stream error handling, but I'll
    leave that to you.

    The same rules apply to other streams.

    That is how you can check whether the user typed in a value of the
    type you were expecting and then tell them they aren't following
    instructions and to try again.
     
    Christopher, Apr 7, 2008
    #2
    1. Advertising

  3. John Ruan

    Bo Persson Guest

    John Ruan wrote:
    > This is a small program in C++. I want to try to type in real
    > number instead of integer to see how the program runs.
    > And I am confused. If I input integer for both variables, the
    > program runs fine. But if I input a real number for the height
    > (like 68.2), the second cin doesn't work at all!!
    >


    The input is originally text, which is scanned for digits that can be
    part of an integer. The first input reads a 6 and an 8, and gives you
    the number 68.

    When you try to read a second integer, the first character found is a
    period, which is an error for integers. You get no number.



    Bo Persson
     
    Bo Persson, Apr 7, 2008
    #3
    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. Gregory W. Ernest

    Java equivalent of cin

    Gregory W. Ernest, Jan 18, 2005, in forum: Java
    Replies:
    3
    Views:
    25,177
    gstkein
    Aug 3, 2008
  2. TaiwanNoWhere

    cin and cin.clear() problem

    TaiwanNoWhere, Oct 13, 2003, in forum: C++
    Replies:
    8
    Views:
    4,152
    P.J. Plauger
    Oct 17, 2003
  3. Chris Mantoulidis

    std::cin.ignore() and std::cin.clear()

    Chris Mantoulidis, Jan 6, 2004, in forum: C++
    Replies:
    5
    Views:
    17,268
    Kevin Saff
    Jan 6, 2004
  4. Aleander

    cin and cin.getline()

    Aleander, Mar 6, 2005, in forum: C++
    Replies:
    5
    Views:
    8,799
    Alex Vinokur
    Mar 6, 2005
  5. Fernando
    Replies:
    4
    Views:
    1,855
    Fernando
    Nov 16, 2011
Loading...

Share This Page