プログラミングの世界に新しい人たちがいます。文字列としてすべての入力を読むべきですが、これは単純なプログラムであり、私は思っていますが、なぜこの無限ループを持っているのですか
は、これまでのところ、私は終了し、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'に設定し、それを更新しません。私はあなたの2番目の 'while'条件を' while(quit!= grade) 'に変更し、' quit'を 'q'に初期化することをお勧めします。それは最も美しいアプローチから遠いですが、うまくいくはずです! –