簡體   English   中英

我對如何解決此警告和錯誤感到有些困惑

[英]I'm a bit confused about how to solve this warning and error

我必須編寫一個代碼,其中生成 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;
    }
}

在您的 while 循環中,您應該將打印語句括在花括號{} 內。 使用分號結束 while 語句。 此外,您聲明了 time_t,但您只是在使用時間。 沒有聲明可變時間。

我更正你的程序:

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM