读书人

生手什么都不懂 求指点这个程序 //程

发布时间: 2012-06-23 14:52:43 作者: rapoo

新手什么都不懂 求指点这个程序 //程序清单9.9 and 9.10 and 9.11-多源代码文件程序的编译- 旅店收费程序
这是c primer plus 231 上的一个例题:希望前辈们指点下小弟 ,小弟将万分感谢!
先贴源代码:(我的编译器是vs2010)

第一个文件:usehotel.c
#include <stdio.h>
#include "hotel.h" //定义常量,声明函数
int main (void)

{
int nights;
double hotel_rate;
int code;

while ((code = menu())!=QUIT)
{
switch (code)
{
case 1: hotel_rate = HOTEL1;
break;
case 2: hotel_rate = HOTEL2;
break;
case 3: hotel_rate = HOTEL3;
break;
case 4: hotel_rate = HOTEL4;
break;
default: hotel_rate = 0.0;
printf("Oops!\n");
break;
}
nights = getnights();
showprice(hotel_rate,nights);
}
printf("thank you and goodbye.");
return 0;
}

第二个文件:hotel.c

//旅店管理函数
#include <stdio.h>
#include "hotel.h"

int menu(void)
{
int code, status;

printf("%s",STARS)

printf("Enter the number of the desired hotel:\n");
printf("1)Fairfield Arms 2)Hotel Olympic\n");
printf("3)Chertworthy plaza 4)the Stockton\n");
printf("5)quit\n");

printf("%s%s\n",STARS,STARS);

while ((status = scanf("%d",&code))!=1 || (code <1 || code >5 ))
{
if (status != 1)
scanf ("%*s");
printf("Enter an integer from 1 to 5,please.\n");
}
return code;
}

int getnights(void)
{
int nights;

printf("HOW many nights are needed? ");
while (scanf ("%d",&nights) != 1)
{
scanf ("%*s");
printf("Please enter integer,such as 2 . \n");
}
return nights;
}
void showprice (double rate,int nights)
{
int n;
double total = 0.0;
double factor = 1.0;
for (n=1; n<=nights; n++,factor *= DISCOUNT)
total += rate * factor;
printf("the total cost will be $%0.2f.\n",total);
}

第三个文件: hotel.h
#define QUIT 5
#define HOTEL1 80.00
#define HOTEL2 125.00
#define HOTEL3 155.00
#define HOTEL4 200.00
#define DISCOUNT 0.95
#define STARS ********************

int menu(void) // choice list

int getnights(void); //return the days

void showprice (double,int);

-------------------------------------------------
报错信息以及截图:
1>------ 已启动生成: 项目: 休闲鞋, 配置: Debug Win32 ------
1>生成启动时间为 2012-6-18 0:26:09。
1>PrepareForBuild:
1> 正在创建目录“c:\documents and settings\administrator\my documents\visual studio 2010\Projects\休闲鞋\Debug\”。
1>InitializeBuildStatus:
1> 正在创建“Debug\休闲鞋.unsuccessfulbuild”,因为已指定“AlwaysCreate”。
1>ClCompile:
1> usehotel.c
1>c:\documents and settings\administrator\my documents\visual studio 2010\projects\hotel.h(11): error C2085: “getnights”: 不在形参表中
1>c:\documents and settings\administrator\my documents\visual studio 2010\projects\hotel.h(13): error C2085: “showprice”: 不在形参表中
1>c:\documents and settings\administrator\my documents\visual studio 2010\projects\usehotel.c(5): error C2085: “main”: 不在形参表中
1>c:\documents and settings\administrator\my documents\visual studio 2010\projects\usehotel.c(5): error C2143: 语法错误 : 缺少“;”(在“{”的前面)
1>c:\documents and settings\administrator\my documents\visual studio 2010\projects\usehotel.c(26): warning C4013: “getnights”未定义;假设外部返回 int
1>c:\documents and settings\administrator\my documents\visual studio 2010\projects\usehotel.c(27): warning C4013: “showprice”未定义;假设外部返回 int
1> hotel.c
1>c:\documents and settings\administrator\my documents\visual studio 2010\projects\hotel.h(11): error C2085: “getnights”: 不在形参表中
1>c:\documents and settings\administrator\my documents\visual studio 2010\projects\hotel.h(13): error C2085: “showprice”: 不在形参表中


1>c:\documents and settings\administrator\my documents\visual studio 2010\projects\hotel.c(7): error C2082: 形参“menu”的重定义
1>c:\documents and settings\administrator\my documents\visual studio 2010\projects\hotel.c(7): error C2143: 语法错误 : 缺少“;”(在“{”的前面)
1>c:\documents and settings\administrator\my documents\visual studio 2010\projects\hotel.c(10): error C2059: 语法错误:“,”
1>c:\documents and settings\administrator\my documents\visual studio 2010\projects\hotel.c(17): error C2059: 语法错误:“,”
1> 正在生成代码...
1>
1>生成失败。
1>
1>已用时间 00:00:00.84
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========












[解决办法]
头文件中

int menu(void) // choice list

加个分号试试。。目测中
[解决办法]
偶遇到类似问题都是用
“每次用/*...*/注释掉不同部分再重新编译,直到定位到具体语法出错的位置。”
的方法解决的。

读书人网 >C语言

热点推荐