Hello,
So I´m new in C++ programming and I´m trying to read a .txt file into arrays.
My file contains 4 columns separated with space. The first column contains words (just one word per line) and the last three contain numbers. The file has unknown number of lines.
I know how to read a file in c++ but I need help to read the columns into 4 separated arrays.
Does anyone here have a tip to do that ?
thank you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
string thefilename;
ifstream infile;
cout << " Write the file path : " << endl;
cin >> thefilename;
cout << endl;
infile.open(thefilename.c_str());
if ( infile.fail() ) {
cout << " Cannot open file: " << thefilename << endl;
exit(1);
}
infile.close();
return 0;
}
|
So the file contains information about number of students at a university.
The 1 column contains information about the department, 2 column contains the year, 3 contains number of men and the 4 column contains number of women.
The program needs to give the user a menu to choose from.
If the user chooses "0: total " then the program gives number of student each per year that the file contains independent of what the student was studying.
and if the user chooses for example "1: business" than the programprogram gives number of students per each year at the business program