2012-01-30 81 views
1

我必须写一个程序,询问用户,如果他们是男性或女性,那里的生日(mm dd yyyy),然后日期。然后计算那些用于if if循环的保险事件的年龄,以告知用户他们将支付的价格。这里是我的代码:错误编译在c用户输入

#include<stdio.h> 
#include<conio.h> 


char* calculateAge(int month, int day, int year, int birthmonth, int birthday, int  birthyear, char gender) 
{ 
    int temp; 
    temp = (year - birthyear); 
    if (temp >= 33 && <= 62 && gender == 'm' || temp >= 30 && <= 62 && gender == 'f') { 
     return "The rate class is: Best rate - $40.00 per day or $200.00 per week."; 
    } else if (temp >= 25 && <= 29 && gender == 'f') { 
     return "The rate class is: Risk rate 1 - Best rate plus $10.00 per day or best  rate plus $55.00 per week."; 
    } else if (temp >= 25 && <= 32 && gender == 'm') { 
      return "The rate class is: Risk rate 1 - Risk rate 1 plus $7.00 per day or risk rate 1  plus $30.00 per week."; 
     } else if (temp >= 66 && gender == 'm' || temp >= 63 && gender == 'f') { 
      return "The rate class is: Best rate plus $2.00 for each year over age 66 (male) or 63 (female), per day or best rate plus $5.00 for each year over age 66 (male) or 63 (female), per  week." 
    } else { 
     return "Sorry, the renter is not 25 years of age or older."; 
    } 
} 

void main() { 
int month, day, year, birthmonth, birthday, birthyear; 
char gender; 
printf("\nWelcome to the car renter’s rate finder. "); 
printf("\nPlease enter today's date (mm dd yyyy): "); 
scanf("%d%d%d",&month, &day, &year); 
printf("\nPlease enter the renter’s gender (m/f):"); 
scanf("%c", &gender); 
printf("\nPlease enter the renter’s date of birth (mm dd yyyy):"); 
scanf("%d%d%d", &birthmonth, &birthday, &birthyear); 
printf("\n Thank you."); 
printf("%s", calculateAge(month, day, year, birthmonth, birthday, birthyear, gender)); 
return 0; 
} 

我不断收到这些错误,他说: 14行:错误:之前预期的表达式 '< =' 令牌 线16:同一 线18:同一 22行:错误:预期';' '}'令牌前

我不明白这些错误,有人可以帮助我让这个编程工作吗?这里是输出它应该有:

Welcome to the car renter’s rate finder. 

Please enter today’s date (mm dd yyyy): 1 23 2008 

Please enter the renter’s gender (m/f): m 

Please enter the renter’s date of birth (mm dd yyyy): 6 9 1983 

Thank you. 

Sorry, the renter is not 25 years of age or older. 

Welcome to the car renter’s rate finder. 

Please enter today’s date (mm dd yyyy): 1 23 2008 

Please enter the renter’s gender (m/f): f 

Please enter the renter’s date of birth (mm dd yyyy): 2 23 1980 

Thank you. 

The female renter is 27 years old. 

The rate class is: Risk rate 1 - $50.00 per day or $255.00 per week. 

回答

3

当编译器生成错误消息时,最可靠的方法是从开始,生成第一个错误消息。 (有时消息可能无序生成;在这种情况下,请从代码中引用最低行数的那个开始。)

如果您有语法错误,编译器可能难以计算出您意思。它通常会猜测你的应该是写的,但是这个猜测可能是错误的。编译器在猜测丢失分号时有时特别不好。

根据您得到的错误消息判断,您可能正在使用gcc。当我编译代码,第一个消息我得到的是:

c.c: In function ‘calculateAge’: 
c.c:9:23: error: expected expression before ‘<=’ token 

9号线:

if (temp >= 33 && <= 62 && gender == 'm' || temp >= 30 && <= 62 && gender == 'f') { 

和列23是第一<=运营商。

碰巧,这个特殊的消息告诉你到底是什么问题(你需要的<=前一个表达式),但在消息本身还不清楚的情况下,把它当作一个迹象表明,有东西错误的它指向的地方,或者可能稍微在它之前(比如,在前一行)。

修复该错误,重新编译,并在新版本的文件上报告的第一个错误执行相同的操作。

正如你更好地理解语言和你的编译器,你应该能够找出它在做什么,并一次纠正多个错误。但是现在,一次修正一个错误是一个好方法。

在你的代码的一些其他注意事项:

删除这一行:

#include<conio.h> 

这是不可移植的,并且你没有使用任何来自<conio.h>反正。

void main()是错误的;它应该是int main(void)

0

你缺少操作数在各种比较:

if (temp >= 33 && <= 62 && ... 

这里有个例子,你应该写

if (temp >= 33 && temp <= 62 && ... 

希望这有助于。

+0

是的,它帮助谢谢你! – anthony 2012-01-30 20:17:11

0

您的问题是表达式temp >= 25 && <= 29。我想你的意思是写temp >= 25 && temp <= 29

0

你在其中一个返回语句中缺少一个分号。最长的文本行最后没有分号。