首先,我對c完全不熟悉。如果你像對待我一樣的初學者,那就太好了。是否可以使用scanf(「%d」&i)並僅使用第一個輸入的數字,而沒有別的?
所以,我的問題是,我似乎無法做到這一點,使程序只接受一個數字的信息,使用它,然後忽視任何其他信息。
目前,我有一些與此類似:
#include <stdio.h>
#include <string.h>
int main(){
int i, ret;
char c, type;
do
{
printf("Convert ASCII # to character\n");
printf("q: Quit.\n");
scanf("%c", &type);
/* I use the " if(type== 'n'); " a number of times. */
/* I left the others out to simplify what my problem is. */
if(type=='1'){
printf("ASCII NUMBER -> CHAR \n");
printf("\t Please input one ASCII code \n");
int ret = scanf("%d", &i);
/* My aim here is to get the amount of integers the user inputs,*/
/* and use that to categorize, but I think I am failing to do so. */
if(ret==1){
printf("\t The character for ASCII code %d is -> '%c' \n\n", i, i);
break;
}
else{
printf("Please input one number./n/n");
break;
}
}
}
while(type=='q');
return 0;
/* A problem I face a lot is where the program would terminate*/
/* even when the while conditions weren't met. */
}
我希望你能明白我想通過看上面的代碼做。
任何幫助將不勝感激!
對不起,我不是太熟悉使用的getchar()。我該怎麼做才能保持循環,並且沒有在輸入緩衝區中留下回車鍵? – user3536816
@ user3536816使用scanf()讀取整數後,'\ n'將留在輸入緩衝區中。所以只需添加一個'getchar()'語句並嘗試執行程序。 – LearningC