alright, here's the full code. The error with positioning is still within accessing travel[i] which somehow doesn't exist, and when I debug, printing out travel[0] gets no elements (using break with line after erase). The cout does not output anything (not even after next/continue)... most likely because travel[0] is inaccessible
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
int main(){
int inputs;
cin>> inputs;
vector <string> names;
vector <string> status;
vector <string> travel;
for (int i=0;i< inputs;i++){
//name, status, itinenary
string a, b, c;
cin>>a>>b>>c;
names.push_back(a);
status.push_back(b);
travel.push_back(c);
//get rid of the dashes
travel[i].erase(std::remove(travel[i].begin(), travel[i].end(), '-'), travel[i].end());
cout<<"name was "<<names[i]<< " status "<<status[i]<< " and itinerary "<<travel[i]<<endl; //testing only
}
int count = 0;
vector <string> sickguys;
while (true){
int sickcount =0;
//traverse through the peoples
for (int i=0;i<inputs;i++){
// cout<<"status "<< i<< " is "<<status[i]<<endl; //testing only
if (status[i].compare("sick") == 0|| status[i].compare("recovering") ==0){
sickcount++;
if (status[i].compare("sick")==0){ status[i] = "recovering";}
else if (status[i].compare("recovering") ==0){ status[i]= "healthy";}
//cout<<"immediately, sick/recover became "<<status[i]<<endl; //testing only
int icount;
if ((count+1)*3 >= travel[i].length()){
icount = (count+2)*3%(travel[i].length()); //obtain which day in the guy's itenerary
}
else{
icount = (count+1)*3;
}
// can change healthy back to sick!
for (int j=0; j< inputs;j++){
int jcount;
if ((count+1)*3 >= travel[j].length()){
jcount = (count+2)*3%(travel[j].length());
}
else{
jcount = (count+1)*3;
}
cout<<"count = "<<count<< " icount = "<< icount<<" and jcount = "<<jcount<<endl; //testing only
//cout<<"icount to -3: "<<travel[i].substr(icount-3, 3)<< " jcount to +3: "<<travel[j].substr(jcount-3, 3)<<endl; //testing only
if (travel[i].substr(icount-3, 3).compare(travel[j].substr(jcount-3, 3)) == 0 && status[j].compare("healthy") == 0){
status[j]="sick";
}
} //end j loop
sickguys.clear();
sickguys.push_back(names[i]); //on any day with sick people, re-add them
} //end IF
} //end FOR
if (sickcount==0|| count ==99){
cout<<"Got to the end!"<<endl; //testing only
for (int k=0;k<sickcount;k++){ //output the sick people's names
cout<<sickguys[k]<<" ";
}
cout<<sickcount<<" ";
break; //output result since everyone is good or 100 day pass
}
count++;
//cout<<"Got to count = "<<count<< " with sickcount "<<sickcount<<endl; //testing only
} //end WHILE
cout<<endl;
return 0;
}
|
input:
8
Pat sick XPU-JBK-TNU-BEN-HEM-ZJY-IMY-WFA-PPT
Xan sick KSB-TRV-XPU-JBK-TNU-BEN-HEM-IMY-WFA-NND
Mel sick KSB-TRV-XPU-TNU-BEN-HEM-ZJY-IMY-WFA-PPT-NND
Nick healthy KSB-TRV-JBK-HEM-IMY-WFA
Alf recovering KSB-TRV-XPU-JBK-TNU-BEN-IMY-WFA-NND
Tim healthy TRV-XPU-TNU-HEM-ZJY-PPT-NND
Irv recovering KSB-TRV-XPU-JBK-BEN-HEM-ZJY-IMY-PPT
Andy recovering XPU-JBK-TNU-BEN-HEM-ZJY-IMY-PPT-NND
perhaps it's the "input three strings" in the original program requirement that are messing me up? hmmm....