-3

はい/いいえのユーザープロンプトを使用して、ユーザーがプログラムを実行するか、プログラムを終了するかを決定しています... y または Y と入力すると、プログラムが再び実行されます。ただし、n または N だけでなく、他の文字はプログラムを中止します。どうすればこれを修正できるのだろうかと思っていました。

int main() {
    unsigned num;
    char response;

    do {
        printf("Please enter a positive integer greater than 1 and less than 2000: ");
        scanf("%d", & num);
        if(num > 1 && num < 2000) {
            printf("All the prime factors of %d are given below: \n", num);
            printPrimeFactors(num);
            printf("\n\nThe distinct prime factors of %d are given below: \n", num);
            printDistinctPrimeFactors(num);
        } else {
            printf("Sorry that number does not fall within the given range.\n");
        }
        printf("\n\nDo you want to try another number? Say Y(es) or N(o): ");
        getchar();
        response = getchar();
    }
    while (response == 'Y' || response == 'y'); // if response is Y or y then program runs again
        printf("Thank you for using my program. Good Bye!\n\n"); //if not Y or y, program terminates
    return 0;
}
4

5 に答える 5

2

以下のロジックが、使用からの入力として、、、またはのみを取得して決定を下すことを期待yしていることを願っています。YnN

do
{
    ...

    r = getchar();
    if (r == '\n') r = getchar();
    while(r != 'n' && r != 'N' && r != 'y` && r != `Y`)
    {
        printf("\invalid input, enter the choice(y/Y/n/N) again : ");
        r = getchar();
        if (r == '\n') r = getchar();
    }
}while(r == 'Y' || r == 'y');
于 2013-02-01T19:47:45.863 に答える
1

問題は、プログラムで2つ使用しているためだと思いgetchar()ます。。

エラーはわかりません。getchar()それでも、プログラムの直後に削除してみることができprintfます。

于 2013-02-01T19:45:08.963 に答える
1

さらに...交換したい場合があります

getchar();
response = getchar();

と:

char c;
do{
    printf("Do you want to continue? (y/n)");
    scanf(" %c",&c); c = toUpper(c);
}while(c != 'N');

(必須ではありませんが) %c の前のスペースは、空白を削除するためのものです。

于 2013-02-01T19:47:11.417 に答える
1

n または N だけでなく、他の文字はプログラムを中止します。どうすればこれを修正できるのだろうと思っていました

その場合、おそらくテストしたいでしょう:

while(tolower(response) != 'n'));

getchar()改行を捨てるために空があるとしか思えません。これを行うにはもっと良い方法がありますが、この場合は scanf にスペースを追加するだけです:

scanf("%d ", &num);
         ^
于 2013-02-01T19:14:13.927 に答える
0

次の行を次のようgetchar();に置き換えます。fflush(stdin);

これは完全なコードです。

#include <stdio.h>
#include <string.h>

int main(){
    unsigned num;
    char response;

    do {
        printf("Please enter a positive integer greater than 1 and less than 2000: ");
        scanf("%d", &num);
        if (num > 1 && num  < 2000){
            printf("All the prime factors of %d are given below: \n", num);
            printPrimeFactors(num);
            printf("\n\nThe distinct prime factors of %d are given below: \n", num);
            printDistinctPrimeFactors(num);
        }
        else{
            printf("Sorry that number does not fall within the given range.\n");
        }

        fflush(stdin);
        printf("\n\nDo you want to try another number? Say Y(es) or N(o): ");
        response = getchar();
    } while(response == 'Y' || response == 'y');

    printf("Thank you for using my program. Good Bye!\n\n");
    return 0;
}
于 2013-02-01T19:49:21.697 に答える