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


}
darko
  • 2,438
  • 8
  • 42
  • 54

3 Answers3

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].

Mahesh
  • 34,573
  • 20
  • 89
  • 115
  • mashesh, thanks for the answer. After these changes I am getting an error telling me that airplane is undeclared in my readfile function. – darko Mar 12 '11 at 23:24
  • @Matt - Keeping the error aside, why are you using `extern` in this case. Any reason ? – Mahesh Mar 12 '11 at 23:27
  • 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. – darko Mar 12 '11 at 23:29
  • You can omit variable identifiers from prototype declarations, but not from the implementation signature. – Matheus Moreira Mar 12 '11 at 23:30
  • @mahesh, Hmm, seems this didnt do what I needed exactly. When I output the array in the function its been sent to it is filled with garbage. – darko Mar 12 '11 at 23:37
  • 1
    @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 . – Mahesh Mar 12 '11 at 23:41
  • I am outputting the array in the function with the exact loop I use to output in the readFile function above. The first row is all 0s when it is supposed to be all 1s then the next 50 or so indicies are all 0 as well. (they're supposed to be 0s and 1s in variation) then the rest of the array is garbage (11554, 55454, etc.) – darko Mar 12 '11 at 23:50
  • @Matt - Have you tried my suggestion of printing the elements while reading. This itself shows whether the elements are assigned to array properly or not. Try it in the next for loop bunch also. – Mahesh Mar 12 '11 at 23:55
  • Not as they are read but right after and the data is in the desired indices. Im pretty positive they are assigned properly. The problem is either the data is changing when it is passed or I am not passing it properly to print the same array. – darko Mar 12 '11 at 23:56
  • Adding a cout statement after in the for loop it fills is the only way i can think to output as it fills but when i try to do so by doing this: for (a = 0; a < FC_Row; a++) for (b = 0; b < FC_Col; b++) inFile >> airplane[a][b] ; cout << airplane[a][b]; It only outputs one value – darko Mar 13 '11 at 00:05
  • @Matt - Copy the exact for loop, I suggested in a comment. Also, does your `FC_Row + EconRow` sums to **100**? Is the value of `FC_Col` and `EconCol` equals to **6**? If you are printing entire array ( i.e., **100*6** ), but answer to any of the two questions is NO, that explains the garbage values. – Mahesh Mar 13 '11 at 00:13
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);
Matheus Moreira
  • 17,106
  • 3
  • 68
  • 107
  • "cannot convert `int*' to `int (*)[6]" – darko Mar 12 '11 at 23:14
  • Are you outputting the contents of the array (`cout << array[i][j];`), or the array itself (`cout << array;`)? – Matheus Moreira Mar 12 '11 at 23:39
  • 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. – darko Mar 12 '11 at 23:42
0

These might lead you to the proper solution

Passing multidimensional arrays as function arguments in C

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

Community
  • 1
  • 1
celavek
  • 5,575
  • 6
  • 41
  • 69