grade2()に関しても、数字しか入力しない場合はバグが生じないのですが、アルファベット(例えばAやB)を入力すると、”エラーです”というメッセージが表示され、それ以降数字の1~4を入力しても、”エラーです”というメッセージが表示され続けてしまいます。なぜなのでしょうか?よろしくお願いします。
#include "stdafx.h"
#include <iostream>
using namespace std;
void grade()
{
int math;
char temp;
do {
cout << "成績(A~D)を入力してください。" << endl;
cout << "math: ";
cin >> temp;
while (getchar() != '\n');
if (temp == 'A')
math = 4;
if (temp == 'B')
math = 3;
if (temp == 'C')
math = 2;
if (temp == 'D')
math = 1;
else
cout << "エラーです。もう一度入力してください。";
} while (temp != 'A' && temp != 'B' && temp != 'C' && temp != 'D');
}
void grade2() {
int math = 1;
cout << "成績(1~4)を入力してください" << endl;
do {
if (math != 1 && math != 2 && math != 3 && math != 4) {
cout << "エラーです。" << endl;
}
cout << "math: ";
cin >> math;
while (getchar() != '\n');
} while (math != 1 && math != 2 && math != 3 && math != 4);
}