简体   繁体   English

将C ++数组传递给函数

[英]Passing C++ array to function

I cannot figure out how to pass the sales[] array to the BubbleSort() function. 我不知道如何将sales[]数组传递给BubbleSort()函数。 The error I'm getting is that sales was not declared in this scope, like it's an undeclared variable. 我收到的错误是未在此范围内声明销售,就像它是未声明的变量一样。 That's the only issue I'm having with everything. 那是我遇到的所有唯一问题。 If I don't call the function, the program works fine. 如果我不调用该函数,则该程序可以正常运行。

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void BubbleSort(int arr[], int n)
{
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
            if (arr[j] > arr[j + 1])
            {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
}

int main() {

    //Variables
    //String to hold user data file
    string fileName;
    //Double to hold calculated sales average
    double averageSales;
    // Double to hold total sales
    double totalSales;
    // Int to read array size
    int arraySize;
    // Double to hold highest sales value
    double highest = 0;
    // Double to hold lowest sales value
    double lowest;
    // Char to hold rerun choice, default value is no
    char rerun = 'n';

    //Do / While loop for rerun choice
    do {

        //Get File Name from User
        cout << "Input Filename to read:" << endl;
        cin >> fileName;
        cout << "File Name: " << fileName << endl;

        // Open files stream
        ifstream file;
        file.open(fileName.c_str());

        //Check File Steam
        if (file.is_open()) {
            // Read arraySize Variable from first line of file
            file >> arraySize;

            // Create array from arraySize variable; read from file
            double sales[arraySize];

            // Read data into array
            for (int i = 0; i<arraySize; i++) {
                file >> sales[i];
            }

            // Find Total Sales & Average
            for (int i = 0; i<arraySize; i++) {
                totalSales += sales[i];
            }
            averageSales = totalSales / arraySize;

            //Find largest Sales
            for (int i = 0; i<arraySize; i++) {
                if (sales[i] > highest) {
                    highest = sales[i];
                }
            }

            //Find lowest Sales - set default lowest value to the highest value
            lowest = highest;
            for (int i = 0; i<arraySize; i++) {
                if (sales[i] < lowest) {
                    lowest = sales[i];
                }
            }

            //Close File stream
            file.close();
        }
        else {
            cout << "Error Opening File" << endl;
        }

        //Sort Array
        BubbleSort(sales, arraySize);

        //Output Sales data
        cout << "Total Sales: " << totalSales << endl;
        cout << "Average Sales: " << averageSales << endl;
        cout << "Highest Sales amount: " << highest << endl;
        cout << "Lowest Sales Amount: " << lowest << endl;

        //Choice to rerun
        cout << endl << "Would you like to run again? Y/N " << endl;
        cin >> rerun;
    } while (rerun == 'y' || rerun == 'Y');
}

First, you declare variable sales inside an if -block, whereas you call BubbleSort outside of this if -block. 首先,你声明变量salesif -块,而你叫BubbleSort的这个之外if嵌段。 Hence, the variable is out of scope and cannot be used for the call. 因此,该变量超出范围,无法用于调用。

Further, note that void BubbleSort(int arr[], int n) expects an integer array, while sales is an array of doubles, ie double sales[arraySize] . 此外,请注意, void BubbleSort(int arr[], int n)需要整数数组,而salesdouble sales[arraySize]精度数组,即double sales[arraySize]

Calling BubbleSort(sales, arraySize) should yield a compiler error once you have corrected the scope issue. 更正了范围问题后BubbleSort(sales, arraySize)调用BubbleSort(sales, arraySize)应该会产生编译器错误。

You have declared your array inside 'if' block. 您已经在“ if”块中声明了数组。 It exists only there. 它仅存在于此。 Moreover, 'sales' is array of doubles, but your function expects an array of integers. 此外,“ sales”是双精度数组,但是您的函数需要一个整数数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM