I am trying to read in a file that only have one column, and write it out to terminal in 3 columns. I have decided to read the one column into separate vector<string>. Then put the vector string into one vector<vector <string> >, before writing out to terminal.
I know it is suppose to be a roster for chores in a flat, hence i know i can split the file by read 15 rows, before i start to read into another vector. However, the number of duties/chores can be changed by the manager, hence i don’t know how many vector<string> i will need, so i can’t just have a set number of vector<strings> pre-declared.
The .dat file is in the form of
Duty1
P1
P1
P1
...
Duty2
P2
P2
P2
...
Duty3
P3
P3
P3
...
*
This is what i want it to look like
Duty1 Duty2 Duty3
P1 P2 P3
P1 P2 P3
P1 P2 P3
This is how it looks like now
Duty1 Duty1 Duty1
P1 P1 P1
P1 P1 P1
P1 P1 P1
I was wondering if anyone could help me spot my mistake
Here’s my code for importing the file.
void DutyManagement::importFlatDuties(){
ifstream fin1(dutyFile.c_str());
if( !fin1 ){
cout << "Unable to find file: " << dutyFile<<endl;
exit(1);
}
else{
vector <string> temptVec;
string tempString;
flatDutyRoster.clear();
while(true){
for (int i=0; i<14; i++){
fin1 >> tempString;
if (tempString == "*"){
fin1.close();
return;
}
temptVec.push_back(tempString);
}
flatDutyRoster.push_back(temptVec);
}
}
}
And here is my code for displaying it to terminal
void DutyManagement::displayDuty(){
importFlatDuties();
for (int i=0; i<14; i++){
for (int j=0; j<flatDutyRoster.size(); j++){
if (i == 0){
cout<<setw(10)<<"Duty "<<j+1<<": "<<setw(10)<<flatDutyRoster[j].at(i);
}
else{
cout<<setw(23)<<flatDutyRoster[j].at(i);
}
}
cout<<endl;
}
}
I’m almost certain it’s just a typo somewhere, but after carefully combing through for several times, i was unable to spot it. It’s my first time trying to work with a vector inside a vector. Perhaps I miss understood something? I don’t know
Any help would be much appreciated. Either you know what’s wrong, or if you’ve a better way of achieving the same result.
Thanks in advance.
You need to call
temptVec.clear()somewhere.