正在获取另一个函数作用域中的参数值

Getting parameter values in the scope of another function

本文关键字:参数 作用域 函数 获取 另一个      更新时间:2023-10-16

我不知道如何将读取输入的变量放入计算席位的范围。不管我如何声明变量,而不是重新定义它们;函数calculateSeats似乎找不到行、列和数组seatingChart。

*添加了calculateSeats(seatingChart[行][列])*

int main() {
    int seatsSold = 0, oneSeat = 0, seatsTogether = 0, threeSeats = 0, fourSeats = 0, fiveSeats = 0, noSeats = 0, totalSold = 0, 
    rows, columns, pecentageSold, i;
    const int SIZE = 50;
    char seatingChart[SIZE][SIZE];
    readInput(rows, columns, seatingChart[rows][columns]); // Call read input.
    // Values of read input are then passed to calculate values. 
    calculateSeats(seatsSold, oneSeat, seatsTogether, threeSeats, fourSeats, fiveSeats, noSeats, i);
    // Calculated valus are then declared as calculated values and passed to write output. 
    /* int calculatedValues = calculateSeats(seatsSold, oneSeat = 0, seatsTogether = 0, threeSeats = 0, 
                                          fourSeats = 0, fiveSeats = 0, noSeats = 0); */
    // Function is called to write the output.
    writeOutput(seatsSold, oneSeat, seatsTogether, threeSeats, fourSeats, fiveSeats, noSeats, pecentageSold, totalSold); 
    return 0; // Program ends.
}
// Function is needed for assigning values to the char array. Values are then passed from read to calculate. 
int readInput(int & rows, int & columns, int i) {
    const int SIZE = 50;
    int seatingChart[SIZE][SIZE];
    ifstream inputFile;
    inputFile.open("SeatingChart.txt");
    for (rows; rows < SIZE; rows++) { // Step through the valueless array and give the array values.
        for (columns; columns < SIZE; columns++)
            inputFile >> seatingChart[rows][columns]; // Assign the array values from the input file. 
    }
    inputFile.close();
    calculateSeats(seatingChart[rows][columns]);
    return 0;
}
// Function is needed just for calculations. Values are then passed from calculate to write output. 
void calculateSeats(int & seatsSold, int & oneSeat, int & seatsTogether, int & threeSeats, 
                    int & fourSeats, int & fiveSeats, int & noSeats, int & sixSeats) {
    for (int count = 0; count < rows; count++) { // Step back through the array with loaded values. 
        for (int num = 0; num < columns; num++) {
            // If equal to A and count is equal to count++. Then consecutive read chars is true.
            if (seatingChart[count][num] == 'A' && seatingChart[count][num] == seatingChart[count][num]++) {
                seatsTogether++;
                    if (seatsTogether > 1) {
                        threeSeats++;
                    if (seatsTogether > 2) {
                        fourSeats++;
                    }
                    if (seatsTogether > 3) {
                        fiveSeats++;
                    }
                    if (seatsTogether > 4) {
                        sixSeats++;
                    }
                }
            }
            else {
                seatsSold++;
                cout << "Total seats sold: " << seatsSold << endl;
                if (seatsSold == 6) {
                    cout << "Rows with no seats available: " << "row" << seatingChart[count] << endl;
                }
            }
        }
    }
}

您正在main的范围中声明行、列和座位表。这些变量位于堆栈上。如果你想让它们在readInput中可见,你必须将它们声明为全局变量。所以它应该是这样的。

int row, column;
char seatingChart[SIZE][SIZE];
int main()
{   
//your code
}

全球化被认为是糟糕的风格。http://en.wikipedia.org/wiki/Global_variable