1

I am passing a 2d that is filled in one function to all the rest of the functions in my program. For some reason the contents seem to change when I output in one of my functions although it outputs fine in the two other functions it is in. I believe I am passing the array the same exact way as I am the other functions so it is puzzling that the contents are changing on me. The only difference between this function and the other functions that the array is successfully passed to is that this function is called from within a function rather than from main. Any care to provide some insight on what might be going on?

header:

#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

main:

#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: (the array is filled and then printed in this function)

#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: (The array is passed to this function because a function called in this function uses the array. The array prints in this function with the same contents in it as in the function it is filled 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: (This is the function that the array seems to change its contents in. I know I am printing the array correctly because I am using the exact same code to print it as I use when it correctly prints in the other functions.)

#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;  
}
darko
  • 2,438
  • 8
  • 42
  • 54
  • You should really take the advice of @dribeas from your previous question. Don't pollute everything with the std namespace by way of "using" in the header file. – celavek Mar 13 '11 at 00:40
  • Why not? My understanding is that using namespace std; is used so I do not have to type std:: or something of the sort before all my cin and cout statements. – darko Mar 13 '11 at 00:46
  • I confused the questions ... Sorry about that. You should use "using namespace std" or any other namespace for that matter, but in a source file. It's bad practice to put it in the header file, as you'll force anyone including your header to use the namespace std. – celavek Mar 13 '11 at 00:52
  • Good thing my professor teaches putting it in the header then huh? -_- – darko Mar 13 '11 at 01:11
  • Don't know about the reasons your professor does that for. Just ask the community here on stackoverflow http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-a-bad-practice-in-c http://stackoverflow.com/questions/1265039/using-std-namespace – celavek Mar 13 '11 at 08:13

4 Answers4

2

You declare airplane[][] in the main. Then you re-declare it in readFile, which actually hides your first declaration. The array is declared on the stack and when your program returns from readFile the array you just filled will be destroyed and you'll end up with the original array which contains giberish. That's why you print different things. Use only one array. Don't redeclare it in the readFile function.

celavek
  • 5,575
  • 6
  • 41
  • 69
  • celavek, thanks for the answer. If I do not have airplane declared in the readFile function ( and the other functions as well) I get an error saying that airplane is undeclared. – darko Mar 13 '11 at 00:43
  • @Matt correct the function signatures(see answer by John Gordon). It doesn't have to be necessarily airplane, the name of the function parameter for the 2d array in readFile() for example - don't confuse the function parameters with the actual arguments you pass in. – celavek Mar 13 '11 at 00:47
2

You're not actually passing your array to any of the functions. Your declaration "int[][6]" declares that the function takes a two-dimensional array with six columns, but because you don't give it a name, it can't be used in the function. The airplane variable you declare is a local, completely separate variable in each function. Try writing you function as

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;
}

making the same change in each function.

John Gordon
  • 2,576
  • 3
  • 24
  • 29
  • John, thanks for the answer. If I do not have airplane declared in the readFile function ( and the other functions as well) I get an error saying that airplane is undeclared – darko Mar 13 '11 at 00:44
0

Are you talking about your airplane array?

You're not passing it around, you're declaring it in every function.

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
0

To be sure that your functions will not modify input parameters which are passed by reference, use const.

For example

const int& someArray[]

or similar should disallow the script from doing this (may throw a compile-time error at where its being changed if its not intended to).

Regards,
Dennis M.

RageD
  • 6,693
  • 4
  • 30
  • 37