The objective of the program is to read from two input files and do some comparisons on them.
I did the EOF loop since i had to read every thing from the file and the number of tokens coming in were unknown.
std::ifstream first("file1.txt");
std::ifstream second("file2.txt");
int a, b;
if(first.is_open() && second.is_open()) {
while(first >> a && second >> b) {
if(a == b) // ...
}
}
// don't call close; let first and second fall out of scope
Also notice your variable names are prone to confusion and non descriptive. Try to use better names (the ones I used are terrible as well, but at least they're clearly different).
But this approach would not work since I am comparing each element of file1 with all the elements of file2.
So may be something like this could work.
1 2 3 4 5 6 7 8 9 10 11 12
std::ifstream first("file1.txt");
std::ifstream second("file2.txt");
int a, b;
if(first.is_open())
{
while (first >> a) {
while (first >> b) {
if(a == b) // ...
}
}
}
Note:Variable names are shown for demonstration purposes. Not actual variable names used in the program.