猜数游戏
我按书上的代码敲了一下、但是为什么出错呢? 感觉代码应该没什么问题。(在linux上用vim编辑器做的)编译时候出现的错误:undefined reference to "initialize_number_generator"
undefined reference to "read_guess"
/* 猜数游戏 */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_NUMBER 100
//全局变量
int secret_number;
//函数声明
void initialize_number_generator(void);
void choose_new_secret_number(void);
void read_guesses(void);
//主函数
int main(void)
{
char command;
//输入提示
printf("Guess the secret number between 1 and %d.\n\n", MAX_NUMBER);
initialize_number_gernerator();
do {
choose_new_secret_number();
printf("A new number has been chosen.\n");
read_guess();
printf("Play again? (Y/N) ");
scanf("%c", &command);
printf("\n");
} while(command == 'y' || command == 'Y');
return 0;
}
/********************************************************
* initialize_number_generator: Initializes the random *
* number generator using *
* the time of day. *
*********************************************************/
void initialize_number_generator(void)
{
srand((unsigned) time(NULL));
}
/*********************************************************
* choose_new_secret_number: Randomly selects a number *
* between 1 and MAX_NUMBER and*
* stores it in secret_number. *
**********************************************************/
void choose_new_secret_number(void)
{
secret_number = rand() % MAX_NUMBER + 1;
}
/**********************************************************
* read_guesses: Repeatedly reads user guesses and tells *
* the user whether each guess is too low, *
* too high, or correct. When the guess is *
* correct, prints the total number of *
* guesses and returns. *
***********************************************************/
void read_guesses(void)
{
int guess, num_guesses = 0;
for ( ; ; )
{
num_guesses++;
printf("Enter guess: ");
scanf("%d", &guess);
if (guess == secret_number)
{
printf("You won in %d guesses!\n\n", num_guesses);
return;
}
else if (guess < secret_number)
printf("Too low; try again.\n");
else
printf("Too high; try again.\n");
}
}