Thread: while runs foreverly

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    56

    while runs foreverly

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    string oddOrEven(int);
    
    int main()
    {
    
    int x;
    
    while(1 == 1)
    {
        cout<<"Enter a number: ";
        cin>>x;
        if(x==0)
        {
            cout<<"You've chosen to quit. BYE."<<endl;
            cin.get();
            cin.get();
            break;
        }
        cout<<"Number is "<<oddOrEven(x)<<endl<<endl;
        cin.get();
    }
    
    }
    
    string oddOrEven(int n)
    {
        return ((n % 2) == 0) ? "even" : "odd";
    }
    The code works okay, but if the user input a character instead of a number when asked, the program runs the while structure foreverly, how can i fix this?
    btw, any tips on making the code cleaner are welcome

  2. #2
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    Of course it will run forever. You test for "1 == 1" which is always true. 1 always will equal 1.

  3. #3
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    This loops until the user inputs zero:

    Code:
    #include <iostream>
    #include <stdbool.h>
    using namespace std;
    
    bool isodd(int x){
        return ( x % 2 ) != 0;
    }
    
    int main(){
        
        int x;
        
        cin >> x;
    
        while(x){
            if(isodd(x))
                cout << "Odd" << endl;
            else
                cout << "Even" << endl;
            cin >> x;
        }
    
        return 0;
    
    }

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    56
    Of course it will run forever. You test for "1 == 1" which is always true. 1 always will equal 1.
    no thats not the problem. You see, it's meant to run that way until the user inputs 0, then the program will close. If you type a number it will display wheter the number is odd or even. So far so good. But if the user type a character instead of a number, the program will run a very annoying loop.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > how can i fix this?
    By testing the return result of the cin operation to see if it was successful.

    Say
    if ( cin >> x )

    If true, you do something
    If false, something else, say clean up the input stream of junk data
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-24-2010, 01:35 PM
  2. How to make a program that runs in the process
    By Makoy in forum Windows Programming
    Replies: 12
    Last Post: 12-20-2004, 10:19 AM
  3. windows installer for psp runs for vs6
    By iain in forum Tech Board
    Replies: 1
    Last Post: 06-27-2004, 09:21 AM
  4. Replies: 6
    Last Post: 10-24-2002, 08:58 AM
  5. DirectX code runs on my machine, but not another
    By VirtualAce in forum Game Programming
    Replies: 2
    Last Post: 06-16-2002, 02:08 PM