0

I cannot figure out how to pass the sales[] array to the BubbleSort() function. 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');
}
10
  • 2
    Shouldnt this: BubbleArray(sales, arraySize); be this BubbleSort(sales, arraySize); ? Commented Apr 11, 2017 at 19:32
  • 5
    double sales[arraySize]; is not portable c++. The size of an array must be a compile time constant. Some compilers allow it through extensions but it is not standard. Consider std::vector<double> sales(arraySize); instead. Commented Apr 11, 2017 at 19:33
  • 2
    sales is declared inside your if (file.is_open()) block and is only valid inside it. Also, double sales[arraySize]; is not valid C++ when arraySize is not a constant expression, although some compilers accept it as an extension. Use std::vector instead of an array. Commented Apr 11, 2017 at 19:34
  • 2
    you need to read about scope. The array is indeed not declared in the scope you are trying to use it. Commented Apr 11, 2017 at 19:37
  • 1
    @JasonBrown, just as a tip, if you are using C++ study the STL. check std::vector it will help you A LOT; Commented Apr 11, 2017 at 19:42

2 Answers 2

4

First, you declare variable sales inside an if-block, whereas you call BubbleSort outside of this if-block. 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, i.e. double sales[arraySize].

Calling BubbleSort(sales, arraySize) should yield a compiler error once you have corrected the scope issue.

Sign up to request clarification or add additional context in comments.

Comments

0

You have declared your array inside 'if' block. It exists only there. Moreover, 'sales' is array of doubles, but your function expects an array of integers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.