| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 500 人关注过本帖
标题:猜数游戏
只看楼主 加入收藏
kim2014
Rank: 2
等 级:论坛游民
帖 子:21
专家分:11
注 册:2011-5-20
结帖率:100%
收藏
 问题点数:0 回复次数:1 
猜数游戏
我按书上的代码敲了一下、但是为什么出错呢? 感觉代码应该没什么问题。(在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");
    }
}
                                                            
搜索更多相关主题的帖子: void 游戏 undefined reference include 
2012-03-29 19:41
苦逼计科班
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2012-10-16
收藏
得分:0 
我是新手,不愿看这个
2012-11-09 22:34
快速回复:猜数游戏
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022191 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved