cin.getline() problem

Discussion in 'C++' started by Markus, Sep 1, 2006.

  1. Markus

    Markus Guest

    Hi,

    I want to get an integer from the user.
    > cin >> a;

    If the user inserts a character instead of an int the program goes
    crazy. So I tried something like this:

    >int get_value() {
    > int a;
    > char b[6];
    > cout << "Insert a value " << endl;
    > cin.getline(b, 7, '\n');
    > a = atoi(b);
    > cout << "a: " << a << endl;
    > return 0;
    >}


    This program works pretty well. But if I put this code in my main
    program the b-array does not get a value.
    When I change my code the array does not get a value.

    >int get_value() {
    > int a;
    > int x;
    > char b[6];
    > cout << "Insert a value" << endl;
    > cin >> x;
    > cout << "Insert a value " << endl;
    > cin.getline(b, 7, '\n');
    > a = atoi(b);
    > cout << "a: " << a << endl;
    > return 0;
    >}


    What is the problem with the cin >> before the cin.getline() ?

    Best Regards

    Markus
    Markus, Sep 1, 2006
    #1
    1. Advertising

  2. Markus

    Rolf Magnus Guest

    Markus wrote:

    > Hi,
    >
    > I want to get an integer from the user.
    >> cin >> a;

    > If the user inserts a character instead of an int the program goes
    > crazy.


    Then you probably forgot to check for the stream state after reading.

    > So I tried something like this:
    >
    >>int get_value() {
    >> int a;
    >> char b[6];
    >> cout << "Insert a value " << endl;
    >> cin.getline(b, 7, '\n');
    >> a = atoi(b);
    >> cout << "a: " << a << endl;
    >> return 0;
    >>}

    >
    > This program works pretty well. But if I put this code in my main
    > program the b-array does not get a value.
    > When I change my code the array does not get a value.
    >
    >>int get_value() {
    >> int a;
    >> int x;
    >> char b[6];
    >> cout << "Insert a value" << endl;
    >> cin >> x;
    >> cout << "Insert a value " << endl;
    >> cin.getline(b, 7, '\n');
    >> a = atoi(b);
    >> cout << "a: " << a << endl;
    >> return 0;
    >>}

    >
    > What is the problem with the cin >> before the cin.getline() ?


    When you type in the value for x, you press return aftewards. The resulting
    \n stays in the buffer and gets picked up by your getline call.
    Rolf Magnus, Sep 1, 2006
    #2
    1. Advertising

  3. Markus

    Kai-Uwe Bux Guest

    Markus wrote:

    > Hi,
    >
    > I want to get an integer from the user.
    >> cin >> a;

    > If the user inserts a character instead of an int the program goes
    > crazy.

    [snip]

    You should test for success:

    if ( cin >> a ) {
    // input could be parsed as an int.
    } else {
    // input was not an int. take error recovery measures.
    }



    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Sep 1, 2006
    #3
  4. Markus

    David Harmon Guest

    On 1 Sep 2006 02:42:54 -0700 in comp.lang.c++, "Markus"
    <> wrote,
    >What is the problem with the cin >> before the cin.getline() ?


    It leaves '\n' in the stream, and getline sees it as an empty line.
    David Harmon, Sep 1, 2006
    #4
  5. Markus

    Jim Langston Guest

    "David Harmon" <> wrote in message
    news:...
    > On 1 Sep 2006 02:42:54 -0700 in comp.lang.c++, "Markus"
    > <> wrote,
    >>What is the problem with the cin >> before the cin.getline() ?

    >
    > It leaves '\n' in the stream, and getline sees it as an empty line.


    The solution being, of course, just to do another getline and ignore the
    results.

    Although you'll probably want to check the state of the stream after the int
    input, because if the user typed a character instead of a number it'll be
    bad and you'll have to clear it.
    Jim Langston, Sep 1, 2006
    #5
  6. Markus

    Rolf Magnus Guest

    Jim Langston wrote:

    >
    > "David Harmon" <> wrote in message
    > news:...
    >> On 1 Sep 2006 02:42:54 -0700 in comp.lang.c++, "Markus"
    >> <> wrote,
    >>>What is the problem with the cin >> before the cin.getline() ?

    >>
    >> It leaves '\n' in the stream, and getline sees it as an empty line.

    >
    > The solution being, of course, just to do another getline and ignore the
    > results.


    Or just use the ignore() member function.
    Rolf Magnus, Sep 1, 2006
    #6
  7. Markus

    Markus Guest

    Rolf Magnus wrote:
    > Jim Langston wrote:
    >
    > >
    > > "David Harmon" <> wrote in message
    > > news:...
    > >> On 1 Sep 2006 02:42:54 -0700 in comp.lang.c++, "Markus"
    > >> <> wrote,
    > >>>What is the problem with the cin >> before the cin.getline() ?
    > >>
    > >> It leaves '\n' in the stream, and getline sees it as an empty line.

    > >
    > > The solution being, of course, just to do another getline and ignore the
    > > results.

    >
    > Or just use the ignore() member function.


    Thank you for your answers.

    Another solution seems to be:
    > cin.seekg(0, ios::end);
    > cin.clear();


    But now I have got another problem. If I insert something like 23c3
    just 23 is stored in a . Everything that follows a char is ignored
    without making any problems. To check each char in the array wheter it
    is an integer or not seems to be a little bit to costly.
    This source code has exactly the same effect:
    > int function() {
    > int a;
    > if ( cin >> a ) {
    > if (a < 1000000) {
    > cout << "a = " << a << endl;
    > } else {
    > cout << "Wrong input" << endl;
    > }
    > } else {
    > cout << "Wrong input" << endl;
    > a = 0;
    > }
    > cin.seekg(0, ios::end);
    > cin.clear();
    > return 0;
    > }


    Is there an elegant way to check that the whole input is not equal with
    a if someone inserts something like 23c3 ?

    Regards

    Markus
    Markus, Sep 1, 2006
    #7
  8. Markus

    Jim Langston Guest

    "Markus" <> wrote in message
    news:...
    >
    > Rolf Magnus wrote:
    >> Jim Langston wrote:
    >>
    >> >
    >> > "David Harmon" <> wrote in message
    >> > news:...
    >> >> On 1 Sep 2006 02:42:54 -0700 in comp.lang.c++, "Markus"
    >> >> <> wrote,
    >> >>>What is the problem with the cin >> before the cin.getline()
    >> >>>?
    >> >>
    >> >> It leaves '\n' in the stream, and getline sees it as an empty line.
    >> >
    >> > The solution being, of course, just to do another getline and ignore
    >> > the
    >> > results.

    >>
    >> Or just use the ignore() member function.

    >
    > Thank you for your answers.
    >
    > Another solution seems to be:
    >> cin.seekg(0, ios::end);
    >> cin.clear();

    >
    > But now I have got another problem. If I insert something like 23c3
    > just 23 is stored in a . Everything that follows a char is ignored
    > without making any problems. To check each char in the array wheter it
    > is an integer or not seems to be a little bit to costly.
    > This source code has exactly the same effect:
    >> int function() {
    >> int a;
    >> if ( cin >> a ) {
    >> if (a < 1000000) {
    >> cout << "a = " << a << endl;
    >> } else {
    >> cout << "Wrong input" << endl;
    >> }
    >> } else {
    >> cout << "Wrong input" << endl;
    >> a = 0;
    >> }
    >> cin.seekg(0, ios::end);
    >> cin.clear();
    >> return 0;
    >> }

    >
    > Is there an elegant way to check that the whole input is not equal with
    > a if someone inserts something like 23c3 ?
    >
    > Regards
    >
    > Markus


    Now you're getting into tricky waters. When you >> int cin will return the
    number portion. The "c3" should remain waiting to be read. I know that
    there's some way to see if anything is waiting, but not sure what it is.
    Someone with more knowledge of iostream will probably have the answer.
    Jim Langston, Sep 1, 2006
    #8
  9. Markus

    Guest

    Markus wrote:
    > Hi,
    >
    > I want to get an integer from the user.
    > > cin >> a;

    > If the user inserts a character instead of an int the program goes
    > crazy. So I tried something like this:
    >
    > >int get_value() {
    > > int a;
    > > char b[6];
    > > cout << "Insert a value " << endl;
    > > cin.getline(b, 7, '\n');
    > > a = atoi(b);
    > > cout << "a: " << a << endl;
    > > return 0;
    > >}

    >
    > This program works pretty well. But if I put this code in my main
    > program the b-array does not get a value.
    > When I change my code the array does not get a value.
    >
    > >int get_value() {
    > > int a;
    > > int x;
    > > char b[6];
    > > cout << "Insert a value" << endl;
    > > cin >> x;
    > > cout << "Insert a value " << endl;

    cin.ignore (1, '\n'); // it can ignore one
    '\n' that you last typed during input "x" // my
    english is poor ,sorry
    > > cin.getline(b, 7, '\n');
    > > a = atoi(b);
    > > cout << "a: " << a << endl;
    > > return 0;
    > >}

    >
    > What is the problem with the cin >> before the cin.getline() ?
    >
    > Best Regards
    >
    > Markus
    , Sep 3, 2006
    #9
  10. Markus

    Markus Guest

    Jim Langston wrote:
    > "Markus" <> wrote in message
    > news:...
    > >
    > > Rolf Magnus wrote:
    > >> Jim Langston wrote:
    > >>
    > >> >
    > >> > "David Harmon" <> wrote in message
    > >> > news:...
    > >> >> On 1 Sep 2006 02:42:54 -0700 in comp.lang.c++, "Markus"
    > >> >> <> wrote,
    > >> >>>What is the problem with the cin >> before the cin.getline()
    > >> >>>?
    > >> >>
    > >> >> It leaves '\n' in the stream, and getline sees it as an empty line.
    > >> >
    > >> > The solution being, of course, just to do another getline and ignore
    > >> > the
    > >> > results.
    > >>
    > >> Or just use the ignore() member function.

    > >
    > > Thank you for your answers.
    > >
    > > Another solution seems to be:
    > >> cin.seekg(0, ios::end);
    > >> cin.clear();

    > >
    > > But now I have got another problem. If I insert something like 23c3
    > > just 23 is stored in a . Everything that follows a char is ignored
    > > without making any problems. To check each char in the array wheter it
    > > is an integer or not seems to be a little bit to costly.
    > > This source code has exactly the same effect:
    > >> int function() {
    > >> int a;
    > >> if ( cin >> a ) {
    > >> if (a < 1000000) {
    > >> cout << "a = " << a << endl;
    > >> } else {
    > >> cout << "Wrong input" << endl;
    > >> }
    > >> } else {
    > >> cout << "Wrong input" << endl;
    > >> a = 0;
    > >> }
    > >> cin.seekg(0, ios::end);
    > >> cin.clear();
    > >> return 0;
    > >> }

    > >
    > > Is there an elegant way to check that the whole input is not equal with
    > > a if someone inserts something like 23c3 ?
    > >
    > > Regards
    > >
    > > Markus

    >
    > Now you're getting into tricky waters. When you >> int cin will return the
    > number portion. The "c3" should remain waiting to be read. I know that
    > there's some way to see if anything is waiting, but not sure what it is.
    > Someone with more knowledge of iostream will probably have the answer.


    I had a resembling idea yesterday. If there is still the "c3" in the
    stream I know that the input was wrong. If there is a '\n' in the
    stream the input was correct.
    I am not sure whether this always works but now my source code goes
    something like this (the aim is to get an input from the user that
    consists of no more than 6 decimals, if it is too long or if a char is
    inserted an error message will be displayed and the user has to insert
    this value again):

    int check_int_input() {
    int value = 0;
    int decimals = 6;
    char check_istream = '\n';

    while (1) {
    cin.seekg(0, ios::end);
    cin.clear();
    cin >> value;
    if (cin.good()) {
    if ((cin.get(check_istream)) && (check_istream != '\n')) {
    cout << "ERROR. Wrong input. Try again" << endl;
    cin.seekg(0, ios::end);
    cin.clear();
    continue;
    }
    if (value >= (pow(10, decimals))) {
    cout << "ERROR. Wrong input. Try again." << endl;
    cin.seekg(0, ios::end);
    cin.clear();
    continue;
    }
    // Correct input
    break;
    } else {
    cout << "ERROR. Wrong input. Try again." << endl;
    cin.seekg(0, ios::end);
    cin.clear();
    continue;
    }
    }
    return value;
    }

    Maybe someone has got a similiar problem.
    There is only one problem ... cin.getline() vanished.

    Regards

    Markus
    Markus, Sep 3, 2006
    #10
  11. Markus

    Jerry Coffin Guest

    In article <>,
    says...

    [ ... ]

    > I had a resembling idea yesterday. If there is still the "c3" in the
    > stream I know that the input was wrong. If there is a '\n' in the
    > stream the input was correct.


    One common way of dealing with this is to start by reading a line into a
    string, then attempt to convert the string to an int (or whatever). If
    that consumes the entire string, it succeeded, but if there's something
    left, it failed.

    int getval(int max) {
    static string prompts[] = {
    "Please enter a value",
    "ERROR. Wrong input. Try again"
    };

    bool error = false;

    int value = max+1;
    bool extra_junk;

    do {
    cout << prompts[error] << ": " << flush;
    error=true;
    string temp;
    getline(cin, temp);
    stringstream t(temp);
    t >> value;
    extra_junk=!t.eof();
    } while (extra_junk || value > max);
    return value;
    }

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Sep 3, 2006
    #11
  12. Markus

    Guest

    int get_value() {
    > int a;
    > int x;
    > char b[6];
    > cout << "Insert a value" << endl;
    > cin >> x;

    cin.ignore (1, '\n');
    > cout << "Insert a value " << endl;
    > cin.getline(b, 7, '\n');
    > a = atoi(b);
    > cout << "a: " << a << endl;
    > return 0;
    >}

    why not try it!!

    I think that your problem is forget the '\n', then when you run "--
    cin.getline(b, 7, '\n'); --", computer
    will read the last '\n' first , and read senven chars of you typing!
    "getline" function read '\n' first and it stop reading, because it
    ending by '\n' char.
    "ignore " function , you can look up the MSDN, my english is poor
    ,so i can't s describe clearly!

    and i don't know whether rigtht of my posted; sorry,
    i hope that i can help you !
    , Sep 4, 2006
    #12
    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. Poof
    Replies:
    1
    Views:
    382
  2. Aleander

    cin and cin.getline()

    Aleander, Mar 6, 2005, in forum: C++
    Replies:
    5
    Views:
    8,684
    Alex Vinokur
    Mar 6, 2005
  3. Chris
    Replies:
    7
    Views:
    440
    Chris
    Oct 20, 2006
  4. Devendra_Vidhale

    newbie problem with cin and getline

    Devendra_Vidhale, Jul 17, 2007, in forum: C++
    Replies:
    20
    Views:
    1,150
    James Kanze
    Jul 19, 2007
  5. bintom

    Problem with cin.getline()

    bintom, May 29, 2010, in forum: C++
    Replies:
    4
    Views:
    1,326
Loading...

Share This Page