获取无效的操作数到二进制+以及先前的声明错误

时间:2013-10-22 14:33:34

标签: c function pointers math sales

据我所知,我没有使用多个运算符,但我无法让我的程序运行。

我很确定我的功能正常,除了电话,但我不知道如何将其应用于特定代码。

#include<stdio.h>

void inputTotalSales(float *);
void calculateTaxes(float *, float *, float *, float *);
void displayTaxes(float *, float *, float *);

int main()
{
float totalSales, salesTax, stateTax, countyTax;

    inputTotalSales(& totalSales);
    calculateTaxes(&totalSales, & salesTax, & stateTax, & countyTax);
    displayTaxes(& salesTax, &stateTax, &countyTax);

  return 0;
}

void inputTotalSales(float *totalSalesp)
{
      printf("Please enter your total sales for the month: ");
      scanf("%f", totalSalesp);
      printf("\nYour monthly sales total is %.2f", *totalSalesp);

}

void calculateTaxes (float *totalSalesp, float *salesTaxp, float *stateTaxp,float     *countyTaxp )
{
    salesTaxp = (totalSalesp * .06);
    stateTaxp = (totalSalesp * .04);
    countyTaxp = (totalSalesp * .02);
}

void displayTaxes (float salesTax, float stateTax, float countyTax)
{
    printf("\nYour monthly sales tax is %.2f.", salesTax);
    printf("\nYour monthly state tax is %.2f.", stateTax);
    printf("\nYour monthly county tax is %.2f.", countyTax);
}

此外,您推荐的任何书籍都会很棒!我的老师从未有过他喜欢的书,所以我们从来没有被分配过......而且我很挣扎。任何其他好的网站也将非常感谢!

4 个答案:

答案 0 :(得分:0)

首先,你的原型:

void displayTaxes(float *, float *, float *);  

与您的功能定义不符:

void displayTaxes (float salesTax, float stateTax, float countyTax)
{  
...  
}  

我建议您更改原型以匹配您的功能定义。这将允许您当前的调用方法按(或函数def。来匹配原型,只要它们是相同的,但您必须在调用时传递指针)

在函数inputTotalSales中scanf函数中存在类型不匹配。创建局部变量,扫描值,然后进行分配:将inputTotalSales()更改为如下所示:

void inputTotalSales(float *totalSalesp)
{
      float x=0;
      printf("Please enter your total sales for the month: ");
      scanf("%f", &x);
      *totalSalesp = x;
      printf("\nYour monthly sales total is %.2f", *totalSalesp);

}

此外,您尝试在传递指针之后在此函数中为变量赋值,然后传回指针,而不更改它。要解决此问题,请将calculateTaxes()更改为:

void calculateTaxes (float *totalSalesp, float *salesTaxp, float *stateTaxp,float     *countyTaxp )
{
    *salesTaxp = (*totalSalesp * .06);
    *stateTaxp = (*totalSalesp * .04);
    *countyTaxp = (*totalSalesp * .02);
}

最后,在main(),最后一行,需要更改displayTaxes()的参数以匹配原型(即float,而不是float *)。将main()更改为如下所示:

int main()
{
    float totalSales, salesTax, stateTax, countyTax;

    inputTotalSales(&totalSales);
    calculateTaxes(&totalSales, & salesTax, & stateTax, & countyTax);
    displayTaxes(salesTax, stateTax, countyTax); //passing by value, not by reference, to match original prototype.

  return 0;
}

摘要 以下是删除问题的示例:

#include<stdio.h>

void inputTotalSales(float *);
void calculateTaxes(float *, float *, float *, float *);
void displayTaxes (float salesTax, float stateTax, float countyTax);


int main()
{
    float totalSales, salesTax, stateTax, countyTax;

    inputTotalSales(&totalSales);
    calculateTaxes(&totalSales, &salesTax, &stateTax, &countyTax);
    displayTaxes(salesTax, stateTax, countyTax);

  return 0;
}

void inputTotalSales(float *totalSalesp)
{
      float x=0;
      printf("Please enter your total sales for the month: ");
      scanf("%f", &x);
      *totalSalesp = x;
      printf("\nYour monthly sales total is %.2f", *totalSalesp);

}

void calculateTaxes (float *totalSalesp, float *salesTaxp, float *stateTaxp,float     *countyTaxp )
{
    *salesTaxp = (*totalSalesp * .06);
    *stateTaxp = (*totalSalesp * .04);
    *countyTaxp = (*totalSalesp * .02);
}


void displayTaxes (float salesTax, float stateTax, float countyTax)
{
    printf("\nYour monthly sales tax is %.2f.", salesTax);
    printf("\nYour monthly state tax is %.2f.", stateTax);
    printf("\nYour monthly county tax is %.2f.", countyTax);
}

答案 1 :(得分:0)

您的代码存在一些问题

1)calculateTaxes函数你传递的指针,你试图将指针乘以一个数字,而不是你应该将这些指针的值乘以数字。

2)您已经使用指针(float *)声明了displayTaxes,并且在定义函数时,您使用的是仅包含浮点数的不同声明。

以下是没有这些问题的代码:

void inputTotalSales(float *);
void calculateTaxes(float *, float *, float *, float *);
void displayTaxes(float , float , float );
int main()
{

 float totalSales, salesTax, stateTax, countyTax;

    inputTotalSales(& totalSales);
    calculateTaxes(&totalSales, & salesTax, & stateTax, & countyTax);
    displayTaxes(salesTax, stateTax, countyTax);

 return 0;
}



void inputTotalSales(float *totalSalesp)
{
   printf("Please enter your total sales for the month: ");
   scanf("%f", totalSalesp);
   printf("\nYour monthly sales total is %.2f", *totalSalesp);

}

void calculateTaxes (float *totalSalesp, float *salesTaxp, float *stateTaxp,float     *countyTaxp )
{
   *salesTaxp = (*totalSalesp * .06);
   *stateTaxp = (*totalSalesp * .04);
   *countyTaxp = (*totalSalesp * .02);
}


void displayTaxes (float  salesTax, float  stateTax, float  countyTax)
{
     printf("\nYour monthly sales tax is %.2f.", salesTax);
     printf("\nYour monthly state tax is %.2f.", stateTax);
     printf("\nYour monthly county tax is %.2f.", countyTax);
}

答案 2 :(得分:-1)

您正在传递指向您的函数的指针,但不会取消引用它们。

要访问float的{​​{1}},您需要取消引用它:float * totalSalesp

您可以记住:您需要尽可能多的星号来获取该类型中的内容。

答案 3 :(得分:-1)

本练习(大概)旨在教你如何使用和理解C中的指针。

您发布的代码存在大量错误。

您的声明和displayTaxes实现的原型是不同的。编译器会抱怨这一点,它们必须是相同的。例如:

void displayTaxes(float *, float *, float *);

应该与下面的实施相同:

void displayTaxes(float, float, float);

在calculateTaxes函数中,你没有取消引用指针,所以你试图通过浮点数多次指针(这在C中是不允许的)。相反,您必须取消引用totalSalesp以获取值,然后通过浮点数乘以该值;然后再次尝试将该值分配给指针,而不是指针指向的值。因此,必须取消引用这些指针才能分配它们的值:

void calculateTaxes (float *totalSalesp, float *salesTaxp, float *stateTaxp,
        float *countyTaxp )
{
    *salesTaxp = (*totalSalesp * .06);
    *stateTaxp = (*totalSalesp * .04);
    *countyTaxp = (*totalSalesp * .02);
}

调用displayTaxes时,你也试图取消引用不是指针的变量!所以调用应该只是在没有任何解除引用的情况下调用:

displayTaxes(*salesTax, *stateTax, *countyTax);

变为

displayTaxes(salesTax, stateTax, countyTax);

完成修复后,您将最终得到:

#include <stdio.h>

void inputTotalSales(float *);
void calculateTaxes(float *, float *, float *, float *);
void displayTaxes(float, float, float);


int main()
{
    float totalSales, salesTax, stateTax, countyTax;

    inputTotalSales(&totalSales);
    calculateTaxes(&totalSales, &salesTax, &stateTax, &countyTax);
    displayTaxes(salesTax, stateTax, countyTax);

    return 0;
}

void inputTotalSales(float *totalSalesp)
{
    printf("Please enter your total sales for the month: ");
    scanf("%f", totalSalesp);
    printf("\nYour monthly sales total is %.2f", *totalSalesp);
}

void calculateTaxes (float *totalSalesp, float *salesTaxp, float *stateTaxp,
        float *countyTaxp )
{
    *salesTaxp = (*totalSalesp * .06);
    *stateTaxp = (*totalSalesp * .04);
    *countyTaxp = (*totalSalesp * .02);
}    


void displayTaxes (float salesTax, float stateTax, float countyTax)
{
    printf("\nYour monthly sales tax is %.2f.", salesTax);
    printf("\nYour monthly state tax is %.2f.", stateTax);
    printf("\nYour monthly county tax is %.2f.", countyTax);
}

这最有可能带来理想的结果:

Please enter your total sales for the month: 564

Your monthly sales total is 564.00
Your monthly sales tax is 33.84.
Your monthly state tax is 22.56.
Your monthly county tax is 11.28.

如果您想获得一本关于C主题的书,请获取Kernighan和Ritchie的书:The C Programming Language