【问题标题】:I'm a bit confused about how to solve this warning and error我对如何解决这个警告和错误有点困惑
【发布时间】:2020-02-15 18:18:46
【问题描述】:

我必须编写一个代码,其中生成一个介于 1 和 100 之间的随机数,并且用户必须输入数字,直到他们弄明白为止。最后,它必须告诉他们需要多少次猜测才能弄清楚。我有一个错误和一个警告,我都不知道如何处理。我尝试查找它们,但我真的找不到可以帮助我的东西:

main.c: In function ‘main’:
main.c:18:26: warning: implicit declaration of function ‘time’ [-Wimplicit-function-declaration]
         srand((unsigned) time(&t));
                          ^~~~
main.c:43:1: error: expected ‘while’ before ‘}’ token
 }
 ^
main.c:43:1: error: expected declaration or statement at end of input

这是我正在使用的代码以供参考:

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

int main() {
    char name[1024];
    int i, n;
    time_t t;
    int randNum;
    int userTries;
    int inpUser;

    printf("Please enter your name: \n");
    scanf("%s", name);
    printf("Hello %s\n, we're going to play a game where you guess a secret number. I will tell you if you're too high or too low, and I will also tell you at the end how many guesses you took.\n", name);

    //initializes the random number generator
    srand((unsigned) time(&t));

    //calculates a random number between 100 and 1*/
    randNum = rand() % 100 + 1;

    do {
        printf("Guess a random number, but I bet you'll get it wrong:\n");
        scanf("%d", inpUser);
        if (inpUser == randNum) {
            printf("That's the number! Maybe I underestimated you.\n");
        }
        else if (inpUser > randNum) {
            printf("You're a little high there, bud.\n");
        }
        else if (inpUser < randNum) {
            printf("Maybe go a little higher next time?\n");

            userTries++;
        }

        while (randNum != inpUser);
        printf("Congratulations, you actually did it! That's the number! It took you %d tries, but you did it!", userTries);

        return 0;
    }
}

【问题讨论】:

  • 修复缩进,问题就会显现出来。
  • 如果不是 == 也不是 > 那么它一定是

标签: c random numbers user-input


【解决方案1】:

在您的 while 循环中,您应该将 print 语句括在花括号{} 中。使用分号结束 while 语句。此外,您声明了 time_t,但您使用的只是时间。没有声明可变时间。

【讨论】:

    【解决方案2】:

    我更正了你的程序:

    #include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main() {
    
        char name[1024];
        int i, n;
        time_t t;
        int randNum = 0;
        int userTries = 0;
        int inpUser = 0;
    
        printf("Please enter your name: \n");
        scanf("%s", name);
        printf("Hello %s\n, we're going to play a game where you guess a secret number. I will tell you if you're too high or too low, and I will also tell you at the end how many guesses you took.\n", name);
    
        //initializes the random number generator
        srand((unsigned) time(&t));
    
        //calculates a random number between 100 and 1*/
        randNum = rand() %100 + 1;
    
        do {
            printf("Guess a random number, but I bet you'll get it wrong:\n");
            scanf("%d", &inpUser);
            if (inpUser == randNum) {
                printf("That's the number! Maybe I underestimated you.\n");
            }
            else if (inpUser > randNum) {
                printf("You're a little high there, bud.\n");
            }
            else if (inpUser < randNum) {
                printf("Maybe go a little higher next time?\n");
    
                userTries++;
    
            }
        }
        while (randNum != inpUser);
        printf("Congratulations, you actually did it! That's the number! It took you %d tries, but you did it!", userTries);
    
        return 0;
    
    }
    

    【讨论】:

    • @AndrewHenle 感谢您的评论(它是 time.h 而不是 ctime)
    • 当我尝试运行它时,我得到分段错误(核心转储)...程序以退出代码 139 完成。
    • @Varian 它已被更正,但在起源上,我更正了句法和编译方面,而不是执行,也不是功能方面
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 2016-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多