错误:字符串常量[puts()和gets()语句错误之前的预期声明说明符或'...' [英] Error: expected declaration specifiers or '...' before string constant [puts() and gets() statement errors

查看:148
本文介绍了错误:字符串常量[puts()和gets()语句错误之前的预期声明说明符或'...'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

After compiling my program of Dice Roll, I got this error. What is wrong with the code?

也是在我使用gets()而不是scanf()命令之前,但是由于这个错误,我得到了这个错误-传递'gets'的参数1使指针从整数开始而没有强制转换因此,我删除了gets()命令并使用了scanf,然后再没有关于scanf()的错误.

Also before I was using gets() instead of scanf() command, but because of that I got this error - passing argument 1 of 'gets' makes pointer from integer without a cast So I removed the gets() command and used scanf and then there was no error regarding scanf().

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

好吧,按照答案,我知道我应该如何使用gets()命令,为什么我不应该使用它,而应该使用scanf().因此,我进行了更改.尽管我遇到了两个新错误,但这一次与我使用的delay()命令有关.

Ok, so as per the answer I got to know how I should have used the gets() command and why I shouldn't use it instead should use scanf(). So, I made the changes. Though I have encountered two new errors, this time it's related to the delay() command that I used.

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

Errors: undefined reference to delay |error: ld returned 1 exit status|

好的,所以我通过使用windows.h库中的Sleep()命令而不是Delay()命令解决了我的最后一个错误.程序已编译.但是程序中仍然存在运行时错误,它在到达roll1之前可以很好地运行,但是随后只打印下两个语句并终止程序,而无需输入任何猜测.

OK so I solved my last errors by using Sleep() command from windows.h library instead of Delay() command. The programs was compiled. But still there is a runtime error in the program, it works well till getting the roll1 but then it just print the next two statement and terminated the programs without taking a input for the guess.

它会跳过 printf(是更高/更低还是相同?(按H/L/S)\ n"); 之后的所有代码,并直接终止程序.

It skips all the code after printf("Will it be Higher/Lower or the same? (press H/L/S)\n"); and directly terminates the program.

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

Ok So I solved above problem adding a whitespace before the "%c" in scanf(" %c", &nextGuess); statement. (Little things xD) Now only problem is that my toupper() command is not working.

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

At the 2nd line of your main, you declare char nextGuess, instead of char nextGuess;

编译器告诉您它期望在之后的说明符或...,,因此您可以添加这些说明符,或以; 正确结束该行.

The compiler tells you it expects specifiers or ... after , so either you add these, or you end the line properly with;.

对于另一个问题,您提到:

And for the other problem you mention:

传递'gets'的参数1使指针从整数开始而无需强制转换

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

由于 gets 参数应为 char * str ,而您未提供它.

Since gets argument should be char *str and you didn't provide it.

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

You can fix that by, for example:

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

但是我更喜欢 scanf 解决方案

PS :(在代码的当前编辑版本中) ***//错误行*** 不是注释(至少不是全部)它),因为 *** 仍被视为代码的一部分,并会导致错误.将//左移一点,或将整个部分用/* ... */

PS: (in a now edited version of the code) ***//Error Line*** is not a comment (at least, not all of it) since the *** is still counted as part of the code and will cause an error. Either move the // a bit to the left or enclose that whole part with /* ... */

这篇关于错误:字符串常量[puts()和gets()语句错误之前的预期声明说明符或'...'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆