正在获取另一个函数作用域中的参数值
Getting parameter values in the scope of another function
我不知道如何将读取输入的变量放入计算席位的范围。不管我如何声明变量,而不是重新定义它们;函数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
相关文章:
- 未在作用域中声明unordered_map
- 有没有一种方法可以在编译时获得作用域类名
- C++quit()函数中可能存在作用域问题
- 未在此作用域OpenCV3.4中声明cvSaveImage
- 全局作用域中函数指针的赋值
- 在类函数中初始化外部作用域变量
- 不同作用域中的静态变量和全局变量
- 是同一作用域的函数部分中的函数调用
- 未在此作用域中声明的函数和变量 (C++)
- 类作用域的类型别名"using":[何时]方法中的用法可以先于类型别名?
- 将作用域枚举转换为基础类型
- 表达式必须具有完整或无作用域的枚举图
- 在构造函数中输入对象时C++类成员作用域
- 无法让"std::enable_if"适用于无作用域枚举
- 将非左值作为常量引用参数传递.临时是在本地作用域还是在调用方作用域中创建的?
- 创建从外部作用域重新声明模板参数的typedef的负面影响
- 当我把一个方法作为参数传递时,我怎么能忽略作用域呢
- C/ c++中数组的参数传递和作用域
- 在包含作用域的函数调用期间持续的临时函数参数
- 函数参数名称的作用域