我只是想知道的東西。我有以下代碼:非終止while循環
#include <iostream>
using namespace std;
int main()
{
int number, largest, counter = 1;
while (counter <= 10)
{
cout << "Enter a number: ";
cin >> number;
if (counter = 1)
{
largest = number;
}
else if (number > largest)
{
largest = number;
}
counter++;
}
cout << "\n\nThe largest number is: " << largest;
system("pause");
return 0;
}
事情是,它永遠不會終止。我確實通過修改代碼來解決問題,但我想知道爲什麼會發生這種情況。這裏是固定的代碼:
#include <iostream>
using namespace std;
int main()
{
int number, largest, counter = 1;
cout << "Enter a number: ";
cin >> number;
largest = number;
while (counter < 10)
{
cout << "Enter a number: ";
cin >> number;
if (number > largest)
{
largest = number;
}
counter++;
}
cout << "\n\nThe largest number is: " << largest << endl;
system("pause");
return 0;
}
似乎刪除else if語句後,它的工作。發生了什麼?
編譯器不會給您提示嗎? –
如果我是編譯器,我會說「不能隱式轉換類型'int'到'bool'」或者「你不應該在'if'中設置一個變量」或者像「哎Emile wazzup爲什麼只用'if'中有一個'='?嘗試'=='。「 – Bastardo
@JohnnyCageWins。我的編譯器(MSVC)說...沿線的東西... –