【问题标题】:error: invalid operands to binary + (have 'float *' and 'float *')错误:二进制 + 的操作数无效(有 'float *' 和 'float *')
【发布时间】:2017-07-06 06:53:12
【问题描述】:

有人可以帮帮我吗?我唯一的错误是我不能在最后添加成本 1、成本 2 和成本 3 以打印出这三个项目的总数。我收到上述错误。

#include <stdio.h>
#include <string.h>

char name1[100];
char name2[100];
char name3[100];
char price1[100];
char price2[100];
char price3[100];
float cost1[100];
float cost2[100];
float cost3[100];
float total[300];

int main()
{
printf("Welcome to the UT super mart*-*-\n");
printf("Enter the name of your item 1: ");
fgets(name1, sizeof(name1), stdin);
printf("Enter the price of %s: ", name1);
fgets(price1, sizeof(price1), stdin);
sscanf(price1, "%f", &cost1);

printf("Enter the name of your item 2: ");
fgets(name2, sizeof(name2), stdin);
printf("Enter the price of %s: ", name2);
fgets(price2, sizeof(price2), stdin);
sscanf(price2, "%f", &cost2);

printf("Enter the name of your item 3: ");
fgets(name3, sizeof(name3), stdin);
printf("Enter the price of %s: ", name3);
fgets(price3, sizeof(price3), stdin);
sscanf(price3, "%f", &cost3);

total=cost1+cost2+cost3; /*this is what causes the error*/
printf("Your total is: %f\nThank you for shopping at the UT super mart.", total);
return(0);
}

【问题讨论】:

  • 请记住我是初学者
  • 嗨 Derek,欢迎来到 Stack Overflow。您的问题是关于 C 语言,而不是标记为 C#。这些是完全不同的语言,因此请注意正确标记。也就是说,cjds 已经发布了一个可以解释问题的答案。
  • 我的错,我对编程非常陌生
  • 它可以帮助您获得更好的答案,并且人们倾向于监控他们最感兴趣的领域,C# 人们不一定会给您最好的答案 @987654325 @ 问题。希望你能找到答案。
  • 我已回滚您最近的编辑。您更改了问题中的代码以更正您所询问的问题。这会使任何答案无效并使问题变得无用。请不要那样做。

标签: c


【解决方案1】:

您的错误是将成本变量声明为数组。不需要。

float cost1[100];
float cost2[100];
float cost3[100];
float total[300];

应该是

float cost1;
float cost2;
float cost3;
float total;

【讨论】:

    猜你喜欢
    • 2016-09-24
    • 2016-01-06
    • 1970-01-01
    • 1970-01-01
    • 2019-03-02
    • 2012-04-16
    • 2023-01-26
    • 1970-01-01
    相关资源
    最近更新 更多