resurrecting cin

Discussion in 'C++' started by float_dublin, Sep 14, 2005.

  1. float_dublin

    float_dublin Guest

    Hi All,
    I just have found out (for myself) that the program:

    //**************************************
    #include <iostream>

    int main()
    {
    int a=0;
    while(true)
    {
    std::cout << "Input a NUMBER!!!\n" << std::endl;
    std::cin>>a; /// Here I input "a" eg char
    if(!std::cin)
    {
    std::cout<<"Oouch, you have not listend me... I'm dying....\n";
    std::cin.clear(); /// I want just to make another turn and not
    cycle for eternity
    }
    std::cout<<a<<"\n";
    }
    return 0;
    }
    //**************************************

    cycles forever if i input some char instead of numbers. Fast look throw
    books have not showed me any solution for this but inputing into some
    string and then parsing. All i want is just resurrect cin to make
    another turn, is it possible?

    --
    float_dublin
     
    float_dublin, Sep 14, 2005
    #1
    1. Advertising

  2. float_dublin

    Anand Guest

    float_dublin wrote:
    > Hi All,
    > I just have found out (for myself) that the program:
    >
    > //**************************************
    > #include <iostream>
    >
    > int main()
    > {
    > int a=0;
    > while(true)
    > {
    > std::cout << "Input a NUMBER!!!\n" << std::endl;
    > std::cin>>a; /// Here I input "a" eg char
    > if(!std::cin)
    > {
    > std::cout<<"Oouch, you have not listend me... I'm dying....\n";
    > std::cin.clear(); /// I want just to make another turn and not
    > cycle for eternity

    // you need to do a sync
    std::cin.sync();
    > }
    > std::cout<<a<<"\n";
    > }
    > return 0;
    > }
    > //**************************************
    >
    > cycles forever if i input some char instead of numbers. Fast look throw
    > books have not showed me any solution for this but inputing into some
    > string and then parsing. All i want is just resurrect cin to make
    > another turn, is it possible?
    >
    > --
    > float_dublin
     
    Anand, Sep 14, 2005
    #2
    1. Advertising

  3. float_dublin wrote:
    >
    > Hi All,
    > I just have found out (for myself) that the program:
    >
    > //**************************************
    > #include <iostream>
    >
    > int main()
    > {
    > int a=0;
    > while(true)
    > {
    > std::cout << "Input a NUMBER!!!\n" << std::endl;
    > std::cin>>a; /// Here I input "a" eg char
    > if(!std::cin)
    > {
    > std::cout<<"Oouch, you have not listend me... I'm dying....\n";
    > std::cin.clear(); /// I want just to make another turn and not
    > cycle for eternity
    > }
    > std::cout<<a<<"\n";
    > }
    > return 0;
    > }
    > //**************************************
    >
    > cycles forever if i input some char instead of numbers. Fast look throw
    > books have not showed me any solution for this but inputing into some
    > string and then parsing. All i want is just resurrect cin to make
    > another turn, is it possible?


    Yes, it is.
    If you input eg. the character a, then cin >> a recognizes that it can't
    use this character for constructing a number. This the conversion stops
    and since no usable character for forming a number was entered the input
    operation does 2 things: it stops the conversion with an error. it feeds
    back that character to the input stream to be processed later (probably
    by another >> operation). So when the loop turns over the next time, the
    >> operation sees that very same character again and again and again.

    -> You need to get rid of that input. Look up stream member function ignore().

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Sep 14, 2005
    #3
  4. Anand wrote:
    >
    > > if(!std::cin)
    > > {
    > > std::cout<<"Oouch, you have not listend me... I'm dying....\n";
    > > std::cin.clear(); /// I want just to make another turn and not
    > > cycle for eternity

    > // you need to do a sync
    > std::cin.sync();


    sync() has nothing to do with the problem.
    The OP needs to get rid of the erronous input. ignore() does this.


    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Sep 14, 2005
    #4
  5. float_dublin

    float_dublin Guest

    Karl Heinz Buchegger wrote:
    > Yes, it is.
    > If you input eg. the character a, then cin >> a recognizes that it can't
    > use this character for constructing a number. This the conversion stops
    > and since no usable character for forming a number was entered the input
    > operation does 2 things: it stops the conversion with an error. it feeds
    > back that character to the input stream to be processed later (probably
    > by another >> operation). So when the loop turns over the next time, the
    >
    > >>operation sees that very same character again and again and again.

    >
    > -> You need to get rid of that input. Look up stream member function ignore().
    >


    I did that:
    #include <iostream>

    int main()
    {
    int a=0;
    while(true)
    {
    std::cout << "Input a NUMBER!!!\n" << std::endl;
    std::cin>>a; /// Here i input "a" eg char
    std::cin.ignore();
    std::cout<<a<<"\n";
    }
    return 0;
    }

    but it still loooops.
    PS I've tried from std::cin.ignore(); up to std::cin.ignore(25); just
    to be certain :)
    And by the way i've done it before starting topic - eg clear & ignore
    mixture don't works for me. Any qlues?
     
    float_dublin, Sep 14, 2005
    #5
  6. float_dublin wrote:
    > Karl Heinz Buchegger wrote:
    >
    >> Yes, it is.
    >> If you input eg. the character a, then cin >> a recognizes that it can't
    >> use this character for constructing a number. This the conversion stops
    >> and since no usable character for forming a number was entered the input
    >> operation does 2 things: it stops the conversion with an error. it feeds
    >> back that character to the input stream to be processed later (probably
    >> by another >> operation). So when the loop turns over the next time, the
    >>
    >> >>operation sees that very same character again and again and again.

    >>
    >> -> You need to get rid of that input. Look up stream member function
    >> ignore().
    >>

    >
    > I did that:
    > #include <iostream>
    >
    > int main()
    > {
    > int a=0;
    > while(true)
    > {
    > std::cout << "Input a NUMBER!!!\n" << std::endl;
    > std::cin>>a; /// Here i input "a" eg char
    > std::cin.ignore();
    > std::cout<<a<<"\n";
    > }
    > return 0;
    > }
    >
    > but it still loooops.
    > PS I've tried from std::cin.ignore(); up to std::cin.ignore(25); just
    > to be certain :)
    > And by the way i've done it before starting topic - eg clear & ignore
    > mixture don't works for me. Any qlues?


    clear

    followed by

    ignore(some_big_number);

    if that doesn't work for you your compiler is broken.

    John
     
    John Harrison, Sep 14, 2005
    #6
  7. float_dublin

    float_dublin Guest

    John Harrison wrote:
    >
    > clear
    >
    > followed by
    >
    > ignore(some_big_number);
    >
    > if that doesn't work for you your compiler is broken.
    >
    > John

    thanks it finally works :) and I am happy.

    --
    float_dublin
     
    float_dublin, Sep 14, 2005
    #7
  8. float_dublin

    float_dublin Guest

    > followed by
    >
    > ignore(some_big_number);
    >
    > if that doesn't work for you your compiler is broken.
    >
    > John

    hmm not so good as i expected. If I ignore(8) - cin waits 8 characters
    and only after this my happy int is recognized.
    sample:

    Input a NUMBER!!!
    aaaaa
    Oouch, you have not listend me... I'm dying....
    1
    0 /*<- here 1 is ignored as well as aaaaa*/
    Input a NUMBER!!!
    1
    1
    Input a NUMBER!!!

    this simple input makes me mad. My prog is 10 lines, and i'm forced to
    string input with parsing (eg stringstreams) :(
    All I want is int input ]:(
    PS I've tried playing with seekg(n,ios::end) and gcount, but it didn't
    work.

    PPS code:

    #include <iostream>

    int main()
    {
    int a=0;
    while(true)
    {
    std::cout << "Input a NUMBER!!!\n" << std::endl;
    std::cin>>a;
    if(!std::cin)
    {
    std::cout<<"Oouch, you have not listend me... I'm dying....\n";
    std::cin.clear();
    std::cin.ignore(8);
    }
    std::cout<<a<<"\n";
    }
    return 0;
    }
     
    float_dublin, Sep 15, 2005
    #8
  9. float_dublin

    Anand Guest

    float_dublin wrote:
    [..]

    > #include <iostream>
    >
    > int main()
    > {
    > int a=0;
    > while(true)
    > {
    > std::cout << "Input a NUMBER!!!\n" << std::endl;
    > std::cin>>a;
    > if(!std::cin)
    > {
    > std::cout<<"Oouch, you have not listend me... I'm dying....\n";
    > std::cin.clear();
    > std::cin.ignore(8);

    // the following works for me
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
    // Also the previously said std::cin.sync() worked for me !!!!
    // need to figure out why???

    > }
    > std::cout<<a<<"\n";
    > }
    > return 0;
    > }
     
    Anand, Sep 15, 2005
    #9
  10. float_dublin

    Anand Guest

    float_dublin wrote:
    >> followed by
    >>
    >> ignore(some_big_number);
    >>
    >> if that doesn't work for you your compiler is broken.
    >>
    >> John

    >
    > hmm not so good as i expected. If I ignore(8) - cin waits 8 characters
    > and only after this my happy int is recognized.
    > sample:
    >
    > Input a NUMBER!!!
    > aaaaa
    > Oouch, you have not listend me... I'm dying....
    > 1
    > 0 /*<- here 1 is ignored as well as aaaaa*/
    > Input a NUMBER!!!
    > 1
    > 1
    > Input a NUMBER!!!
    >
    > this simple input makes me mad. My prog is 10 lines, and i'm forced to
    > string input with parsing (eg stringstreams) :(
    > All I want is int input ]:(
    > PS I've tried playing with seekg(n,ios::end) and gcount, but it didn't
    > work.
    >
    > PPS code:
    >
    > #include <iostream>
    >
    > int main()
    > {
    > int a=0;
    > while(true)
    > {
    > std::cout << "Input a NUMBER!!!\n" << std::endl;
    > std::cin>>a;
    > if(!std::cin)
    > {
    > std::cout<<"Oouch, you have not listend me... I'm dying....\n";
    > std::cin.clear();
    > std::cin.ignore(8);

    /* try giving a limit to ignore...
    ignore X number of characters until '\n'
    */
    std::cin.ignore(big_number, '\n');
    > }
    > std::cout<<a<<"\n";
    > }
    > return 0;
    > }
     
    Anand, Sep 15, 2005
    #10
  11. float_dublin wrote:
    >
    > hmm not so good as i expected. If I ignore(8) - cin waits 8 characters
    > and only after this my happy int is recognized.


    :)
    You have not read the documentation if ignore()

    ignore() can have 2 arguments:
    1. the number of characters to ignore
    2. a special character. When this character appears in the
    input stream, ignore() stops ignoring further characters.

    In your situation a
    std::cin::ignore( MAX_INT, '\n' );

    would be what you want.
    You want to ignore everything up and including a '\n' character.
    That is: from this point on everything up to the end of line the user
    has input.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Sep 15, 2005
    #11
  12. float_dublin

    float_dublin Guest

    Thanks everybody.
    If anyone is interested I'm happy with this :)

    #include <iostream>
    #include <stdexcept>

    struct __resurrect_cin
    {
    __resurrect_cin(){}
    /**
    \brief Resurrect std::cin only, never touch other istreams
    */
    static void
    resurrect()
    {
    if(!std::cin)
    {
    std::cin.clear();
    std::cin.ignore(4096,'\n');
    throw(std::runtime_error("Wrong input.\n"));
    }
    }
    };

    const __resurrect_cin resurrect_cin;

    std::istream&
    operator>>(std::istream& in, const __resurrect_cin&)
    {
    __resurrect_cin::resurrect();
    return in;
    }

    int
    main()
    {
    int a=0;
    while(true)
    {
    std::cout << "Input a NUMBER!!!\n" << std::endl;
    try
    {
    std::cin>>a>>resurrect_cin;
    }
    catch(std::runtime_error& a)
    {
    std::cout<<a.what();
    }
    std::cout<<a<<"\n";
    }
    return 0;
    }

    /**
    Output:

    Input a NUMBER!!!

    a
    Wrong input.
    0
    Input a NUMBER!!!

    aaaa
    Wrong input.
    0
    Input a NUMBER!!!

    1
    1
    Input a NUMBER!!!

    */
     
    float_dublin, Sep 16, 2005
    #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. TaiwanNoWhere

    cin and cin.clear() problem

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

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

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

    cin and cin.getline()

    Aleander, Mar 6, 2005, in forum: C++
    Replies:
    5
    Views:
    8,746
    Alex Vinokur
    Mar 6, 2005
  4. Josef 'Jupp' SCHUGT

    Resurrecting German mailing list?

    Josef 'Jupp' SCHUGT, Oct 13, 2003, in forum: Ruby
    Replies:
    7
    Views:
    154
    Josef 'Jupp' Schugt
    Oct 21, 2003
  5. Martin DeMello

    Resurrecting the Ruby Wiki

    Martin DeMello, Feb 8, 2008, in forum: Ruby
    Replies:
    1
    Views:
    124
    Tomas Pospisek's Mailing Lists
    Feb 8, 2008
Loading...

Share This Page