Когда я пытаюсь использовать 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#####