开发者

Trying to pass an array as an argument getting a double[4] to double conversion error

开发者 https://www.devze.com 2023-04-11 08:07 出处:网络
The program is supposed to use functions to get input about each of the four regions sales figures then determine the highest and display both. Everything compiled fine until I wrote the last function

The program is supposed to use functions to get input about each of the four regions sales figures then determine the highest and display both. Everything compiled fine until I wrote the last function FindHighest. I am trying to pass the sales array which is the data I collected from GetSales to FindHighest and determine the largest number of the array and cout that info.

The error I get when compiling is Error 1 error C2664: 'FindHighest' : cannot convert parameter 1 from 'double [4]' to 'double' g:\cis5\week6\week6\problem3.cpp 31 1 Week6

Here is the code:

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


You problem is up at the top where you declare void FindHighest(double);

That doesn't agree with the definition of FindHighest.


You forgot to mention that the parameter is an array in the function declaration

void FindHighest(double);

try making that

void FindHighest(double[]);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号