2013-02-01 40 views
-2

我使用是/否用戶提示來確定用戶是否想要瀏覽程序或退出程序......當您輸入y或Y時,它將再次通過程序。但是,任何其他字符,不僅僅是n或N,都會中斷程序。我想知道如何解決這個問題?是/否C中的提示?

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; 
} 
+0

如果Y使它們繼續,N使它們退出,那麼您希望其他字母做什麼? – Pete

+0

我希望其他字母不能像N一樣停止播放...喜歡「你輸入了非Yes或No以外的內容,請重試」 –

+0

嗯,這不正是你想要的嗎?或者需要修正什麼? – 2013-02-01 19:41:35

回答

1

其他任何字符,而不是僅僅侷限於N或N,將停止該程序。我 想知道我怎麼能解決這個

在這種情況下,你可能想測試:

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

我只能推測空getchar()有扔掉的換行符。有更好的方法來做到這一點,但在這種情況下,你可以簡單地添加一個空格scanf函數:

scanf("%d ", &num); 
     ^
+0

這沒有效果 –

1

我認爲這個問題是因爲您使用的是你的方案二getchar()。 。

我們不知道錯誤。仍然可以嘗試在程序中的printf之後立即刪除getchar()

+0

我也注意到了這一點,但我不知道這是否是問題。 – NlightNFotis

+0

@NlightNFotis第一個是從scanf中刪除輸入緩衝區中留下的換行符。如果輸入符合預期,則按預期工作。 –

0

此外...你可能需要更換

getchar(); 
response = getchar(); 

有:

​​

需要注意的是(雖然不是必要的),提前%C的空間是消除空格

2

我希望你期待下面的邏輯得到只有y,Y,nN作爲輸入從使用tak是一個決定。

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'); 
+0

這很好。唯一的問題是當我輸入一個字符而不是Y/Y時N/n它會無限地說printf語句 –

+0

您期待正確的輸入形式的用戶權然後循環需要完成 – rashok

+0

耶,所以如果他們進入不正確的輸入,因爲你寫了上面的代碼...我怎麼能這樣說,所以它說無效輸入只是一次,而不是無限運行 –

0

替代與fflush(stdin);

工作很適合我讀getchar();行了,這是完整的代碼。

#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; 
} 
+0

'fflush(stdin)'是每個標準未定義的行爲。它可能適用於某些實現,但它當然不是可移植的。 –