我在C++中提出了一個程序,要求輸入任何整數。該程序只有2次迭代後崩潰。代碼如下:if語句正在運行時只有一條語句
#include<iostream>
int main()
{
int user_choice;
std::cout <<"Please enter any number other than five: ";
std::cin >> user_choice;
while(user_choice != 5)
{
std::cout <<"Please enter any number other than five: ";
std::cin >> user_choice;
if(user_choice == 5)
std::cout << "Program Crash";
break;
}
std::cout << "I told you not to enter 5!";
return 0;
}
然後我試着這樣做:
if(user_choice == 5)
std::cout << "Program Crash";
//std::cout << "Shutting Down";
哪些工作。爲什麼註釋掉第二行,導致程序運行正常?
'if(condition){statement1;語句2; }' – LogicStuff
@LogicStuff能否請你解釋一下你的評論 –
還有更多的代碼,比如,你爲什麼要比較'user_choice'和'5'文字,你應該做'user_choice == right_answer'?爲什麼你覺得有必要將'right_answer'分配給'user_choice'如果它們已經相等了?你還將'5'硬編碼到輸出消息中...... – LogicStuff