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

C++ 在另一个函数的作用域中获取参数值,c++,C++,我不知道如何将读取输入的变量放入calculate seats的范围。尽管我声明变量时没有重新定义它们;CalculateSets函数似乎找不到行、列和数组座位图 *增加了CalculateSets(座位图[行][列])* int main() { int seatsSold = 0, oneSeat = 0, seatsTogether = 0, threeSeats = 0, fourSeats = 0, fiveSeats = 0, noSeats = 0, totalSold

我不知道如何将读取输入的变量放入calculate seats的范围。尽管我声明变量时没有重新定义它们;CalculateSets函数似乎找不到行、列和数组座位图

*增加了CalculateSets(座位图[行][列])*

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;
                }
            }
        }
    }
}
intmain(){
int seatssell=0,one seat=0,seatsTogether=0,threeSeats=0,fourSeats=0,fiveSeats=0,noSeats=0,totalseed=0,
行、列、pecentageSold、i;
常数int SIZE=50;
char seatingChart[尺寸][尺寸];
readInput(行、列、座位图[行][列];//调用read input。
//然后将读取输入的值传递给计算值。
计算座位数(售出座位、一个座位、共座、三个座位、四个座位、五个座位、一个座位、一个座位);
//然后将计算值声明为计算值,并传递给写入输出。
/*int calculatedValues=CalculateSets(座位售出,一个座位=0,座位合计=0,三个座位=0,
四个座位=0,五个座位=0,五个座位=0)*/
//调用函数来写入输出。
冲销(座位售出、一个座位、共座、三个座位、四个座位、五个座位、鼻子、百分百售出、全部售出);
返回0;//程序结束。
}
//函数用于为字符数组赋值。然后将值从读取传递到计算。
int readInput(int&rows,int&columns,inti){
常数int SIZE=50;
国际航海图[尺寸][尺寸];
ifstream输入文件;
open(“SeatingChart.txt”);
对于(rows;rows>seatingChart[行][列];//从输入文件分配数组值。
}
inputFile.close();
计算组(座位图[行][列]);
返回0;
}
//函数仅用于计算。然后将值从计算传递到写入输出。
无效计算座位(整座和座位已售出、整座和单座、整座和座位在一起、整座和三座、,
内座和四座、内座和五座、内座和鼻座、内座和六座){
对于(int-count=0;count1){
三席++;
如果(坐在一起>2){
四座位++;
}
如果(坐在一起>3){
五吃++;
}
如果(坐在一起>4){
六席++;
}
}
}
否则{
Seatssell++;

cout您正在main范围内声明行、列和座位表。 这些变量位于堆栈上。如果希望它们在readInput中可见,则必须将它们声明为全局变量。因此,应该如下所示

int row, column;
char seatingChart[SIZE][SIZE];

int main()
{   
//your code
}
通配符被认为是不好的风格。

使用C++容器类(如<代码> STD::vector < /代码>)可能会为您省去很多麻烦。因此,我强烈建议您考虑使用这些。此外,所有与座位相关的变量似乎都是连接的,如果是这种情况,那么在代码中使用
struct
对这些变量进行逻辑分组是很有意义的。此外,如果您不更改您认为e作为引用传递,那么您可能应该通过常量引用或者通过值传递它们。为什么向量比数组更有效?归结到程序员的效率,传递向量非常容易,而且您目前在编译程序时遇到了问题。请在您担心原始数组的效率与<>代码> STD::vector < /COD>。这个C++ FAQ条目是我对这件事的想法的好总结:如果您需要在这里创建数组版本,那么您需要从Read Enter返回数组,然后将数组作为指针计算到CaseStEATE函数。(或者,您可以将其设置为全局变量)创建一个类,将数据存储为成员变量,将函数移动为成员函数,然后它们就可以轻松地访问数据。当您花时间识别对象时,面向对象的语言工作得更好。我被告知没有全局变量,但谢谢您。我想我只是还不完全理解传递变量。还有吗我的问题没有简单的解决方法?如果不允许使用全局变量,则必须将它们传递给函数。CalculateSets已经有8个参数,这不是一个好的样式。我建议您声明两个结构,一个集中于seat参数,另一个集中于row、column和seatingChart。我建议您阅读wikipedia art关于结构的问题。