试图将数组作为参数传递得到双[4]到双转换错误

时间:2011-10-08 00:52:18

标签: c++ arrays double

程序应该使用函数来获取有关四个区域销售数据中每一个的输入,然后确定最高值并显示两者。一切都编译得很好,直到我写了最后一个函数FindHighest。我试图传递销售数组,这是我从GetSales收集到FindHighest的数据,并确定数组和cout信息的最大数量。

编译时遇到的错误是 错误1错误C2664:'FindHighest':无法将参数1从'double [4]'转换为'double'g:\ cis5 \ week6 \ week6 \ problem3.cpp 31 1周6

以下是代码:

#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;
    }
}

2 个答案:

答案 0 :(得分:3)

您的问题出现在您声明的顶部 void FindHighest(double);

这与FindHighest的定义不一致。

答案 1 :(得分:2)

您忘了提到参数是函数声明中的数组

void FindHighest(double);

尝试制作

void FindHighest(double[]);