[英]My Do-While loop doesn't seem to loop?
請幫助,當我要求用戶使用scanf和printf輸入繼續輸入時,我似乎無法使do-while循環循環。 可以是擦拭緩沖區嗎? 我似乎無法正確地做到這一點,我只需要完成循環即可,而該循環應該可以正常工作。 編碼新手,此網站請不要太苛刻。 請,謝謝。
#include <stdio.h>
#include <stdlib.h>
void wipe_buffer(void);
int main(int argc, char* argv[])
{
char play1;
char play2;
char cont;
do{
printf("Player one pick Rock, Paper, or Scissors\n");
scanf(" %c", &play1);
wipe_buffer();
printf("Player two pick Rock, Paper, or Scissors\n");
scanf(" %c", &play2);
wipe_buffer();
switch(play1)
{
case 'r':
if(play2 == 'p' || play2 == 'P')
{
printf("Paper Covers Rock\n");
printf("Player two wins\n");
}
if(play2 == 's' || play2 == 'S')
{
printf("Rock Breaks Scissors\n");
printf("Player one wins\n");
}
if(play2 == 'r' || play2 == 'R')
{
printf("Draw, Nobody wins\n");
}
break;
case 'R':
if(play2 == 'p' || play2 == 'P')
{
printf("Paper Covers Rock\n");
printf("Player two wins\n");
}
if(play2 == 's' || play2 == 'S')
{
printf("Rock Breaks Scissors\n");
printf("Player one wins\n");
}
if(play2 == 'r' || play2 == 'R')
{
printf("Draw, Nobody wins\n");
}
break;
case 'P':
if(play2 == 'p' || play2 == 'P')
{
printf("Draw, Nobody wins\n");
}
if(play2 == 's' || play2 == 'S')
{
printf("Scissors cuts Paper\n");
printf("Player two wins\n");
}
if(play2 == 'r' || play2 == 'R')
{
printf("Paper covers rock\n");
}
break;
case 'p':
if(play2 == 'p' || play2 == 'P')
{
printf("Draw, Nobody wins\n");
}
if(play2 == 's' || play2 == 'S')
{
printf("Scissors cuts Paper\n");
printf("Player two wins\n");
}
if(play2 == 'r' || play2 == 'R')
{
printf("Paper covers rock\n");
}
break;
case 's':
if(play2 == 'p' || play2 == 'P')
{
printf("Scissors Cuts Paper\n");
printf("Player one wins\n");
}
if(play2 == 's' || play2 == 'S')
{
printf("Draw, Nobody wins\n");
}
if(play2 == 'r' || play2 == 'R')
{
printf("Rock breaks Scissors\n");
printf("Player two wins\n");
}
break;
case 'S':
if(play2 == 'p' || play2 == 'P')
{
printf("Scissors Cuts Paper\n");
printf("Player one wins\n");
}
if(play2 == 's' || play2 == 'S')
{
printf("Draw, Nobody wins\n");
}
if(play2 == 'r' || play2 == 'R')
{
printf("Rock breaks Scissors\n");
printf("Player two wins\n");
}
break;
}
printf("Do you wish to continue?\n");
scanf(" &c", &cont);
wipe_buffer();
}while(cont == 'y' || cont == 'Y');
}
void wipe_buffer(void)
{
char t;
scanf("%c", &t);
while(t != '\n' )
{
scanf("%c", &t);
}
return;
}
在這個部分:
printf("Do you wish to continue?\n");
scanf(" &c", &cont);
"&c"
應為"%c"
請注意,當我使用gcc進行編譯時,收到的警告基本上可以解決此確切問題,因此請務必閱讀您的警告。
在這里:
printf("Do you wish to continue?\n");
scanf(" &c", &cont);
wipe_buffer();
}while(cont == 'y' || cont == 'Y');
應該有:
scanf(" %c", &cont);
下次遇到某些問題時(如果使用gcc),請使用-Wall -pedantic
選項進行編譯。 它很好地顯示了問題所在,如果沒有,它將縮小范圍。
這就是您所顯示的情況:
test2.c:117:5: warning: too many arguments for format [-Wformat-extra-args]
test2.c:120:1: warning: control reaches end of non-void function [-Wreturn-type]
因此,這也很好地提醒您main()
函數必須return
一些值。
另一個建議是通過以下鏈接進行訪問: http : //www.gidnetwork.com/b-60.html並轉到“步驟”頁面。
簡而言之,這里有一些信息與getchar
相比,如何使用scanf
從stdin
讀取一個char成為耗時和空間消耗方面的信息。
考慮在您的代碼中應用此建議。
因此,查看發生情況的一種快速方法是在循環之前刪除調試printf
語句
scanf(" &c", &cont);
wipe_buffer();
printf("Debug: [%c]\n", cont);
}while(cont == 'y' || cont == 'Y');
然后在程序運行時查找printf
。
我認為正在發生的事情是每個scanf
在處理之前都在尋找Enter擊鍵,因此wipe_buffer
將在繼續之前請求很多Enter擊鍵。 嘗試在不使用wipe_buffer
情況下運行該程序,您仍然需要按Enter才能繼續。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.