【发布时间】:2011-03-13 08:40:40
【问题描述】:
我正在将一个填充了一个函数的 2d 传递给我程序中的所有其余函数。出于某种原因,当我在其中一个函数中输出时,内容似乎发生了变化,尽管它在它所在的其他两个函数中输出良好。我相信我传递数组的方式与其他函数完全相同,所以令人费解内容在我身上发生了变化。此函数与成功传递数组的其他函数之间的唯一区别是,此函数是从函数内部调用的,而不是从 main 调用的。是否愿意提供一些关于可能发生的事情的见解?
标题:
#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cctype>
#include <cstdlib>
#include <string>
using namespace std;
void extern readFile(ifstream&, int&, int&, int&, int&, int[][6]);
void extern userInput(int&, int&, int&, int&, int&, int&, char&, char&, int[][6]);
void extern findSeats(int&, int&, int&, int&, int&, int&, char&, char&, int[][6]);
#endif // HEADER_H_INCLUDED
主要:
#include "header.h"
int main()
{
ifstream inFile;
int FC_Row, FC_Col, EconRow, EconCol, ticketNum, rowNum;
int airplane[100][6];
char ticketType, seatType;
cout << setw(48) << "Thank you for choosing Cheeta Airlines!" << '\n' << '\n' << endl;
ifstream inData;
inData.open("Airplane.txt");
if (!inData)
{
cout << "Cannot open the input file."
<< endl;
return 1;
}
readFile(inFile, FC_Row, FC_Col, EconRow, EconCol, airplane);
userInput(FC_Row, FC_Col, EconRow, EconCol, ticketNum, rowNum, ticketType, seatType, airplane);
}
readFile:(数组被填充然后在这个函数中打印)
#include "header.h"
void readFile(ifstream& inFile, int& FC_Row, int& FC_Col, int& EconRow, int& EconCol, int[][6])
{
int a, b;
int airplane[100][6];
inFile.open("Airplane.txt");
inFile >> FC_Row >> FC_Col >> EconRow >> EconCol;
for (a = 0; a < FC_Row; a++)
for (b = 0; b < FC_Col; b++)
inFile >> airplane[a][b] ;
for (a = 0; a < EconRow; a++)
for (b = 0; b < EconCol; b++)
inFile >> airplane[a + FC_Row][b] ;
cout << setw(11)<< "A" << setw(6) << "B"
<< setw(6) << "C" << setw(6) << "D"
<< setw(6) << "E" << setw(6) << "F" << endl;
cout << " " << endl;
cout << setw(30) << "First Class: $2,000" << endl;
cout << '\n';
for (a = 0; a < FC_Row; a++)
{
cout << "Row " << setw(2) << a + 1 << ":";
for (b = 0; b < FC_Col; b++)
cout << setw(5) << airplane[a][b] << " ";
cout << endl;
}
cout << '\n';
cout << setw(30) << "Economy Class: $750" << endl;
cout << '\n';
for (a = FC_Row; a < (EconRow + FC_Row); a++)
{
cout <<"Row " << setw(2)<< a + 1 << ":";
for (b = 0; b < EconCol; b++)
cout << setw(5) << airplane[a][b] << " ";
cout << endl;
}
}
userInput: (数组被传递给这个函数是因为在这个函数中调用的函数使用了这个数组。这个数组在这个函数中打印,其中的内容与它填充的函数中的内容相同在,读取文件)
#include "header.h"
void userInput(int& FC_Row, int& FC_Col, int& EconRow, int& EconCol, int& ticketNum, int& rowNum, char& ticketType, char& seatType, int[][6])
{
char reply;
int airplane[100][6];
cout << '\n' << "* The chart above is a seating diagram for your flight. 1 indicates that the " << '\n' << " seat is taken, 0 indicates that the seat is free.";
cout << " Refer to this chart to help you make your class and seating selection." << endl;
cout << '\n';
cout << "- How many tickets will you be purchasing today?" << endl;
cin >> ticketNum;
while (!cin)
{
cin.clear();
while(cin.get()!='\n');
cout << "INVALID DATA!!!!!" << endl;
cout << "Number of tickets must be a NUMBER." << endl;
cout << "try again:" << endl;
cin >> ticketNum;
}
while (ticketNum > ((FC_Row * FC_Col) + (EconRow * EconCol)))
{
cin.clear();
while(cin.get()!='\n');
cout << "The number of tickets you need exceeds the amount of seats we have on that plane! please choose a different amount of tickets!"
<<"[ or a different airline :( ]" << endl;
cin >> ticketNum;
}
while (ticketNum < 1)
{
cin.clear();
while(cin.get()!='\n');
cout << "The number of tickets must be greater than 0." << endl;
cout << "try again:" << endl;
cin >> ticketNum;
}
for (int j = 0; j < ticketNum; j++)
{
cout << '\n' << "Ticket # " << j +1 << " selection:" << endl;
cout << "--------------------------------"<< endl;
cout << "- Ticket type? First class or Economy class (enter F/E)" << endl;
cin >> ticketType;
ticketType = toupper(ticketType);
while (ticketType != 'F' && ticketType != 'E')
{
cin.clear();
while(cin.get()!='\n');
cout << "INVALID DATA!!!!!" << endl;
cout << "Please indicate your ticket type by entering either F (for First Class) " << '\n' << "or E (for Economy Class)" << endl;
cout << "try again:" << endl;
cin >> ticketType;
}
cout << "- preferred seat type? Window, Aisle, or No preference (enter W/A/N)" << endl;
cin >> seatType;
seatType = toupper(seatType);
while (seatType != 'W' && seatType != 'A' && seatType != 'N')
{
cin.clear();
while(cin.get()!='\n');
cout << "INVALID DATA!!!!!" << endl;
cout << "Please indicate your preferred seat type by entering either W (for Window seat), " << '\n' << "A (for Aisle seat), or N (for No preference)" << endl;
cout << "try again:" << endl;
cin >> seatType;
}
cout << "- row number? 1-" << (FC_Row) << " in First Class," << (FC_Row+1) << "-" << (FC_Row + EconRow) << " in Economy Class" << endl;
cin >> rowNum;
while (rowNum > (FC_Row + EconRow))
{
cin.clear();
while(cin.get()!='\n');
cout << "Your row number exceeds" << (FC_Row + EconRow) << endl;
cout << "try again:" << endl;
cin >> rowNum;
}
while (rowNum < 1)
{
cin.clear();
while(cin.get()!='\n');
cout << "The row number must be greater than 0." << endl;
cout << "try again:" << endl;
cin >> rowNum;
}
if (ticketType == 'F')
{
while (rowNum > (FC_Row))
{
cout << "That row is not located in our first class section. Would you like to change your class so you can sit in that row (Y/N)?" << endl;
cin >> reply;
reply = toupper(reply);
while (reply != 'Y' && reply != 'N')
{
cin.clear();
while(cin.get()!='\n');
cout << "Please indicate your answer with Y (yes) or N (no)." << endl;
cout << "try again:" << endl;
cin >> reply;
reply = toupper(reply);
}
if (reply == 'Y')
{
ticketType = 'E';
break;
}
else
{
cout << "Then choose a row numbered 1-" << (FC_Row) << endl;
cin >> rowNum;
}
}
}
if (ticketType == 'E')
{
while (rowNum <= (FC_Row))
{
cout << "That row is not located in our economy class section. Would you like to change your class so you can sit in that row (Y/N)?" << endl;
cin >> reply;
reply = toupper(reply);
while (reply != 'Y' && reply != 'N')
{
cin.clear();
while(cin.get()!='\n');
cout << "Please indicate your answer with Y (yes) or N (no)." << endl;
cout << "try again:" << endl;
cin >> reply;
reply = toupper(reply);
}
if (reply == 'Y')
{
ticketType = 'F';
break;
}
else
{
cout << "Then choose a row numbered " << (FC_Row + 1) << "-" << (FC_Row + EconRow) << endl;
cin >> rowNum;
}
}
}
findSeats(FC_Row, FC_Col, EconRow, EconCol, ticketNum, rowNum, ticketType, seatType, airplane);
}
}
findSeats:(这是数组似乎更改其内容的函数。我知道我正在正确打印数组,因为我使用的代码与我在它在其他功能中正确打印。)
#include "header.h"
void findSeats(int& FC_Row, int& FC_Col, int& EconRow, int& EconCol, int& ticketNum, int& rowNum, char& ticketType, char& seatType, int[][6])
{
int airplane[100][6], a, b;
}
【问题讨论】:
-
您真的应该从上一个问题中听取@dribeas 的建议。不要通过头文件中的“使用”来污染 std 命名空间的所有内容。
-
为什么不呢?我的理解是 using namespace std;使用,所以我不必在所有 cin 和 cout 语句之前输入 std:: 或类似的东西。
-
我把这些问题弄糊涂了……抱歉。您应该使用“使用命名空间 std”或任何其他命名空间,但在源文件中。将它放在头文件中是不好的做法,因为您将强制包括您的头在内的任何人使用命名空间 std。
-
好东西我的教授教把它放在标题中,嗯? -_-
-
不知道你的教授这样做的原因。只需在stackoverflow上询问社区stackoverflow.com/questions/1452721/…stackoverflow.com/questions/1265039/using-std-namespace