这段代码有什么问题?如果变量是字母,我无法测试名为数字的变量

What is wrong about this code? I can't test variable called number if it's a letter

本文关键字:变量 测试 数字 代码 什么 如果 问题 段代码      更新时间:2023-10-16

有人可以帮助我了解我的代码出了什么问题吗?一切正常,直到 p 指针在分配的内存上带来一个字母而不是一个数字。

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
int main() {
    int key;
    int c = 0;
    int number;
    int coins;
    int coins2;
    char answear;
    int n;
    int* p;
    FILE* fp;
    fp = fopen("D://coins.txt", "r");
    PAR2: coins = 100;
    if (!fp) // daca fp nu exista;
    {
        printf("No previous data!.n");
    }
    if (!feof(fp)) // the condition to be read line by line from the txt file.
    {
        printf("Taking values:n");
        fscanf(fp, "%d", & c); //reading values from the savegame.
        coins = c; //getting coins from the savegame.
    }
    fclose(fp);
    for (n = 1; n > 0;) //infinite loop
    {
        if (coins <= 0) //if running out of coins
        {
            PAR4: printf("Sorry but you have no dollars left. Try again Y/N?n");
            PAR3: answear = getch();
            switch (answear) //testing answear
            {
            case 'Y':
                {
                    goto PAR2; //give 100
                }
            case 'y':
                {
                    goto PAR2; //give 100
                }
            case 'N':
                {
                    exit(1);
                }
            case 'n':
                {
                    exit(1);
                }
            default: //if the answear if undifined
                {
                    printf("Not an option. Answear only with Y/N!n");
                    goto PAR3;
                }
            }
        }
        PAR1: printf("You have: %d$n", coins);
        printf("nEnter your number! WARNING: it must be between 1 and 5 only.n");
        p = (int*)malloc(sizeof(int)); //memory for number variable
        scanf("%d", p); //take the number
        if (*p > 0 && *p < 6) //I need the number to be between 1 and 5
        {
            printf("Your number is: %dn", number);
            showing the chosen number
            key = (rand() % 5) + 1; //generating a random key
            printf("The extracted number is: %dn", key);
            if (key == number)
                if the key is guessed {
                printf("Congratulations! You have won 10$.n");
                coins2 = coins + 10;
            } else {
                printf("You are not so lucky this time!n");
                coins2 = coins - 10;
            }
            coins = coins2;
            printf("Rotate again? Y/N!n");
            play again ?
                PAR5 : answear = getch();
            switch (answear) //test the answear
            {
            case 'Y':
                {
                    system("cls"); //clear screen
                    goto PAR4; //going to the starting of the loop
                }
            case 'y':
                {
                    system("cls");
                    clear screen
                    goto PAR4; //going to the starting of the loop
                }
            case 'N':
                {
                    fp = fopen("D://coins.txt", "w");
                    save the coins
                    fprintf(fp, "%d", coins);
                    fclose(fp);
                    exit(1);
                }
            case 'n':
                {
                    fp = fopen("D://coins.txt", "w"); //save the coins
                    fprintf(fp, "%d", coins);
                    fclose(fp);
                    exit(1);
                }
            default:
                {
                    printf("Not an option. Answear only with Y/N!n");
                    goto PAR5;
                }
            }
        } else
            printf("Wrong number:n");
        /*
    Message if a letter or symbol has been entered (this message is shown over and over again, and I don't know why...)
    even if it's infinite loop, the loop it should reset so it should again pass the statements above, but it doesn't. 
    */
        free(p); // free the number.
    }
    getch();
    return 0;
}

您无法通过测试 feof(fp) 来检查是否可以从文件中读取数据。 您必须尝试读取并检查读取 API 的返回值:getchar()会在文件末尾返回 EOFfgets()会返回 NULLscanf()会返回EOF. feof()函数只能在读取操作命中文件末尾使用。

除此之外,你的代码不会编译,它充其量是伪代码,最坏的情况是翻译不好的基本意大利面条代码。

不要以这种方式使用标签和goto,使用循环和测试来构建代码,并可能将其中一些拆分为单独的函数。