substring (x-3, x) not working in program

I have a program that simulates a "travel game" where each person's travel itinerary is given with 3 letters then a dash, 3 letters then a dash (all the way until the last 3 letters which do not have a dash following).

example: ABC-DEF-EFG

This continues on in a loop (each day, they go to the next three-lettered location, if they reached the end, then they go to the first place). I remove these dashes to simplify when calculating.

Everyone has statuses, healthy, sick, and recovering. When people are sick or recovering in the same location (every three letters) with a healthy person, the healthy person becomes sick.

But my problem is when trying to get the right location, I use "count" to end the simulation at the 100th loop or when everyone is healthy. My logic is that (count+1)*3 is the location's end (for instance, on day 1, this is the 3rd character, on day 2, it's the 6th...), and when %travel[i].length() is which travel location we want to be at (the beginning, hence why the +2 for that case), but the titular substring is not extracting the correct thing.


The output is often way more than three characters. How is this possible?

Line 50 is the problematic output line for anyone interested.

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
 #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++){
           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]= "sick";}
               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*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, icount)<< " jcount to +3: "<<travel[j].substr(jcount-3, jcount)<<endl; //testing only
               if (travel[i].substr(icount-3, icount).compare(travel[j].substr(jcount-3, jcount)) == 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 (sickcount==0|| count ==99){
               cout<<"Got to the end!"<<endl; //testing only
               for (int i=0;i<sickcount;i++){ //output the sick people's names
                   cout<<sickguys[sickcount]<<" ";
               }
               cout<<sickcount<<" ";
               break; //output result since everyone is good or 100 day pass
           }
           count++;
    }
    cout<<endl;
    return 0;
}
Last edited on
Disclaimer: I'm on my mobile, didn't read your code.

String.substr( pos, len ) is correct, not substr( pos1, pos2 ).

Always refer to the docs.
> My logic is that (count+1)*3 is the location's end (for instance, on day 1,
> this is the 3rd character, on day 2, it's the 6th...)
std::vector<std::string> travel = {"abc", "def", "efg"};
now you may simply advance by one step.

¿do you have several travelers?
std::vector<std::vector<std::string> > travel;
the rowws represent each person, the column represent the itenerary
each element is a three character string, the name of the city
Instead of removing all the dashes from travel, why not just add one to the end? Then every element is 4 chars long and the calculation is simple. Actually, it's pretty simple without the dash at the end since you could mod the element index (i.e., which 3-char group you want) by the number of elements (3-char groups) before multiplying it by 4.

Anyway, I don't understand your program. How does a person get healthy? It seems once they aren't healthy they toggle back and forth between "sick" and "recovering".
Last edited on
Topic archived. No new replies allowed.