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

时间:2012-09-23 16:25:54

标签: c++

我在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 

3 个答案:

答案 0 :(得分:2)

在此部分:

sales1 = getSales();

sales2 = getSales();

sales3 = getSales();

sales4 = getSales();

你正在使用一个名为(getSales)的函数,该函数应该有这个签名:getSales(float&amp;,float&amp;,float&amp;,float&amp;)。这意味着该函数需要4个参数,并且您将其赋予0。

答案 1 :(得分: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;

}

阅读pointers对您有用。

  1. 这里的for循环是无用的
  2. 我将定义更改为接受指向浮动的指针
  3. 我将浮动地址作为参数传递
  4. 您错过了getHighest()函数中的return语句。
  5. 此外,您正在为getSales的结果分配一些内容,但getSales返回 void
  6. 我建议你回去学习功能和作业的基础知识。

    祝你有个美好的一天!

答案 2 :(得分:0)

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

这是对的。你为什么要将它注释掉并直接反对你定义的函数的参数计数?

此外,您的for循环可能没有按照您的想法执行。你忘记了{}。你现在拥有的东西将只执行 第一行4次。