c用户输入中的错误编译

时间:2012-01-30 20:11:40

标签: c function compiler-construction methods if-statement

我必须写一个程序,询问用户是男性还是女性,有生日(mm dd yyyy)然后是日期。然后计算一个保险用品的年龄,该保险用于if else循环,告诉用户他们将支付的价格。这是我的代码:

#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行:错误:'&lt; ='标记之前的预期表达式 第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.

4 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

您在各种比较中缺少操作数:

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

例如,您应该编写

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

希望这有帮助。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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