「有効な」文字のセットに対して解析する一連の文字列を一緒に取得しようとしています。文字列がすべて「有効な」セットの 1 つである場合、コードは続行する必要があります。ただし、文字列に有効なセット以外の文字が含まれている場合は、エラーが返され、再入力が求められ、有効性が再度チェックされます。
チェックを実行するための 2 つの異なるコード セットを考え出しました。「推測」は入力文字列で、A、a、B、b、C、c、D、および d は使用可能な文字です。コードの最初のセットは、最初は正しく実行されたように見え、その後は何でも受け入れます。2 番目のセットは、ループに入った後、単一の有効な文字入力のみを受け入れます。調べてみると、問題は論理ステートメントに何らかの形で根ざしているようです。とにかく、どんな支援もいただければ幸いです。
コード #1:
int main (){
using namespace std;
string guess;
cout << "please enter your multiple choice answers: ";
cin >> guess;
bool nogood = true;
int i = 0;
while (nogood==true){
if (guess[i]== ('A'||'a'||'B'||'b'||'C'||'c'||'D'||'d')){
i++;
}
else{
cout << "That is not a valid choice please try again: ";
cin.clear();
cin >> guess;
i=0;
}
if (i=guess.length()-1){
nogood = false;
}
else{
nogood = true;
}
}
...code goes on
コード #2:
int main (){
using namespace std;
string guess;
cout << "please enter your multiple choice answers: ";
cin >> guess;
for (int i =0; i < guess.length(); i++){
if (guess[i] == ('A'||'a'||'B'||'b'||'C'||'c'||'D'||'d')){
}
else{
cout << "That is not a valid choice please try again: ";
cin.clear();
cin >> guess;
i=0;
}
}
...code goes on