나는이 프로그램을 작성하여 intro to comp sci 클래스로 작성했습니다. 모두 그것이 while 루프를 사용하는 데모입니다. 교수님은이 프로그램에 무한 루프가 있다고 표시했지만, -1이 입력되면 종료되는 무한 루프라는 것을 이해하지 못합니다.무한 루프는 어떻게 발생합니까?
int main() {
int accountNum, balance, totalOfItems, totalOfCredits, creditLimit; //Integers for the user to input
while (accountNum != -1)
{
cout << "Enter account number" << endl;
cin >> accountNum;
cout << "Enter balance" << endl;
cin >> balance;
cout << "Enter total of all items charged this month" << endl;
cin >> totalOfItems;
cout << "Enter total of all credits applied to the account this month" << endl;
cin >> totalOfCredits;
cout << "Enter credit limit" << endl;
cin >> creditLimit;
int newBalance = balance + totalOfItems - totalOfCredits;
if (newBalance > creditLimit)
{
cout << "Account Number: " << accountNum << "\nCredit Limit: " << creditLimit << "\nNew Balance " << newBalance << endl;
cout << "Credit limit exceeded." << endl;
} else {
cout << "New balance : " << newBalance << endl;
}
}
return 0;
}
교수님 께 물어 보셨습니까? – kfsone
내가 뭔가를 놓치지 않는 한, 나는 당신이 옳다고 생각합니다. 이 루프는 종료 될 수 있습니다. – Carcigenicate
이러한 문제를 해결하는 올바른 도구는 디버거입니다. 스택 오버플로를 묻기 전에 코드를 단계별로 실행해야합니다. 자세한 도움말은 [작은 프로그램 디버깅 방법 (Eric Lippert 작성)] (https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)을 참조하십시오. 문제를 재현하는 [최소, 완료 및 확인 가능] (http://stackoverflow.com/help/mcve) 예제와 함께 해당 질문을 \ [편집]해야합니다. 디버거. –