错误C2440和错误4024

时间:2015-01-28 03:58:54

标签: c

我试图为我的程序调用这两个函数时遇到不断的错误。因为这是我第一次上课,我无法弄清楚它出错的地方。它应该简单地调用函数grosscal和tax cal取决于你选择的工资率,使用switch case循环选择。

#include <stdio.h>
#define BASEHRS 40 // hours at pay1
#define OVERTIME 1.5 // 1.5 time
#define AMT1 300 // 1st rate tier
#define AMT2 150 // 2st rate tier
#define RATE1 0.15 // rate for 1st tier
#define RATE2 0.20 // rate for 2nd tier
#define RATE3 0.25 // rate for 3rd tier

double grossCal(double, double, double);
double taxCal(double, double);
double netCal(double, double, double);

int main(void)
{
double hours, gross, net, taxes;
double pay1, pay2, pay3, pay4;
int payrate;
pay1 = 8.75;
pay2 = 9.33;
pay3 = 10.00;
pay4 = 11.20;

printf("Enter the number of hours worked this week: ");
scanf("%lf", &hours);

printf("*****************************************************************\n");
printf("Enter the number corresponding to the desired pay rate or action:\n");
printf("1) $%.2f/hr\t\t2) $%.2f/hr\n", pay1, pay2);
printf("3) $%.2f/hr\t\t4) $%.2f/h\n", pay3, pay4);
printf("5) quit\n");
printf("*****************************************************************\n");
scanf("%d", &payrate);

switch (payrate)
{
    case '1' :  grossCal(&gross, pay1, hours);
                taxCal(gross, &taxes);
                break;
    case '2' :  grossCal(&gross, pay2, hours);
                taxCal(gross, &taxes);
                break;
    case '3' :  grossCal(&gross, pay3, hours);
                taxCal(gross, &taxes);
                break;
    case '4' :  grossCal(&gross, pay4, hours);
                taxCal(gross, &taxes);
                break;
    default  :  break;
}

net = gross - taxes;
printf("gross: $%.2f; taxes: $%.2f; net: $%.2f\n", gross, taxes, net);
return 0;
}
double grossCal(double *grossPay, double pay, double hours){
    if (hours <= BASEHRS)
        *grossPay = hours * pay;
    else
        *grossPay = BASEHRS * pay + (hours - BASEHRS) * pay * OVERTIME;
}
double taxCal(double gross, double *taxestotal){
    if (gross <= AMT1)
        *taxestotal = gross * RATE1;
    else if (gross <= AMT1 + AMT2)
        *taxestotal = AMT1 * RATE1 + (gross - AMT1) * RATE2;
    else
        *taxestotal = AMT1 * RATE1 + AMT2 * RATE2 + (gross - AMT1 - AMT2) * RATE3;
}

2 个答案:

答案 0 :(得分:1)

您有不匹配的函数声明,函数调用和函数定义。

声明

double grossCal(double, double, double);
double taxCal(double, double);

用法

  case '1' :  grossCal(&gross, pay1, hours); // &gross is not double
              taxCal(gross, &taxes);         // &taxes is not double.
              break;
  case '2' :  grossCal(&gross, pay2, hours);
              taxCal(gross, &taxes);
              break;
  case '3' :  grossCal(&gross, pay3, hours);
              taxCal(gross, &taxes);
              break;
  case '4' :  grossCal(&gross, pay4, hours);
              taxCal(gross, &taxes);

实施

double grossCal(double *grossPay, double pay, double hours){
...

double taxCal(double gross, double *taxestotal){
...

即使函数没有返回任何内容,您的使用和实现也会匹配。

你可以通过以下方式清理:

微小变化

更改声明

void grossCal(double*, double, double);
void taxCal(double, double*);

保持当前使用情况

更改实施

void grossCal(double *grossPay, double pay, double hours){
...

void taxCal(double gross, double *taxestotal){
...

略微改变但更好

接口

// Return the gross pay
double grossCal(double pay, double hours);

// Return the taxes.
double taxCal(double);

用法

简化它,以便重复代码最少。添加一个新变量  叫薪水。

  case 1 :  pay = pay1;
            break;
  case 2 :  pay = pay2;
            break;
  case 3 :  pay = pay3;
            break;
  case 4 :  pay = pay4;
            break;

在switch语句之后,

  gross = grossCal(pay, hour);
  taxes = taxCal(gross);

实现

double grossCal(double pay, double hours){
   if (hours <= BASEHRS)
      return (hours * pay);
   else
      return (BASEHRS * pay + (hours - BASEHRS) * pay * OVERTIME);
}

double taxCal(double gross){
   if (gross <= AMT1)
      return (gross * RATE1);
   else if (gross <= AMT1 + AMT2)
      return (AMT1 * RATE1 + (gross - AMT1) * RATE2);
   else
      return (AMT1 * RATE1 + AMT2 * RATE2 + (gross - AMT1 - AMT2) * RATE3);
}

答案 1 :(得分:0)

你的案件有问题 在使用时

case '1' :  //Code
            break;
case '2' :  //Code
            break;
case '3' :  //Code
            break;
case '4' : //Code
            break;

将此更改为

case 1 :  //Code
            break;
case 2 :  //Code
            break;
case 3 :  //Code
            break;
case 4 : //Code
            break;

因为payrate是一个整数变量 像这样更改你的函数声明:

void grossCal(double*, double, double);  //make it void and 1st parameter should be of pointer type. 
void taxCal(double, double*);  //make it void type and 2nd parameter should be of pointer type.