C I';我对如何解决这个警告和错误有点困惑

C I';我对如何解决这个警告和错误有点困惑,c,random,numbers,user-input,C,Random,Numbers,User Input,我必须编写一个代码,其中随机数在1到100之间生成,用户必须输入数字,直到他们算出为止。最后,它必须告诉他们花了多少猜测才弄明白。我有一个错误和一个警告,我都不知道如何处理。我试着查找它们,但找不到真正能帮助我的东西: main.c: In function ‘main’: main.c:18:26: warning: implicit declaration of function ‘time’ [-Wimplicit-function-declaration] srand(

我必须编写一个代码,其中随机数在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;
    }
}
#包括
#包括
int main(){
字符名[1024];
inti,n;
时间;
int-randNum;
int用户尝试;
int输入用户;
printf(“请输入您的姓名:\n”);
scanf(“%s”,名称);
printf(“你好%s\n,我们要玩一个游戏,你猜一个秘密号码。我会告诉你你猜得太高还是太低,最后我还会告诉你你猜了多少。\n”,name);
//初始化随机数生成器
srand((未签名)时间(&t));
//计算介于100和1之间的随机数*/
randNum=rand()%100+1;
做{
printf(“猜一个随机数,但我打赌你会猜错:\n”);
scanf(“%d”,输入用户);
if(inpUser==randNum){
printf(“这就是数字!也许我低估了你。\n”);
}
else if(inpUser>randNum){
printf(“你有点高了,巴德。\n”);
}
else if(inpUser
在while循环中,应该将print语句括在大括号{}内。使用分号结束while语句。另外,您声明了time\u t,但随后只使用了time。未声明可变时间

我更正您的程序:

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

}
#包括“stdafx.h”
#包括
#包括
#包括
int main(){
字符名[1024];
inti,n;
时间;
int randNum=0;
int usertrys=0;
int inpUser=0;
printf(“请输入您的姓名:\n”);
scanf(“%s”,名称);
printf(“你好%s\n,我们要玩一个游戏,你猜一个秘密号码。我会告诉你你猜得太高还是太低,最后我还会告诉你你猜了多少。\n”,name);
//初始化随机数生成器
srand((未签名)时间(&t));
//计算介于100和1之间的随机数*/
randNum=rand()%100+1;
做{
printf(“猜一个随机数,但我打赌你会猜错:\n”);
scanf(“%d”、&inpUser);
if(inpUser==randNum){
printf(“这就是数字!也许我低估了你。\n”);
}
else if(inpUser>randNum){
printf(“你有点高了,巴德。\n”);
}
else if(inpUser
修复缩进,问题就会变得明显。如果它不是==并且不是>,那么它一定是@AndrewHenle感谢remarque(it time.h而不是ctime)。当我尝试运行此操作时,我得到了分段错误(内核转储)…程序完成,退出代码为139。@Varian它已更正,但在原点,我纠正了语法和编译方面,而不是执行方面,也不是功能方面