显示最高销售数字和平均销售额

displays highest sales figure and average sales

本文关键字:销售额 数字 高销售 显示      更新时间:2023-10-16

l C++有这个程序来查找最高的销售数字和平均值,但它给出的错误在下面请帮助

#include <iostream>
using namespace std;
// You must add the three functions here
void getSales(float & sales1P, float & sales2P, float & sales3P,
        float & sales4P) {
    cout << "Enter four sales values: " << endl;
    cin >> sales1P >> sales2P >> sales3P >> sales4P;
}
float calcAverage(float sales1P, float sales2P, float sales3P, float sales4P) {
    return (sales1P + sales2P + sales3P + sales4P) / 4;
}
float findHighest(float sales1P, float sales2P, float sales3P, float sales4P)
{
    float highest = sales1P;
    if (sales2P > highest)
        highest = sales2P;
    if (sales3P > highest)
        highest = sales3P;
    if (sales4P > highest)
        highest = sales4P;
}
void displayOutput(float highestSales, float averageSales) {
    cout << "The highest sales figure is " << highestSales
    << " with an average of " << averageSales << endl;
}
int main()
{
    float sales1,
    sales2,
    sales3,
    sales4;
    float averageSales;
    float highestSales;
    for (int i = 0; i < 4; i++)
    // Get the sales for each division.
    sales1 = getSales();
    sales2 = getSales();
    sales3 = getSales();
    sales4 = getSales();
    //
    //getSales(sales1, sales2, sales3, sales4);
    averageSales = calcAverage(sales1, sales2, sales3, sales4);
    //getSales(sales1, sales2, sales3, sales4);
    highestSales = findHighest(sales1, sales2, sales3, sales4);
    displayOutput(highestSales, averageSales);
    system("PAUSE");
    return 0;
}

错误:

error::::    In function `int main()': 
Examples_C++question_4a.cpp 5 C:Examples_C++C [Error] too few arguments to function void getSales(float&, float&, float&, float&)' 
Examples_C++question_4a.cpp 41 C:Examples_C++C [Error] at this point in file 
Examples_C++question_4a.cpp 41 C:Examples_C++C [Error] void value not ignored as it ought to be
Examples_C++question_4a.cpp 5 C:Examples_C++C [Error] too few arguments to function `void getSales(float&, float&, float&, float&)' 
Examples_C++question_4a.cpp 42 C:Examples_C++C [Error] at this point in file 
Examples_C++question_4a.cpp 42 C:Examples_C++C [Error] void value not ignored as it ought to be 
Examples_C++question_4a.cpp 5 C:Examples_C++C [Error] too few arguments to function `void getSales(float&, float&, float&, float&)' 
Examples_C++question_4a.cpp 43 C:Examples_C++C [Error] at this point in file 
Examples_C++question_4a.cpp 43 C:Examples_C++C [Error] void value not ignored as it ought to be 
Examples_C++question_4a.cpp 5 C:Examples_C++C [Error] too few arguments to function `void getSales(float&, float&, float&, float&)' 
Examples_C++question_4a.cpp 44 C:Examples_C++C [Error] at this point in file 
Examples_C++question_4a.cpp 44 C:Examples_C++C [Error] void value not ignored as it ought to be 

在本节:

sales1 = getSales();
sales2 = getSales();
sales3 = getSales();
sales4 = getSales();

你正在使用一个名为(getSales)的函数,它应该有这个签名:getSales(float&, float&, float&, float&)。这意味着该函数需要 4 个参数,而您给它 0。

这是您的函数定义:

void getSales(float & sales1P, float & sales2P, float & sales3P, float & sales4P)

这是你如何称呼它:

sales1 = getSales();
sales2 = getSales();
sales3 = getSales();
sales4 = getSales();

您是否看到定义与其调用方式之间的脱节?

你需要用 4 个浮点地址调用 getSales()

编辑:由于OP似乎在理解它时遇到了问题...

#include<iostream>
using namespace std;
// You must add the three functions here
//changed these to take pointers to floats
void getSales(float *sales1P, float *sales2P, float *sales3P, float *sales4P) {
cout << "Enter four sales values: " <
//Changed the cin to store in the content of pointers
cin >> *sales1P >> *sales2P >> *sales3P >> *sales4P; }
float calcAverage(float sales1P, float sales2P, float sales3P, float sales4P) {
return (sales1P + sales2P + sales3P + sales4P) / 4; }
float findHighest( float sales1P, float sales2P, float sales3P, float sales4P)
{
float highest = sales1P;
if (sales2P > highest)
    highest = sales2P;
if (sales3P > highest)
    highest = sales3P;
if (sales4P > highest)
    highest = sales4P;
//You were for some reason missing the return statement on highest here. 
return highest;
}
void displayOutput(float highestSales, float averageSales) { cout << "The highest sales         figure is " << highestSales  << " with an average of " << averageSales <<endl;
}
int main()
{
float sales1,sales2,sales3,sales4;
float averageSales;
float highestSales;
//for (int i = 0; i < 4; i++)
// Get the sales for each division.
//The For loop here is useless. Call the function with the addresses of the float values. 
getSales(&sales1, &sales2, &sales3, &sales4);

averageSales = calcAverage(sales1, sales2, sales3, sales4);

highestSales = findHighest(sales1, sales2, sales3, sales4);
displayOutput(highestSales, averageSales);
system("PAUSE");
return 0;
}

阅读指针对您很有用。

  1. 这里的 for 循环是无用的
  2. 我更改了定义以接受指向浮点数的指针
  3. 我将浮点地址作为参数传递
  4. 你错过了 getHighest() 函数中的返回语句。
  5. 此外,您正在为 getSales 的结果分配一些内容,但 getSales 返回无效

我建议你回去学习函数和分配的基础知识。

祝你今天开心!

// getSales(sales1, sales2, sales3, sales4);

没错。为什么要注释掉它并直接反对您定义的函数的参数计数?

此外,您的for循环可能没有做您认为它正在做的事情。你忘了{}.您现在拥有的内容将仅执行第一行 4 次。