我无法弄清楚如何将读取输入的变量放入计算座位的范围内。尽管我如何声明变量不重新定义它们;函数calculateSeats似乎找不到行,列和数组seatChart。
*添加了calculateSeats(seatingChart [rows] [columns]); *
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;
}
}
}
}
}
答案 0 :(得分:0)
您在主要范围内声明行,列和座位表。 这些变量位于堆栈上。如果您希望它们在readInput中可见,则必须将它们声明为全局变量。所以看起来应该是这样的。
int row, column;
char seatingChart[SIZE][SIZE];
int main()
{
//your code
}
Globals被认为是糟糕的风格。 http://en.wikipedia.org/wiki/Global_variable