Using a file inside a function

I need to use an input(and later an output) file inside a function. I am writing this according to the textbook, and yet I recieve this error. If someone can clarify for me how I can properly use a file within a function I would be very gratefull.

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
const int SIZE = 10;


//Function read2arrays
//Input:
// istream - input file contaning k and test gardes
// test1 and test 2 arrays for test grades
// k - number of test grades in each array
//Process:
// Inputs grades from istream into the arrays
//Output;
//
void read2arrays (istream &in, int test1[], int test2[], int &k)

in >> k;

if(k>0 && k<=SIZE)

out << "There are " << k << " test grades" <<endl;

else {

cout << "Invalid number of marks" << endl;

exit(1);

}

for (count = 0; count<=k; count ++) {

in >> test1[count] >> test2[count];

cout << "\tTest 1 Grades\tTest 2 Grades" << endl << endl;

cout << '\t' << test1[count] << '\t' << test2[count] << endl;

}

return;

int main() {

int test1[SIZE], test2[SIZE], k;

ifstream infile("HW5inputfile.dat");

if(!infile.is_open()) {

cout << "Error opening file HW5inputfile.dat" <,endl;
exit(1);

}

read2arrays(infile, test1, test2, k);

infile.close();

return 0;

}






May we see the error output to see what the problem is?
deleted
Last edited on
deleted
Last edited on
heres the better one... sorry...

C:\Users\Neil Alek\Desktop\Labs\Homework 6.cpp|20|error: expected initializer before 'in'|
C:\Users\Neil Alek\Desktop\Labs\Homework 6.cpp|22|error: expected unqualified-id before 'if'|
C:\Users\Neil Alek\Desktop\Labs\Homework 6.cpp|26|error: expected unqualified-id before 'else'|
C:\Users\Neil Alek\Desktop\Labs\Homework 6.cpp|34|error: expected unqualified-id before 'for'|
C:\Users\Neil Alek\Desktop\Labs\Homework 6.cpp|34|error: 'count' does not name a type|
C:\Users\Neil Alek\Desktop\Labs\Homework 6.cpp|34|error: 'count' does not name a type|
C:\Users\Neil Alek\Desktop\Labs\Homework 6.cpp|44|error: expected unqualified-id before 'return'|
C:\Users\Neil Alek\Desktop\Labs\Homework 6.cpp||In function 'int main()':|
C:\Users\Neil Alek\Desktop\Labs\Homework 6.cpp|54|error: expected primary-expression before ',' token|
C:\Users\Neil Alek\Desktop\Labs\Homework 6.cpp|59|error: 'read2arrays' was not declared in this scope|
||=== Build finished: 9 errors, 0 warnings (0 minutes, 0 seconds) ===|
I think you meant to say in = k within your read2arrays function, that's all I can see as of right now.
And within the for loop for(count = 0....) I think you meant to say for(int count = 0...), otherwise count is not initialized
No gentlemen, I am trying to use 'in' in the same way as you would use infile

i.e.

infile >>variable k

in order to place a value that is inside the file into the variable k

alos, i know and fixed the int count thing, this is not a concern for me

so the question is how do i correctly use ifstream inside a function. cannot find any tutorial dealing with this, and the textbook offers only the solution that you see me using in the code.
bump. please this important. i cant go further without this.
void read2arrays (istream &in, int test1[], int test2[], int &k) you have no opening bracket for this function. Add the {
Of course this fixed my problem. Thank you so much Zaita.
Nps :)
Topic archived. No new replies allowed.