CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2006
    Posts
    102

    Inifinite loop problem (easy question)

    Hey all, sorry for all the beginner's questions

    Anyways, I'm trying to build a simple loop that will protect against unwanted input (i.e. when the user enters a characters instead of an integer). Here's what I have so far:

    Code:
    #include <iostream>
    #include <cctype>
    
    using namespace std;
    
    int main()
    {
        
        cout << "Enter a number: ";
        int number;
        cin >> number;
      
        
        while (!isdigit(number))        //Created an infinite loop!
        {cout << endl << "That is not a number, please try again: ";
         cin >> number;}
        
        cout << endl << "Enter a character: ";
        char ch;
        cin >> ch;
        
        cout << endl << number << " " << ch;
        
        int wait;
        cin >> wait;
        return 0;
    }
    First of all, am I correct that (!isdigit(number)) is really saying "Do this while the input is not an integer?"

    Whenever I input a numeric value, say 3, I get the message stating "That is not a number, please try again"??!

    Whenever I input a character value, I get an infinite loop stating" "That is not a number, please try again", and the program ignores the cin >> number within the loop.

    Can anyone tell me whats wrong here?

    Thanks.

  2. #2
    Join Date
    Mar 2009
    Location
    Granada, Spain
    Posts
    40

    Re: Inifinite loop problem (easy question)

    Be careful. 2484 is an integer, but not a digit. I have no time to check it, sorry.

    PD: You can read a char* and then check if num[i] is a digit.

  3. #3
    Join Date
    May 2006
    Posts
    102

    Re: Inifinite loop problem (easy question)

    Hmmm...nevermind, this appears to be a character function. Not an integer function. I'll have to come up with a different way to do this

  4. #4
    Join Date
    Jul 2006
    Posts
    49

    Re: Inifinite loop problem (easy question)

    Hi
    I think you should declare the "number" variable as char. I don know why. In another way, I would like to advise you some method to check the number which is atoi. It return 0 if the input cannot be converted to a value of that type.

    correct me if t wrong
    thx

  5. #5
    Join Date
    May 2006
    Posts
    102

    Re: Inifinite loop problem (easy question)

    Sunton

    Yeah, I think that solution works. I'll just have to remember not to use the variable as an INT later on.

    Thanks

  6. #6
    Join Date
    Dec 2008
    Posts
    91

    Re: Inifinite loop problem (easy question)

    do this:

    Code:
    #include <iostream>
    #include <cctype>
    
    using namespace std;
    
    int main()
    {
    int number;
        while(true)
    {
        cout << "Enter a number: ";
        string numberS;
        cin >> numberS;
        number = atoi(numberS.c_str());
        if (itisanumber(numberS))
           break;
        else
        cout << "\nThat is not a number, please try again: ";
    
    }
        
        cout << endl << "Enter a character: ";
        char ch;
        cin >> ch;
        
        cout << endl << number << " " << ch;
        
        int wait;
        cin >> wait;
        return 0;
    }
    itisanumber() function you write yourself to check if the string is a number.

  7. #7
    Join Date
    May 2006
    Posts
    102

    Re: Inifinite loop problem (easy question)

    Thanks for the help everyone. I was able to find fix to the problem by using cin.clear and cin.get() to clean up my input. Here's a slightly modified version of the program:

    Code:
    #include <iostream>
    #include <cctype>
    
    using namespace std;
    
    int main()
    {
        
        cout << "Enter a number (or enter 77 to exit): ";  //"Lost" reference
        int number=0;
        int sum=0;
        
      
        
        while (number!=77)
    {
         if (!(cin >> number))
         
         {  cout << "That is not a number. Please enter a number: ";
            cin.clear();                                  // Resets cin error flag
            cin.get();                                    // Removes bad input        
         }
         
         else if (number==77)
         break;
         
         else
         
         {    
                 sum+=number;
                 cout << endl << "Enter another number: ";
         }
    }
    
        cout << endl << "Total is: " << sum;
        
        int wait;
        cin >> wait;
        return 0;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured