I want to pass an input file to a function and within the function, fill an array by reference. It isn't allowing me to do this, it keeps telling me it's expecting a ";" after i in the function. I couldn't really find anything about this in my book - if it can be done and if so, how?

#include <iostream>
#include <fstream>

using namespace std;

// ------ Prototype
void readFile(char [][30], ifstream);


// ****** MAIN ****************************************************************
int main()
{
    char weather[3][30];
    ifstream inputFile("P4.dat");

    return 0;
}

// ------ FUNCTION ------------------------------------------------------------
//
// ----------------------------------------------------------------------------
void readFile(char weather[][30], ifstream inputFile)
{
    for(int i = 0; i < 3 i++)
    {
        for(int k = 0; k < 30; k++)
        {
            inputFile >> weather[i][k];
        }
    }


    return;
}

This is embarassing, I was staring at that stupid loop forever! Thanks!

OK, thanks for the quick fix, now for the next question which is pretty much the same as the first, when I try and run the function the compiler goes apes****. Is what I'm trying allowed?

#include <iostream>
#include <fstream>

using namespace std;

// ------ Prototype
void readFile(char [][30], ifstream);


// ****** MAIN ****************************************************************
int main()
{
    char weather[3][30];
    ifstream inputFile("P4.dat");

    readFile(weather, inputFile);

        for(int i = 0; i < 3; i++)
        {
            for(int k = 0; k < 30; k++)
            {
            cout << weather[i][k];
            if(k == 30)
            {
                cout << endl;
            }
        }
    }

    return 0;
}

// ------ FUNCTION ------------------------------------------------------------
//
// ----------------------------------------------------------------------------
void readFile(char weather[][30], ifstream inputFile)
{
    for(int i = 0; i < 3; i++)
    {
        for(int k = 0; k < 30; k++)
        {
            inputFile >> weather[i][k];
        }
    }


    return;
}

It would help to show a bit what the sample data file looks like.

I'm betting you have numbers, which may be more than one digit?

Your array is of type char, which holds one character per element. Perhaps you should change the array to type int?

In your display loop, you test for k == 30 in order to print a newline. k will never be 30 inside the loop, test for 29.

Nah, it's all char(s) I pasted the content below, just 90 characters. Also, I forgot to note that when I try running the file the compiler opens up another file called, io_base.h

This only happens when I try and run the program, it compiles just fine. I think there's something I'm not understanding about the fstream object, and how it works as opposed to regular variables. Like I said the books I have never covered this, or at least I couldn't find any mention of it.

R
R
R
C
S
C
S
R
S
R
S
S
R
S
S
S
C
S
R
C
S
R
R
R
S
S
S
C
C
S
C
S
C
C
R
R
R
S
C
C
R
S
C
R
R
C
S
S
C
C
R
S
C
R
S
S
C
S
R
R
R
S
S
S
C
C
S
C
S
R
R
R
C
C
S
C
C
R
C
C
R
S
S
S
S
S
C
R
S
S

Edited 2 Years Ago by mixelplik: update info

I've also tried passing by reference and it didn't work:

void readFile(char [][30], ifstream &);

readFile(weather, stuff);

void readFile(char weather[][30], ifstream &stuff)

Edited 2 Years Ago by mixelplik: cleaned up

You cannot copy an ifstream, so passing it by value would be forbidden and should not compile. Passing it by reference should be fine; the following code compiles and runs without problem:

#include <iostream>
#include <fstream>
using namespace std;
// ------ Prototype
void readFile(char [][30], ifstream&);
// ****** MAIN ****************************************************************
int main()
{
    char weather[3][30];
    ifstream inputFile("P4.dat");
    readFile(weather, inputFile);
        for(int i = 0; i < 3; i++)
        {
            for(int k = 0; k < 30; k++)
            {
            cout << weather[i][k];
            if(k == 30)
            {
                cout << endl;
            }
        }
    }
    return 0;
}
// ------ FUNCTION ------------------------------------------------------------
//
// ----------------------------------------------------------------------------
void readFile(char weather[][30], ifstream& inputFile)
{
    for(int i = 0; i < 3; i++)
    {
        for(int k = 0; k < 30; k++)
        {
            inputFile >> weather[i][k];
        }
    }
    return;
}

Output:
RRRCSCSRSRSSRSSSCSRCSRRRSSSCCSCSCCRRRSCCRSCRRCSSCCRSCRSSCSRRRSSSCCSCSRRRCCSCCRCCRSSSSSCRSS

Edited 2 Years Ago by Moschops

Edit: Damn double post again. I hate this super-clever edit-in-place magic posting thing. :)

Edited 2 Years Ago by Moschops

Thanks! I tried it yesterday and it refused to work. I think I was messing with it so much that I may have had the wrong file name for my .dat file, because today the mofo seems to work. Either way, question answered with aplomb. Much appreciated!

This question has already been answered. Start a new discussion instead.