Hi I am stuck on how to output data that I got from a text file.
The text file looks like this:
1 5
2 6
3 7
4 8
5 9
6 10
7 11
8 12
9 13
10 14
Since I want to have an array of a class, in my main.cpp I have this:
And in my header file I have this:Code:#include <iostream> #include <fstream> #include <iomanip> #include <string> #include "Check.h" using namespace std; int main() { Check Checks[2]; cout << Checks[0].id[0] << Checks[0].num[0] << endl; cout << Checks[1].id[0] << Checks[1].num[0] << endl; return 0; }
So with that, I want to see the results to be:Code:#ifndef CHECK #define CHECK #include <iostream> #include <fstream> #include <iomanip> #include <string> class Check { public: int id[5]; int num[5]; Check(); void readCheck(ifstream&); }; Check::Check( ) { for ( int i = 0; i < 5; i++) { id[i] = 0; num[i] = 0; } } void Check::readCheck(ifstream&) { ifstream File; File.open("data.txt"); Check Checks[4]; for(int i = 0; i < 2; i++) { for(int j = 0; j < 5; j++) { File >> Checks[i].id[j]; >> Checks[i].num[j]; } } File.close(); } #endif
1 5
6 10
However I would just keep the output of
0 0
0 0
Please see what I did wrong. Thank you so much!



LinkBack URL
About LinkBacks


