프로그래밍 세계에 새로운 사람이 있습니다. 모든 입력을 문자열로 읽어야한다는 것을 알고 있습니다. 그러나 이것은 단순한 프로그램이며, 생각하지만 여기 있습니다.하지만 여기 있습니다. 내가 무한 루프를 얻는 방법은 내가 그 여기에 총 보이는 때문에 동안 루프 응축 것입니다 종료 q를 눌러 왜 내 질문에 ...... 내가 지금까지왜이 무한 루프가 있습니까?
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int grade;
char quit = 'a';
cout << "Input your grade (0-100): ";
cout << endl;
cin >> grade;
while (grade < 0) {
cout << "If you have a negetive grade....drop out! otherwise enter another grade" << endl;
cin >> grade;
}
while (quit != 'q') {
while (grade < 0) {
cout << "If you have a negetive grade....drop out! otherwise enter another grade" << endl;
cin >> grade;
}
if (grade == 100) {
cout << "You got a perfect grade!" << endl;
cout << "Letter grade: A" << endl;
}
else if (grade >= 90 && grade <= 100) {
cout << "Letter grade: A" << endl << endl;
}
else if (grade >= 80 && grade <= 89) {
cout << "Letter grade: B" << endl << endl;
}
else if (grade >= 70 && grade <= 79) {
cout << "Letter grade: C" << endl << endl;
}
else if (grade >= 60 && grade <= 69) {
cout << "Letter grade: D" << endl << endl;
}
else if (grade < 60) {
cout << "Letter grade: F" << endl << endl;
}
else {
cout << "Invalid grade!" << endl;
}
cout << " would you like to enter another grade? or press q to quit" << endl;
cin >> grade;
}
system("pause");
return 0;`enter code here`
}
'quit' 변수를 실제로 변경하지 않은 것 같습니다. –
당신은'quit'을 문자 'q'와 같지 않은 것으로 테스트합니다. 그러나 변수를 상단에 명시 적으로 'a'로 설정하고 결코 업데이트하지 마십시오. 두 번째 while 조건을 while (quit! = grade)'로 변경하고'quit'를 'q'로 초기화하는 것이 좋습니다. 가장 아름다운 접근 방식과는 거리가 있지만 작동해야합니다! –