The code below is designed two input two separate .txt files. Each file has a different integer on each line. The code below reads the files, and stores each value in the index off an array. The input works great, and the cout in the while loop prints the proper value to command line.
The issue however comes with the cout << setTwo[0] << endl; outside of the loop. It prints random values as if the index of the array is not set. Im totally confused.
set2.txt
1
3
9
2
Code
int maxSize = 100;
int setOne[maxSize];
int setTwo[maxSize];
int setOneSize = 0;
int setTwoSize = 0;
//open files
ifstream fileOne ("set1.txt");
ifstream fileTwo ("set2.txt");
int number;
int number2;
fileOne >> number;
fileTwo >> number2;
//declare arrays
for(int i = 0; i < maxSize; i++) {
while (fileOne.good()) {
setOne[i] = number;
cout << "set 1 " << setOne[i] << endl;
fileOne >> number;
setOneSize += 1;
}
while (fileTwo.good()) {
setTwo[i] = number2;
cout << "set 2 " << setTwo[i] << endl;
fileTwo >> number2;
setTwoSize += 1;
}
}
cout << setTwo[0] << endl;
setOneorsetTwoin the code you posted.istream::good()is not preferred practice. Use something linewhile (fileOne >> number).