你好,还在掌握 C 的窍门,这个程序应该可以工作!但它一直在 void cheack(int *guess, int num)
崩溃,我只是不知道如何修复它。有人有建议吗?谢谢。此外,该程序应该生成一个随机数 1-20,用户应该猜测它,如果打印出高或低五次尝试,直到用户猜对或输为止。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int ranNumGen();
void Right(int num); // prompt that you guessed right
void cheack(int* guess, int num); // if else's to tell you to high to low...
void sorry(int num); // prompt for not guessing it right
int main(void)
{
// Local declarations
int num;
int guess;
// Sets variables
num = ranNumGen();
// Prompt
printf("\nLets play a game");
printf("\nCan you guess the number I am thinking of?");
printf("\nI will give you a hint its a number between (1 and 20)");
printf("\nI am also going to going to give you five tries.");
printf("\nHave a guess! ");
printf("\nShows number to win %d\n", num); // shows number to win to check if it works
scanf_s("%d", &guess);
// Check function
cheack(&guess, num);
// End promts (will be its own function later)
if (guess == num)
{
printf("\nGoodbye\n");
}
else
{
sorry(num);
}
return 0;
}
int ranNumGen()
{
int range;
srand(time(NULL));
range = 20;
range = (rand() % range + 1);
return range;
}
void Right(int num)
{
printf("\n\t*******************************************");
printf("\n\t* Wow you guessed it! Yep its %d alright. *", num);
printf("\n\t* You won't get it next time! :) *");
printf("\n\t*******************************************");
}
void cheack(int *guess, int num)
{
int a;
a = 0;
while (a < 4)
{
if ((*guess) == num)
{
Right(num);
break;
}
if ((*guess) > num)
{
printf("\nYour guess is to high.", *guess);
printf("\nTry again: ");
scanf_s("%d", &guess);
}
if ((*guess) < num)
{
printf("\nYour guess is to low.", *guess);
printf("\nTry again: ");
scanf_s("%d", &guess);
}
}
return;
}
void sorry(int num)
{
printf("\nSorry buddy, you didn't guess it...");
printf("\nThe number was %d better luck next time.\n", num);
return;
}
最佳答案
scanf
需要一个指针,因此此处不需要地址运算符:
scanf_s("%d", guess);
关于调试中的 C 函数崩溃(未给出错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22129952/