gpt4 book ai didi

c++ - 这段代码有什么问题吗?如果它是字母,我无法测试名为 number 的变量

转载 作者:行者123 更新时间:2023-11-30 21:37:14 25 4
gpt4 key购买 nike

有人可以帮助我理解我的代码有什么问题吗?一切都很顺利,直到 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: %d\n", number);
showing the chosen number
key = (rand() % 5) + 1; //generating a random key
printf("The extracted number is: %d\n", 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() 将返回 EOFfeof() 函数只能在读取操作到达文件末尾之后使用。

除此之外,您的代码无法编译,它充其量是伪代码,最坏的情况是翻译得很糟糕的基本意大利面条代码。

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

关于c++ - 这段代码有什么问题吗?如果它是字母,我无法测试名为 number 的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35807673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com