-3

I'm using a Yes/No user prompt to determine whether the user wants to go through the program or exit the program...when you type y or Y it will go through the program again. However, any other character, not just n or N, will discontinue the program. I was wondering how I could fix this?

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, Y,nN作为使用的输入来做出决定。

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

此外......您可能需要更换

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

我认为问题在于您getchar()在程序中使用了两个。.

我们不知道错误。您仍然可以尝试在程序中getchar()立即删除printf

于 2013-02-01T19:45:08.963 回答
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 回答