0

以下では、ユーザーの入力を Y または y または N または n のみに制限しようとしています。問題が何であるかを指摘できるように、コードに関する私のコメントに従ってください。私はこのフォーラムに非常に慣れていません。プログラミングに情熱を持っています。誰か助けてください。ありがとうございました。while ループ (do-while ループではない) は、私が問題を抱えている部分です。!= を正しく使用していない可能性があります。まだ何も進んでいませんが、今通っているクラスは入門レベルです。

    cout << "Would you like to use this program again?: ",
    cin >> ans;

    if(ans =='Y'||ans =='y'||ans =='N'||ans =='n')
        break;
    else //This is where I'm having problem with.
        while (ans != 'Y'||ans != 'y'||ans !='N'||ans !='n')
        {
            cout << "Please enter Y or y if you like to use the program again and N or n do exit.",
            cin >> ans; //If the question is asked and no matter what I input for ans, the while loop never gets exited. Why? Is there something I didn't use right?
        }
}while (ans == 'Y'||ans =='y');

return 0;
4

1 に答える 1

0

ロジックを処理するためのより良い方法doは、ユーザーが入力するまで yes/no の入力を継続的に求める単一のループを作成することです。

do {
    cout << "Please enter Y or y if you like to use the program again and N or n do exit.",
    cin >> ans;
} while (ans != 'Y' || ans != 'y' || ans !='N' || ans !='n');
于 2016-04-01T11:04:34.003 に答える