2017-04-11 109 views
0

我無法弄清楚如何將sales[]數組傳遞給BubbleSort()函數。我得到的錯誤是銷售未在此範圍內聲明,例如它是未聲明的變量。這是我遇到的唯一問題。如果我沒有調用這個函數,程序工作正常。將C++數組傳遞到函數

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

不一致的縮進將阻止用戶閱讀您的示例。 –

+2

不應該這樣:BubbleArray(sales,arraySize);是這個BubbleSort(sales,arraySize); ? – victor

+5

'double sales [arraySize];'不是可移植的C++。數組的大小必須是編譯時間常量。一些編譯器允許通過擴展,但它不是標準的。考慮'std :: vector sales(arraySize);'而是。 –

回答

4

首先,聲明變量salesif - 塊裏面,而你打電話BubbleSort超出這個if - 阻塞的。因此,該變量超出了範圍,不能用於呼叫。

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

調用BubbleSort(sales, arraySize)應該會在您糾正範圍問題後產生編譯器錯誤。

0

你已經在'if'塊中聲明瞭你的數組。它只存在於那裏。此外,'銷售'是雙數組,但你的函數需要一個整數數組。