簡體   English   中英

將C ++數組傳遞給函數

[英]Passing C++ array to function

我不知道如何將sales[]數組傳遞給BubbleSort()函數。 我收到的錯誤是未在此范圍內聲明銷售,就像它是未聲明的變量一樣。 那是我遇到的所有唯一問題。 如果我不調用該函數,則該程序可以正常運行。

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

首先,你聲明變量salesif -塊,而你叫BubbleSort的這個之外if嵌段。 因此,該變量超出范圍,無法用於調用。

此外,請注意, void BubbleSort(int arr[], int n)需要整數數組,而salesdouble sales[arraySize]精度數組,即double sales[arraySize]

更正了范圍問題后BubbleSort(sales, arraySize)調用BubbleSort(sales, arraySize)應該會產生編譯器錯誤。

您已經在“ if”塊中聲明了數組。 它僅存在於此。 此外,“ sales”是雙精度數組,但是您的函數需要一個整數數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM