这段代码有什么问题?如果变量是字母,我无法测试名为数字的变量
What is wrong about this code? I can't test variable called number if it's a letter
有人可以帮助我了解我的代码出了什么问题吗?一切正常,直到 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()
会在文件末尾返回 EOF
,fgets()
会返回 NULL
。 scanf()
会返回EOF
. feof()
函数只能在读取操作命中文件末尾后使用。
除此之外,你的代码不会编译,它充其量是伪代码,最坏的情况是翻译不好的基本意大利面条代码。
不要以这种方式使用标签和goto,使用循环和测试来构建代码,并可能将其中一些拆分为单独的函数。
相关文章:
- 如何在提升构建中设置环境变量以进行提升单元测试框架?
- 使用 gtest 框架在单元测试代码中检查目标对象的私有变量的最佳实践是什么?
- 在 gtests 中测试C++全局变量的最佳方法
- 如何对私有变量是否已更改进行单元测试
- 在命名空间中定义变量,但在测试中获取空值
- 测试适用于成员变量的成员函数
- C++ / GoogleTest - 如何测试正在测试的类的成员变量
- 单元测试呼叫实例变量的函数
- 如果需要测试,则C 将常数变量放在CC文件中
- 单元测试一个变量的true和false
- 我可以测试变量是否已使用 new 或 malloc 分配吗?
- 对同一变量的测试结果似乎不同
- C++在测试成员变量是否等于NULL时冻结
- QT 测试 - 变量/对象失去价值
- 这段代码有什么问题?如果变量是字母,我无法测试名为数字的变量
- Xcode C++全局变量的单元测试
- 在谷歌测试中访问受保护的变量
- Google使用SetUpTestCase静态变量测试未定义的引用
- 访问私有变量的单元测试
- C++ 使用变量参数"..."进行布尔测试