Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 代码::块崩溃/非常混乱_C_Codeblocks - Fatal编程技术网

C 代码::块崩溃/非常混乱

C 代码::块崩溃/非常混乱,c,codeblocks,C,Codeblocks,编辑:非常感谢您的详细回复 试图用C语言编程,包括“ctype.h”和“stdio.h”库。我让程序用“printf”提示用户输入一个数字,但当我试图用“scanf”存储它时,它似乎在程序中的该点崩溃了。有什么特别的东西我应该检查,因为我真的只是下载和安装了代码块,假设它会工作。请帮忙 这是我的密码: #include <stdio.h> #include <ctype.h> main() { int userNumber = 0; int correctNu

编辑:非常感谢您的详细回复 试图用C语言编程,包括“ctype.h”和“stdio.h”库。我让程序用“printf”提示用户输入一个数字,但当我试图用“scanf”存储它时,它似乎在程序中的该点崩溃了。有什么特别的东西我应该检查,因为我真的只是下载和安装了代码块,假设它会工作。请帮忙

这是我的密码:

#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");

   }
}
#包括
#包括
main()
{
int userNumber=0;
整数=0;
correctNumber=(rand()%10)+1;
printf(“选择1到10之间的数字”);
scanf(“%d”、&userNumber);
if(isdigit(用户编号)){
if(userNumber==correctNumber){
printf(“耶,你猜对了!”);
}
否则{
printf(“错误号码!!!”);
}
}
否则{
printf(“这不是1-10之间的数字”);
}
}

进行以下更改

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

该程序在gcc中运行良好,使用stdlib.h for rand函数进行以下更改

 if (isdigit((char)userNumber+'0'))
该程序在gcc中运行良好,使用stdlib.h for rand函数

函数检查参数是否为十进制数字字符。
如果你想用这种方式工作,只需施展:

if (userNumber == correctNumber)

如果括号中的表达式具有值
0
,则仅执行单词
else
后面的语句

如果
return
isdigit
函数的值为
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(“这不是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");
scanf("%d",&userNumber)
如果使用
((char)userNumber+'0')
,结果将相同。只有获取值的指令才会更改为
movsx eax,字节ptr[ebp-8]

函数检查参数是否为十进制数字字符。
如果你想用这种方式工作,只需施展:

if (userNumber == correctNumber)

如果括号中的表达式具有值
0
,则仅执行单词
else
后面的语句

如果
return
isdigit
函数的值为
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(“这不是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");
scanf("%d",&userNumber)
如果使用
((char)userNumber+'0')
,结果将相同。只有获取该值的指令才会更改为
movsx eax,字节ptr[ebp-8]

函数
“int isdigit(int c);”
is“检查c是否为十进制数字字符。

因此,更改代码如下:

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");
scanf("%d",&userNumber)
==>

它会像预期的那样工作

添加以下内容:

if(uerNumber == currentNumber)
==>

你能把我的名誉还给我吗?我在凌晨3点回答你的问题,有点想睡

函数
“int isdigit(int c);”
是“检查c是否是十进制数字字符。

因此,更改代码如下:

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");
scanf("%d",&userNumber)
==>

它会像预期的那样工作

添加以下内容:

if(uerNumber == currentNumber)
==>

你能把我的名誉还给我吗?我在凌晨3点回答你的问题,只是有点困,而不是:
if(isdigit(userNumber))

写这个:
if((userNumber>0)和&(userNumber而不是:
if(isdigit(userNumber))

写这个:
if((userNumber>0)和&(userNumber
)\include
#包括
main()
{
char userNumber=0;//isdigit只能用于char
整数=0;
correctNumber=(rand()%10)+1;
printf(“选择1到10之间的数字”);
scanf(“%c”、&userNumber);
if(isdigit(用户编号)){
如果((userNumber-'0')==correctNumber){//将userNumber转换为int
printf(“耶,你猜对了!”);
}
否则{
printf(“错误号码!!!”);
}
}
否则{
printf(“这不是1-10之间的数字”);
}
}
另外,isdigit()只能用于检测0-9之间的字符。在代码中,10将无法正确识别。

\include
#包括
main()
{
char userNumber=0;//isdigit只能用于char
整数=0;
correctNumber=(rand()%10)+1;
printf(“选择1到10之间的数字”);
scanf(“%c”、&userNumber);
if(isdigit(用户编号)){
如果((userNumber-'0')==correctNumber){//将userNumber转换为int
printf(“耶,你猜对了!”);
}
否则{
printf(“错误号码!!!”);
}
}
否则{
printf(“这不是1-10之间的数字”);
}
}

另外,isdigit()只能用于检测0-9之间的字符。在您的代码中,10将无法正确识别。

我为输入不符合预期的不同情况添加了一些退出代码,并进行了一些进一步的验证(除了修复
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);
    }
}
#包括
#包括
#包括
int main()/*让我们修复main的签名*/
{
char userNumber[10];/*因为我们需要传递一个char数组,所以让我们更改userNumber的类型*/
整数=0;
correctNumber=5;/*(rand()%10)+1;-我对其进行硬编码只是为了测试*/
printf(“选择一个介于1和10之间的数字:”);
scanf(“%s”,用户编号);
/*我已经更新了if位,分别检查范围和输入是否为有效数字*/
if(isdigit(用户编号[0]))
{
int inputednumber=atoi(userNumber);/*将字符串转换为