infinite loop !

This is a discussion on infinite loop ! within the C++ Programming forums, part of the General Programming Boards category; Hi, guys~ I just want to user input a number and then decide whether continue or not, but I met ...

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Exclamation infinite loop !

    Hi, guys~

    I just want to user input a number and then decide whether continue or not, but I met a infinite loop when testing it. my code is right below:

    Code:
    #include <iostream>
    
    void main ()
    {
        int x;
        while(1)
        {
            cout << "input a number: ";
            cin >> x;
            if (number==8)
            {
                 break;
            }
        }
        //if break then go to next step;
    }
    it works fine when input a simply number, but that mess comes when your input a string like "relrejlre" etc. How to calm it down ???
    Never end on learning~

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    17
    look at the "atoi" (alpha to integer) function, an integer cannot handle input other than a number, and will crash, whereas if you use atoi ( stdio.h or stdlib.h i think ) it will convert to 0.

    I think that is what you mean.
    Scorpion-ice
    Imperium et Respectus

  3. #3
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    i ran nito the same problem as you. Good coding practice would be to put the loop in a try/catch block...but my program was for a buddy at work, so i left him an "easter egg" so to speak.
    Code:
    while (int theNumber<24&&int infiniteLoopCount<20)
    {
    std::cout<<"enter a number between 1 and 24"<<endl;
    std::cin>>theNumber;
    infiniteLoopCount++;
    }
    if (infiniteLoopCount<=10)
    {
    std::cout<<"Oh no! You've been caught in the INFINITE LOOP OF DEATH!!!\nNext time enter a number, have a nice day."<<endl;
    //delete pointers and junk
    exit(1);
    }
    now like i said, that's not exactly good coding practice, but the guy i made it for wasn't very computer savvy, and the look on his face was priceless . But if it's a formal app just go with the try/catch block....if you really want you can model it after the above while loop, then use a goto command to repeat it.
    PHP and XML
    Let's talk about SAX

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    #include <iostream.h>
    
    int main(void)
    {
        int x;
        while (1)
        {
            cout << "input a number: ";
            cin >> x;
            if (!cin.good())
            {
                cin.clear();
                cin.ignore(10, '\n');
            }
            else if (x == 8) break;
        }   
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    This is better, IMHO:

    Code:
    #include <iostream.h>
    
    int main(void)
    {
        int x;
        while (1)
        {
            cout << "input a number: ";
            cin >> x;
            if (!cin.good())
            {
                //Clear all error flags
                cin.clear();
                //Remove all characters waiting to be read
                while (cin.rdbuf()->in_avail() > 0) cin.get();
            }
            else if (x == 8) break;
        }   
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Originally posted by Hammer
    Code:
    #include <iostream.h>
    
    int main(void)
    {
        int x;
        while (1)
        {
            cout << "input a number: ";
            cin >> x;
            if (!cin.good())
            {
                cin.clear();
                cin.ignore(10, '\n');
            }
            else if (x == 8) break;
        }   
    }
    thanx boys and girls~

    I just found cin.get and cin.ignore in my book, what about cin.good and cin.clear please £¿
    Never end on learning~

  7. #7
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    cin.good is what it says it is, if the input stream is good.
    cin.clear does what it says, clears the input stream

  8. #8
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    it seems cin.clear and cin.good didnt works fine, what's the matter ? here is my code:

    Code:
    #include <iostream>
    
    int i, j;
    
    void main()
    {
        while(1)
        {
        	cout << "please input a number: ";
            cin >> i;
            if (!cin.good())
            {
            	cin.clear();
            }
            else
            {
            	if(i==8)
             	{
              		cout << "good boy!";
              		break;
              	}
           }
        }
    }
    Never end on learning~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 03:45 AM
  3. Infinite Loop with GetAsyncKeyState
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2008, 12:09 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  5. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21