Hi, I've been trying to figure out how to cout the even lines from a file into one array and the odd lines from the file into another array. I've tried a few things but nothing seems to be working.
Any help would be appreciated.
I can't really follow your code, but I might do it something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
void readResp(string resp[], string cat[], int &size) {
intconst maxsize = 25;
ifstream infile("pa.txt"); // don't pass infile in just to use it as a local variable!
if (!infile) {
cout << "Cannot open pa.txt\n";
return;
}
string line;
int i = 0;
while (i < maxsize && getline(infile, line)) {
resp[i] = line;
if (!getline(infile, line)) {
// error: missing 2nd line
}
cat[i] = line;
++i;
}
size = i;
}
Thanks for replying quickly, I'll try what you did. Can I ask you what specific line makes it so that the array resp[] only reads in the even lines from the file?
For the infile thing, I actually have a few more functions where I'll be using it which is why I had passed it in the parameter.
The idea is that there are two getlines. The first one reads the odd lines, the second one reads the even lines. The way it is, it's actually putting odd lines in resp, but you can switch that around either way.
Actually, it looks like those are "parallel arrays", which are usually better implemented as a single array of structs (or classes). Also, it's always best to use a vector unless there's a good reason not to. So I think you want something like this (still reading odd lines into resp) :
Unfortunately, for this assignment, we can't use vectors, structs, or classes, we have to use arrays. I tried what you said and even tried switching some things around but now my output is that this file did not open.