2012-01-30 85 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

你在其中一個返回語句中缺少一個分號。最長的文本行最後沒有分號。