我正在将一个函数中的2d传递给程序中的所有其余函数。出于某种原因,当我在我的一个函数中输出时,内容似乎发生了变化,虽然它在其它两个函数中输出正常。我相信我传递数组的方式和其他函数一样,所以很令人费解内容正在改变我。此函数与成功传递给数组的其他函数之间的唯一区别是,此函数是从函数内而不是从函数中调用的。有什么关心可以提供一些可能发生的事情?
标题
#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:(数组传递给此函数,因为此函数中调用的函数使用数组。数组在此函数中打印的内容与填充的函数中的内容相同in,readFile)
#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;
}
答案 0 :(得分:2)
你在主要声明飞机[] []。然后你在readFile中重新声明它,它实际上隐藏了你的第一个声明。数组在堆栈中声明,当你的程序从readFile返回时,你刚刚填充的数组将被销毁,你将得到包含giberish的原始数组。这就是你打印不同东西的原因。仅使用一个阵列。不要在readFile函数中重新声明它。
答案 1 :(得分:2)
您实际上并没有将数组传递给任何函数。您的声明“int [] [6]”声明该函数采用具有六列的二维数组,但由于您没有给它命名,因此不能在函数中使用它。您声明的飞机变量是每个函数中的本地完全独立的变量。试着写你的功能
void findSeats(int& FC_Row, int& FC_Col, int& EconRow, int& EconCol, int& ticketNum, int& rowNum, char& ticketType, char& seatType, int airplane[][6])
{
int a,b;
}
在每个功能中进行相同的更改。
答案 2 :(得分:0)
你在谈论你的airplane
阵列吗?
你没有传递它,你在每个函数中声明它。
答案 3 :(得分:0)
为确保您的函数不会修改通过引用传递的输入参数,请使用const
。
例如
const int& someArray[]
或类似的应该禁止脚本执行此操作(如果不打算将其更改,可能会抛出编译时错误。)
的问候,
丹尼斯M。