Help! what's wrong with this program

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bbmmzz

    Help! what's wrong with this program

    Here is my program:
    int main()
    {
    int ival;

    while(cin >ival, !cin.eof())
    {
    if(cin.bad())
    return 0;
    if(cin.fail())
    {
    cerr << "retry!" << endl;
    cin.clear();
    continue;
    }
    cout << ival << endl;
    }
    return 0;
    }

    Input int number, the program work well. But if input char, example
    'a', it display "retry!" endless.
    "cin >ival" is skip after input 'a'? Why?
  • Obnoxious User

    #2
    Re: Help! what's wrong with this program

    On Mon, 06 Oct 2008 08:25:06 -0700, bbmmzz wrote:
    Here is my program:
    int main()
    {
    int ival;
    >
    while(cin >ival, !cin.eof())
    {
    if(cin.bad())
    return 0;
    if(cin.fail())
    {
    cerr << "retry!" << endl;
    cin.clear();
    continue;
    }
    cout << ival << endl;
    }
    return 0;
    }
    >
    Input int number, the program work well. But if input char, example 'a',
    it display "retry!" endless.
    "cin >ival" is skip after input 'a'? Why?
    Because the 'a' is still in the buffer of std::cin. You need to
    remove it, otherwise it will try to read it again and again and
    always fail.

    See std::istream::i gnore().

    --
    OU
    Remember 18th of June 2008, Democracy died that afternoon.

    Comment

    • asm23

      #3
      Re: Help! what's wrong with this program

      bbmmzz wrote:
      Here is my program:
      int main()
      {
      int ival;
      >
      while(cin >ival, !cin.eof())
      {
      if(cin.bad())
      return 0;
      if(cin.fail())
      {
      cerr << "retry!" << endl;
      cin.clear();
      continue;
      }
      cout << ival << endl;
      }
      return 0;
      }
      >
      Input int number, the program work well. But if input char, example
      'a', it display "retry!" endless.
      "cin >ival" is skip after input 'a'? Why?
      int main()
      {
      int ival;

      while(cin >ival&&!cin.eof ()) //CHANGE HERE!
      {
      if(cin.bad())
      return 0;
      if(cin.fail())
      {
      cerr << "retry!" << endl;
      cin.clear();
      continue;
      }
      cout << ival << endl;
      }
      return 0;
      }

      This will work OK! But I can't explained it why...

      Comment

      • bbmmzz

        #4
        Re: Help! what's wrong with this program

        On 10ÔÂ7ÈÕ, ÉÏÎç12ʱ11·Ö, asm23 <asmwarr...@gma il.comwrote:
        bbmmzz wrote:
        Here is my program:
        int main()
        {
        int ival;
        >
        while(cin >ival, !cin.eof())
        {
        if(cin.bad())
        return 0;
        if(cin.fail())
        {
        cerr << "retry!" << endl;
        cin.clear();
        continue;
        }
        cout << ival << endl;
        }
        return 0;
        }
        >
        Input int number, the program work well. But if input char, example
        'a', it display "retry!" endless.
        "cin >ival" is skip after input 'a'? Why?
        >
        int main()
        {
        int ival;
        >
        while(cin >ival&&!cin.eof ()) //CHANGE HERE!
        {
        if(cin.bad())
        return 0;
        if(cin.fail())
        {
        cerr << "retry!" << endl;
        cin.clear();
        continue;
        }
        cout << ival << endl;
        }
        return 0;
        >
        }
        >
        This will work OK! But I can't explained it why...- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
        >
        - ÏÔʾÒýÓõÄÎÄ×Ö -
        Thanks for your suggestion. Your change make the program check the
        state of cin after "cin >ival", and if fail it will exit while loop
        and never let user retry. In my original program, the value of while
        condition is dependon the expression "!cin.eof() ".

        I follow Obnoxious User's suggestion and add "cin.ignore ()" after
        "cin.clear( )", it works.

        Comment

        • James Kanze

          #5
          Re: Help! what's wrong with this program

          On Oct 6, 5:25 pm, bbmmzz <bumanz...@gmai l.comwrote:
          Here is my program:
          int main()
          {
          int ival;
          while(cin >ival, !cin.eof())
          Could you please explain what this is supposed to do?
          ios::eof() really doesn't have any usable meaning until you know
          that an input has failed. It can return true even if the input
          succeeds.

          The standard idiom for a loop would be:

          while ( std::cin >ival ) ...

          If you really want to put the error handling in the loop (which
          I'm not sure is a good idea), you can do something like:

          while ( std::cin >ival || ! std::cin.eof() ) ...

          (Note that in this case, you will only look at eof if the input
          fails.)
          {
          if(cin.bad())
          return 0;
          if(cin.fail())
          {
          cerr << "retry!" << endl;
          cin.clear();
          continue;
          }
          cout << ival << endl;
          }
          return 0;
          }
          Input int number, the program work well. But if input char, example
          'a', it display "retry!" endless.
          "cin >ival" is skip after input 'a'? Why?
          What did you do with the 'a'? I don't see where you extracted
          it.

          (Also, returning from the middle of a loop or using continue is
          generally a sign of a poor program.)

          --
          James Kanze (GABI Software) email:james.kan ze@gmail.com
          Conseils en informatique orientée objet/
          Beratung in objektorientier ter Datenverarbeitu ng
          9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

          Comment

          • Hendrik Schober

            #6
            Re: Help! what's wrong with this program

            James Kanze wrote:
            [...]
            (Also, returning from the middle of a loop or using continue is
            generally a sign of a poor program.)
            While I can't remember having ever used 'continue', I
            have no objections at all against returning from the
            middle of a loop.

            Schobi

            Comment

            • Obnoxious User

              #7
              Re: Help! what's wrong with this program

              On Tue, 07 Oct 2008 04:26:31 -0700, James Kanze wrote:
              (Also, returning from the middle of a loop or using continue is
              generally a sign of a poor program.)
              Of course, those dreaded continues. Wasn't there a
              letter called "Continue statement considered harmful" ;)

              --
              OU
              Remember 18th of June 2008, Democracy died that afternoon.

              Comment

              Working...