Thread: do while question

  1. #1
    Unregistered
    Guest

    Angry do while question

    Hello,

    This is a simple program where I ask the user to enter a number. If the user accidentally enters a letter, the program should ask him to re-enter a number. When I run this program with a letter as input, it goes into an infinite loop and stops checking for input!


    int main(int argc, char* argv[])
    {
    int num;
    bool test(void);

    do {
    cout<<"Enter a number:\n";
    cin>>num;
    } while (!test());

    return 0;
    }

    bool test(void)
    {
    if(!cin.good())
    {cout<<"Bad input\n";
    return false;
    }
    else
    return true;
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    Here's a KB article exlaining how to clear and istream object.


    HOWTO: Clear an istream Object During Extraction
    Last reviewed: October 3, 1997
    Article ID: Q132422
    The information in this article applies to:
    The C Run-Time (CRT) included with: - Microsoft C/C++ version 7.0 - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, 1.52 - Microsoft Visual C++, 32-bit Edition, versions 1.0, 2.0, 2.1, 4.0, 5.0


    SUMMARY
    Testing for errors during extraction is important. For example:


    int n = 0;
    while (n <= 100) {cin >> n;}


    This program is expecting a value greater than 100. If the user inputs a non-numeric value, the stream's fail bit is set, and the cin object becomes unusable. All subsequent extractions result in an immediate return with no value stored. Consequently, the program hangs (stops responding) in the while loop.


    MORE INFORMATION
    The clear() member function clears the fail bit. However, the istream object is still unusable. The sample code below clears the fail bit and extracts the unusable characters left in the streambuf object.



    Sample Code
    Code:
       /* No special compile options needed. */
    
      #include <iostream.h>
    
       int ClearError(istream& isIn)        // Clears istream object
       {
          streambuf*  sbpThis;
          char        szTempBuf[20];
          int         nCount, nRet = isIn.rdstate();
    
          if  (nRet)                        // Any errors?
          {
              isIn.clear();                 // Clear error flags
              sbpThis = isIn.rdbuf();       // Get streambuf pointer
              nCount = sbpThis->in_avail(); // Number of characters in buffer
    
              while (nCount)                // Extract them to szTempBuf
              {
                  if  (nCount > 20)
                  {
                      sbpThis->sgetn(szTempBuf, 20);
                      nCount -= 20;
                  }
                  else
                  {
                      sbpThis->sgetn(szTempBuf, nCount);
                      nCount = 0;
                  }
              }
          }
    
          return  nRet;
       }
    
       void main()
       {
          int  n = 0, nState;
          while (n <= 100)
          {
             cout << "Please enter an integer greater than 100.\n";
             cin >> n;
             nState = ClearError(cin);   // Clears any errors in cin
          }
       }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM