Search code examples
c++loopsinfinity

c++ do while infinity loop


When I try to use do while, my program below will fall into infinity loop if I input a character at second round. I don't know why, could you explain it to me?

I ran it on

Visual studio version 11.0.61219.00 update 5

//============================================================
//Do while infinity loop
//============================================================
#include <iostream>
using namespace std;
int main ()
{   
    int n;
    do{
        cout << "#####Enter the number you need check#####" << endl;
        cin >> n;
        if  (n != 0 && n <= 10) 
        cout << "You entered  " << n <<  endl;
        cout << "Enter 0 if you want to exit loop" << endl;
        if (n > 10)
        {
            cout << "Sorry with the number " << n << " the result is too big that why i can show you"  << endl; 
        }
    }
    while( n != 0); // do chi check n khac' 0 nen khi da chay 1 vong` do while luc này n da duoc gan' gia tri tu` vong` lap truoc -> vong` sau input sai gia tri vi' du character 'y' -> code se khong input duoc gia tri moi' vao ->dung` gia tri cu -> infinity
    {
        cout << "You entered number 0 then bye" << endl;
    }
    system("pause");
    return 0;   
}

Input

10
y

Output

10                                                                                                                                
You entered  10                                                                                                                   
Enter 0 if you want to exit loop                                                                                                  
#####Enter the number you need check#####                                                                                         
y                                                                                                                                 
You entered  10                                                                                                                   
Enter 0 if you want to exit loop
#####Enter the number you need check##### 
10                                                                                                                                
You entered  10                                                                                                                   
Enter 0 if you want to exit loop                                                                                                  
#####Enter the number you need check##### 
10                                                                                                                                
You entered  10                                                                                                                   
Enter 0 if you want to exit loop                                                                                                  
#####Enter the number you need check##### 

Solution

  • #include "stdafx.h"
    #include <iostream>
    
    using namespace std;
    
    //============================================================
    //Do while infinity loop
    //============================================================
    
    
    
    
    int main()
    {
        int n;
        do {
            cout << "#####Enter the number you need check#####" << endl;
    
            cin >> n;
            if (!cin) // or if(cin.fail())
            {
            // user didn't input a number
                cin.clear(); // reset failbit
                cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //skip bad input
                                                                           // next, request user reinput
            }else {
    
                if (n != 0 && n <= 10) {
                    cout << "You entered  " << n << endl;
                }
    
                if (n > 10)
                {
                    cout << "Sorry with the number " << n << " the result is too big that why i can show you" << endl;
                }
    
                cout << "Enter 0 if you want to exit loop" << endl;// day la mot dong cac ban da comment bang tieng viet
    
            }
        }while(n != 0);
    
        {
            cout << "You entered number 0 then bye" << endl;
        }
        system("pause");
        return 0;
    

    }

    Try it.

    I do not know exactly what your error is. It's probably because you tried to assign a non-int variable to an int leading to a memory overflow. So, check the input number before performing other tasks