c - C - 编程 HI-LO 游戏
问题描述
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <windows.h>
#include <stdlib.h>
#include <ctype.h>
#define PI 3.14159
#define PLANK_CONSTANT 6.626E-034
#define MASS_OF_ELECTRONS 9.109E-031
#define VELOCITY_OF_LIGHT 299800000
void hilo();
int main()
{
char check;
while (1)
{
hilo();
printf("Would you like to play again (y/n)? ");
scanf('%c', &check);
if (check != 'y' || check != 'y')
{
break;
}
}
system("pause");
return 0;
}
void hilo()
{
srand(time(NULL));
int count = 0;
int guess;
int randomnumber = rand() % 101;
while (1)
{
printf("Guess the number: ");
scanf("%d", &guess);
if (guess == randomnumber)
{
printf("Hooray, you have won!\n");
break;
}
if (guess < randomnumber)
printf("Wrong Number, Try again! The number you guessed is too low\n");
if (guess > randomnumber)
printf("Wrong Number, Try again! The number you guessed is too high\n");
count++;
if (count == 7)
{
printf("Sorry you lose, the number is: %d\n", randomnumber);
break;
}
}
}
有人可以帮我弄这个吗?猜数字7次后“破”C说有错误“Exception thrown at 0x586A4B02 (ucrtbased.dll) in ALLlabs.exe: 0xC0000005: Access violation reading location 0x00002563."
我试图到处搜索中断的问题,但找不到问题所在。各位大侠能告诉我代码有什么问题吗?您可以复制代码以尝试自己玩。
解决方案
就是这一行:
scanf('%c', &check);
它使用单引号 - 所以 '%c' 变成一个数字,而不是一个字符串。
尝试这个:
scanf(" %c", &check);
请记住在启用所有警告的情况下进行编译。不要忽略编译器警告!
之前有一些关于使用空间的说明"%c"
。
推荐阅读
- html - 将内部文本和我圈子内的文本居中
- typescript - 如何在 Sequelize 中正确启动 n:m 关联
- javascript - 使用 Ajax 在一个页面上添加多个条带结帐按钮
- python-3.x - 在 numpy 计算中按日期分隔数据
- r - 在情节中将日期时间对象的 x 轴设置为超过 1 天
- javascript - 使用 .when 和 .done 等待 ajax 响应
- c - 计算学生成绩的C程序
- c# - EF Core 3.1 cosmosdb 拥有的实体
- javascript - angularjs +如何访问api响应数组中的元素以在html中显示为消息
- python - 将总行添加到数据框的顶部