1

I am having trouble figuring out how to pass a 2d array as a parameter. The C++ manual and other examples i'm finding aren't helping me much so maybe someone can help me understand more if they take a look at my code. Thanks

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[100][6]);
userInput(FC_Row, FC_Col, EconRow, EconCol, ticketNum, rowNum, ticketType, seatType,     airplane[100][6]);`

}

readFile:

#include "header.h"

void readFile(ifstream& inFile, int& FC_Row, int& FC_Col, int& EconRow, int& EconCol, int airplane[][6])
{
    int a, b;

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


}

3 Answers 3

3

If you need to pass a 2D array -

void extern readFile(ifstream&, int&, int&, int&, int&, int[][6]);

void readFile(ifstream&, int&, int&, int&, int&, int[][6])
{
     // ....

}

And the call should be -

readFile(inFile, FC_Row, FC_Col, EconRow, EconCol, airplane);

When you say -

readFile(inFile, FC_Row, FC_Col, EconRow, EconCol, airplane[100][6]);

You are actually passing the value at that position ( i.e., int ). But the function expects a int [][6].

Sign up to request clarification or add additional context in comments.

11 Comments

mashesh, thanks for the answer. After these changes I am getting an error telling me that airplane is undeclared in my readfile function.
@Matt - Keeping the error aside, why are you using extern in this case. Any reason ?
Extern is used because all functions are located in different .cpp file. the program runs after I redeclare int airplane[100][6] in each function.
You can omit variable identifiers from prototype declarations, but not from the implementation signature.
@Matt - 0k. Will narrow down the mistake. In the beginning of reading loop - for (a = 0; a < FC_Row; a++){ for (b = 0; b < FC_Col; b++){ inFile >> airplane[a][b] ; std::cout << airplane[a][b] << "\t";}} ..Check this whether it is correctly reading or not .
|
1

The last parameter is declared as an int, but a 2D int array is of type int[][depth].

Change the readFile prototype declaration to:

void extern readFile(ifstream&, int&, int&, int&, int&, int[][6]);

Later, when you call the function, you are passing the element inside the array, which is of type int:

readFile(inFile, FC_Row, FC_Col, EconRow, EconCol, airplane[100][6]);

The function is expecting a 2D int array, which is of type int[][6], so you must pass only the variable itself, without any indexes:

readFile(inFile, FC_Row, FC_Col, EconRow, EconCol, airplane);

3 Comments

"cannot convert int*' to int (*)[6]"
Are you outputting the contents of the array (cout << array[i][j];), or the array itself (cout << array;)?
I am outputting array[i][j] in a loop. See the readFile function where it is initially filled and output. In the different function it is the exact code being used.
0

These might lead you to the proper solution

Passing multidimensional arrays as function arguments in C

http://c-faq.com/aryptr/ary2dfunc2.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.