c ++ делать, пока цикл бесконечности - PullRequest
0 голосов
/ 25 июня 2018

Когда я пытаюсь использовать do while, моя программа ниже попадет в бесконечный цикл, если я введу символ во втором раунде.Я не знаю почему, не могли бы вы объяснить это мне?

Я запустил его на

Visual Studio версия 11.0.61219.00 обновление 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;   
}

Вход

10
y

Выход

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##### 

1 Ответ

0 голосов
/ 25 июня 2018
#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;

}

Попробуй.

Я не знаю точно, в чем ваша ошибка. Вероятно, это потому, что вы пытались присвоить переменную не-int для int, что привело к переполнению памяти. Итак, проверьте номер ввода перед выполнением других задач

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...