显示最高销售数字和平均销售额
displays highest sales figure and average sales
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;
}
阅读指针对您很有用。
- 这里的 for 循环是无用的
- 我更改了定义以接受指向浮点数的指针
- 我将浮点地址作为参数传递
- 你错过了 getHighest() 函数中的返回语句。
- 此外,您正在为 getSales 的结果分配一些内容,但 getSales 返回无效。
我建议你回去学习函数和分配的基础知识。
祝你今天开心!
// getSales(sales1, sales2, sales3, sales4);
没错。为什么要注释掉它并直接反对您定义的函数的参数计数?
此外,您的for
循环可能没有做您认为它正在做的事情。你忘了{}
.您现在拥有的内容将仅执行第一行 4 次。
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 检查输入是否不是整数或数字
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 如何用数字处理log(0)
- 最高有效数字侧的第N位
- 如何获取一个数字的前3位
- 查找最接近的大于当前数字的数字的索引
- 找到两对数字,使它们的乘积的绝对差最小化
- 我想做一个彼此不同但重复出现的数字
- 将数字转换为字母(例如:123 转换为一二三)
- C++如何计算用户输入的数字中的偶数位数
- 如何在C++中确定文本文件中的元素是字符还是数字
- 打印数字图案
- C++问题:用户认为数字1-100,程序提出问题不超过6次即可得到答案。无法正确
- 如何检查一个c++字符串中有多少相同的字符/数字
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 将数字打印成文字
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 显示最高销售数字和平均销售额