gpt4 book ai didi

c - 错误: expected declaration specifiers or '...' before string constant [puts() and gets() statement errors

转载 作者:行者123 更新时间:2023-11-30 16:43:36 24 4
gpt4 key购买 nike

编译我的 Dice Roll 程序后,出现此错误。代码有什么问题?

也在我使用 gets() 而不是 scanf() 命令之前,但因此我收到了此错误 - 传递“gets”的参数 1 使指针来自整数而不进行强制转换所以我删除了 gets() 命令并使用了 scanf,然后就没有关于 scanf() 的错误了。

出现这两个错误的原因是什么?

<小时/>

好的,根据答案,我知道应该如何使用 gets() 命令以及为什么我不应该使用它而应该使用 scanf()。所以,我做出了改变。虽然我遇到了两个新错误,但这一次与我使用的delay()命令有关。

错误:未定义的延迟引用 |错误:ld 返回 1 退出状态|

<小时/>

好的,我通过使用 windows.h 库中的 Sleep() 命令而不是 Delay() 命令解决了我最后的错误。程序已编译。但程序中仍然存在运行时错误,它在获得 roll1 之前运行良好,但随后它只打印接下来的两条语句并终止程序,而不接受猜测的输入。

它会跳过 printf("Will it be Higher/Lower or the same? (press H/L/S)\n"); 之后的所有代码并直接终止程序。

<小时/>

好的,所以我解决了上述问题,在 "%c" 之前添加了一个空格。在scanf(" %c", &nextGuess);陈述。 (小事xD)现在唯一的问题是我的toupper()命令不起作用。

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


int main()
{
int i, roll1=0, roll2=0, NumberOfRolls, RandomNUM1[50], RandomNUM2[50];
char nextGuess;

puts("Welcome to the Dice Roll Game");
puts("How many times do you want to roll a dice?");
scanf("%d", &NumberOfRolls);

for( i=1; i<=NumberOfRolls; i++ ) {
RandomNUM1[i] = ( rand()%6 ) + 1;
roll1 += RandomNUM1[i];
}

printf("\nYou Got %d in your first roll!\n", roll1);
Sleep(3000);
printf("\nLet's see if you can guess the value of next roll.\n");
printf("Will it be Higher/Lower or the same? (press H/L/S)\n");
scanf(" %c", &nextGuess);
toupper(nextGuess);

for( i=1; i<=NumberOfRolls; i++ ) {
RandomNUM2[i] = ( rand()%6 ) + 1;
roll2 += RandomNUM2[i];
}

if(nextGuess=='H'){
if(roll1<roll2){
printf("You are such a player, you guessed it right! It's %d", roll2);
}
else if(roll1>roll2){
printf("Uh-Oh! Bad Luck! First roll was higher, It's %d", roll2);
}
else if(roll1==roll2){
printf("Uh-Oh! Bad Luck! Both the rolls are same, It's %d", roll2);
}

}

if(nextGuess=='L'){
if(roll1>roll2){
printf("You are such a player, you guessed it right! It's %d", roll2);
}
else if(roll1<roll2){
printf("Uh-Oh! Bad Luck! First roll was lower, It's %d", roll2);
}
else if(roll1==roll2){
printf("Uh-Oh! Bad Luck! Both the rolls are same, It's %d", roll2);
}

}

if(nextGuess=='S'){
if(roll1==roll2){
printf("You are such a player, you guessed it right! It's %d", roll2);
}
else if(roll1>roll2){
printf("Uh-Oh! Bad Luck! First roll was higher, It's %d", roll2);
}
else if(roll1<roll2){
printf("Uh-Oh! Bad Luck! Second roll is higher, It's %d", roll2);
}

}

return 0;
}

最佳答案

你有一个流浪

main的第二行,声明char nextGuess,而不是char nextGuess;

编译器告诉您它需要在 , 之后添加说明符或 ...,因此您可以添加这些说明符,或者使用 ; 正确结束该行。

对于你提到的另一个问题:

passing argument 1 of 'gets' makes pointer from integer without a cast

因为 gets 参数应该是 char *str 而您没有提供它。

您可以通过以下方式解决该问题:

char tmp_NumberOfRolls[10];
gets(tmp_NumberOfRolls);
NumberOfRolls = atoi(tmp_NumberOfRolls);

但我更喜欢 scanf 解决方案

PS:(在现在编辑的代码版本中)***//Error Line*** 不是注释(至少不是全部)它),因为 *** 仍然被算作代码的一部分,并且会导致错误。将 // 向左移动一点,或者用 /* ... */

将整个部分括起来

关于c - 错误: expected declaration specifiers or '...' before string constant [puts() and gets() statement errors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45127682/

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