-6

我有这个程序在 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 
4

3 回答 3

2

在本节:

sales1 = getSales();

sales2 = getSales();

sales3 = getSales();

sales4 = getSales();

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

于 2012-09-23T16:28:19.687 回答
1

这是您的函数定义:

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() 函数中的 return 语句。
  5. 此外,您正在为 getSales 的结果分配一些东西,但 getSales 返回void

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

祝你有美好的一天!

于 2012-09-23T16:39:07.823 回答
0
// getSales(sales1, sales2, sales3, sales4);

这是正确的。你为什么把它注释掉,直接反对你定义的函数的参数计数?

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

于 2012-09-23T16:32:44.673 回答