2013-07-20 116 views
1

編輯:非常感謝您的詳細回覆 嘗試用C編程。我包括'ctype.h'和'stdio.h'庫。我得到的程序提示用戶輸入'printf'的數字,但是當我嘗試用'scanf'存儲它時,它似乎在程序中的那一點崩潰。有什麼特別的,我應該檢查,因爲我真的只是下載並安裝codeblocks,假設它會工作。請幫忙。Code :: Blocks crashing/Really Confused

這裏是我的代碼:

#include <stdio.h> 
#include <ctype.h> 
main() 
{ 
    int userNumber = 0; 
    int correctNumber = 0; 

    correctNumber = (rand() % 10) + 1; 
    printf("Pick a number between 1 and 10"); 
    scanf("%d", &userNumber); 

    if (isdigit(userNumber)) { 
    if (userNumber == correctNumber) { 
     printf("Yay, you guessed it!"); 
    } 
    else { 
     printf("Wrong Number!!!"); 
    } 
    } 
    else { 
     printf("That is not a number from 1 - 10"); 

    } 
} 
+3

請重新閱讀'isdigit'的[documentation](http://www.cplusplus.com/reference/cctype/isdigit/)。你用錯了。 – abelenky

+0

不是說這會解決問題,但'main'通常被聲明爲'int',並且應該返回一些東西(比如0)。代碼塊是否抱怨缺少返回類型? – Nobilis

+1

''#include ''rand','isdigit(userNumber -1 +'0')' – BLUEPIXY

回答

0

使下面的改變

該項目在海灣合作委員會工作完全正常,使用stdlib.h中蘭德功能

2

isdigit功能檢查是否參數是十進制數字字符
如果你想工作的方式,只投它:

if (userNumber == correctNumber) 

if (isdigit(userNumber + (char)'0')) 

遵循else只執行if括號中的表達式的值爲0字的聲明。
如果returnisdigit功能是true(而不是0),您的代碼的下一行將被執行。
在它看起來像這樣的調試器:

CPU Disasm 
Address Hex dump   Command        Comments 
00401048 |. 68 24504200 push offset t3.00425024    ; /format = "%d" 
0040104D |. E8 E3070000 call t3.scanf      ; \scanf - Read your integer variable and store it to int 
00401052 |. 83C4 08  add esp,8       ; 
00401055 |. 8B45 F8  mov eax,dword ptr [ebp-8]   ; Store userNumber in eax (5 in this case) 
00401058 |. 83C0 30  add eax,30       ; 5 + 0x30 = 0x35 = Character 0, so decimal number is converted to char value 5 
0040105B |. 50   push eax       ; /c => 48., stack it 
0040105C |. E8 E6020000 call t3.isdigit      ; execute isdigit function - if (isdigit(userNumber+(char)'0')) 
00401061 |. 83C4 04  add esp,4       ; adjust stack 
00401064 |. 85C0   test eax,eax      ; isdigit returned result is 0 ? 
00401066 |. 74 37   jz short t3.0040109F    ; if result is NOT 0, next line will be executed 
00401068 |. 8B4D F8  mov ecx,dword ptr [ebp-8]   ; ecx = userNumber 
0040106B |. 3B4D FC  cmp ecx,dword ptr [ebp-4]   ; if (userNumber == correctNumber) 
0040106E |. 75 0F   jne short t3.0040107F    ; if condition is TRUE - statement1 will be executed, otherwise statement2 
00401084 |. E8 22080000 call t3.printf      ; printf("Yay, you guessed it!"); 
.... 
00401081 |. E8 25080000 call t3.printf      ; printf("Wrong Number!!!"); 
..... 
0040109F |. E8 05080000 call t3.printf      ; printf("That is not a number from 1 - 10"); 

如下所示,表達0,並且隨後else總是會被執行如聲明printf("That is not a number from 1 - 10");
您最初的代碼如下所示:

Address Hex dump   Command      Comments 
0040104D |. E8 E3070000 call t3.scanf     ; \scanf 
00401052 |. 83C4 08  add esp,8      ; 
00401055 |. 8B45 F8  mov eax,dword ptr [ebp-8]  ; eax is now 5, but this time the conversion is not made 
00401058 |. 50   push eax      ; /c => 5 
00401059 |. E8 E9020000 call t3.isdigit    ; \isdigit 
..... 
00401061 |. 85C0   test eax,eax     ; isdigit returned 0 this time 
00401063 |. 74 37   jz short t3.0040109C   ; well, jump to last printf 
..... 
0040109C |. E8 05080000 call t3.printf     ; \printf("That is not a number from 1 - 10"); 

如果使用((char) userNumber + '0'),結果將是相同的。只有獲取該值的指令纔會更改爲movsx eax, byte ptr [ebp-8]

-1

功能"int isdigit(int c);"是 「檢查c是否是一個十進制數字符。」

所以改變這樣你的代碼:

scanf("%d",&userNumber) 

==>

scanf("%c",&userNumber); 

它將按預期工作!

補充一點:

if(uerNumber == currentNumber) 

==>

if((userNUmber - '0') ==currentNumber) 

你可以給我的名譽回來?我回答你的問題在凌晨3點,只是有點困

+0

不,這個修正後它不會工作,因爲'correctNumber'仍然是一個int(1-10),而不是char。 – abelenky

+0

@abelenky嗨,我剛剛在一個月前離職,在一家遊戲公司工作。我們使用Linux和C.大部分時間我正在閱讀其他peoper's –

+0

@abelenky大多數時候我只需要讀取其他peoper的代碼並做一些移植的事情。這很慢。所以你想給我一些建議,比如如何改善自己?希望你能提供建議 –

0

相反的:if(isdigit(userNumber))

這樣寫:if((userNumber>0)&&(userNumber<=10))

0
#include <stdio.h> 
#include <ctype.h> 
main() 
{ 
    char userNumber = 0; // isdigit can only be used on char 
    int correctNumber = 0; 

    correctNumber = (rand() % 10) + 1; 
    printf("Pick a number between 1 and 10"); 
    scanf("%c", &userNumber); 

    if (isdigit(userNumber)) { 
     if ((userNumber-'0') == correctNumber) { // Convert userNumber to int 
      printf("Yay, you guessed it!"); 
    } 
    else { 
     printf("Wrong Number!!!"); 
    } 
    } 
    else { 
     printf("That is not a number from 1 - 10"); 
    } 
} 

另外,ISDIGIT()只能用於從0-檢測字符9。在你的代碼中10將不會被正確識別。

0

我添加了一些退出代碼,用於輸入不符合預期的情況以及一些進一步的驗證(除了修復返回類型main)。我還硬編碼了correctNumber的值,以便於測試。

#include <stdio.h> 
#include <ctype.h> 
#include <stdlib.h> 

int main() /* let's fix the signature for main */ 
{ 
    char userNumber [10]; /* as we need to pass a char array let's change the type for userNumber */ 
    int correctNumber = 0; 

    correctNumber = 5;/*(rand() % 10) + 1; - I've hardcoded this just for the sake of testing */ 
    printf("Pick a number between 1 and 10: "); 
    scanf("%s", userNumber); 

    /* I've updated the if a bit to check separately for range and whether input is a valid number */ 

    if (isdigit(userNumber[0])) 
    { 
     int inputtedNumber = atoi(userNumber); /* convert string to int */ 

     if (inputtedNumber <= 10 && inputtedNumber > 0) { 
      if (inputtedNumber == correctNumber) { 
       printf("Yay, you guessed it!\n"); 
       exit(0); /* exit is used to indicate the execution status to the environment (successful or if not how it failed */ 
     ` } 
      else { 
       printf("Wrong Number!!!\n"); 
       exit(1); 
      } 
     } 
     else { 
      printf("You've not inputted a number between 1 and 10.\n"); 
      exit(2); 
     } 
    } 
    else { 
     printf("You've not inputted a valid number.\n"); 
     exit(3); 
    } 
} 

讓我知道如果有什麼不清楚。