我刚从 C 开始,我无法获得一个简单的for
循环来在返回 bool 的函数内部循环。该程序是一个简单的猜谜游戏。
这是我的代码:
//standard input and output header
#include <stdio.h>
#include <conio.h>
#include <stdbool.h>
bool CheckGuess(int guess, int nums[]);
int main(void)
{
int nums[5];
int i;
int userGuess;
for (i = 0; i < sizeof(nums)/sizeof(int); i++)
{
nums[i] = 1 + rand() % 5;
}
printf("Guessing Game\n\n");
printf("Guess a number between 1 and 5: ");
scanf("%d", &userGuess);
if (CheckGuess(userGuess, nums))
{
printf("You win!");
}
else
{
printf("You lose!\nThe correct numbers are:\n\n");
for (i = 0; i < sizeof(nums)/sizeof(int); i++)
{
printf("%d\n", nums[i]);
}
}
//get character, wait/pause
getch();
}
bool CheckGuess(int guess, int nums[])
{
int z;
for (z = 0; z < sizeof(nums)/sizeof(int); z++)
{
printf("%d\n", z); //z just stops at 0 all the time
if (guess == nums[z])
{
return true;
}
}
return false;
}