该程序应该使用函数来获取四个地区销售数据的输入,然后确定最高的并显示两者。一切都编译得很好,直到我编写了最后一个函数 FindHighest。我试图将销售数组(我从 GetSales 收集的数据)传递到 FindHighest 并确定数组的最大数量并计算该信息。
编译时出现的错误是 错误 1 错误 C2664: 'FindHighest' : 无法将参数 1 从 'double [4]' 转换为 'double' g:\cis5\week6\week6\problem3.cpp 31 1 Week6
这是代码:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
double GetSales(string);
void FindHighest(double);
void Validate(double&, string);
const int NUMBER_OF_REGIONS = 4;
const string REGION[NUMBER_OF_REGIONS] = {"Northeast", "Southeast", "Northwest", "Southwest"};
int main(){
double sales[NUMBER_OF_REGIONS] = {0.0};
//This loop calls the function GetSales as it proceeds forward through the REGION array
for (int i = 0; i < 4; i++){
sales[i] = GetSales(REGION[i]);
}
FindHighest(sales);
cin.ignore();
cin.get();
return 0;
}
//This function receives the region as a string and gathers the sales figure as input. It also calls the function Validate to vaildate that the sales figure is over $0.0
double GetSales(string region){
double sales = 0.0;
cout << "\nWhat is the total sales for " << region << " division: ";
cin >> sales;
Validate(sales, region);
return sales;
}
//This function receives the sales figures as an array and determines which division had the highest sales and displays the name and amount
void FindHighest(double sales[]){
string region = "";
double highestSales = 0.0;
for (int i = 0; i < NUMBER_OF_REGIONS; i++){
if (sales[i] > highestSales){
highestSales = sales[i];
region = REGION[i];
}
}
cout << fixed << showpoint << setprecision(2);
cout << "The " << region << " division had the highest sales with a total of $" << highestSales;
}
//This function validates the sales input from the GetSales function
void Validate(double &sales, string region){
while (sales < 0){
cout << "I am sorry but this cannot be a negative number." << endl;
cout << "Please enter a positive sales figure for " << region << " division: ";
cin >> sales;
}
}
最佳答案
您的问题位于您声明的顶部
void FindHighest(double);
这与 FindHighest 的定义不符。
关于c++ - 尝试将数组作为参数传递,出现 double[4] 到 double 转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7694084/