gpt4 book ai didi

C If语句错误: exited with non-zero status

转载 作者:行者123 更新时间:2023-11-30 14:56:45 25 4
gpt4 key购买 nike

我是c语言新手,我正在尝试制作一个打字游戏。但是if语句不起作用,并且输出是“以非零状态退出”。可能问题是由IDE,我不明白,谢谢。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main (void)
{
char start;
printf ("here is a typing game\n");
printf ("type any key to start\n");
scanf ("%c", &start);
void game()
{
int i;
char answer;
char type[20];
{
for (i = 0; i < 20; i++)
{
printf ("%c", 'a' + (random() % 26));
}
}
scanf ("%s", answer);
if (strcmp(answer,type) == 0)
{
printf ("good\n");
}
else
{
printf ("so bad\n");
}
}
game();
}

最佳答案

这段代码有很多问题。

  1. 一个函数不能包含在另一个函数中(就像在 main 中定义的 game 那样)。

  2. answer 是单个 char,而不是字符串,因此要使用 scanf 读取它,您需要使用行 scanf("%c", &answer); - 如果您想要读取单个字符,即;从代码的其余部分来看,您似乎打算将 answer 设为字符串,因此您必须将 answer 声明为 char [20] 数组并调用 scanf"%s" 格式说明符。

  3. 您不能使用 strcmpchar 与字符串进行比较。 strcmp 用于比较字符串与字符串,而不是字符串与字符或字符与字符。此外,您永远不会在代码中的任何位置将字符串分配给数组 type

关于C If语句错误: exited with non-zero status,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44445423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com