iterating vector of strings C++ -


the code read instructions text file , print out graphic patterns. 1 function not working properly. function read vectors of strings i've got file structs.

below output, , second, third, , sixth graphs wrong. seems 2nd , 3rd vectors not putting correct row , column numbers; , last 1 skipped "e" in alphabetical order. tried debug many times , still can't find problem.

  typedef struct pattern{     int rownum;     int colnum;     char token;     bool istriangular;     bool isouter; }pattern; void commandprocessing(vector<string>& , pattern& ); int main() {  (int = 0; < command.size(); i++)     {         pattern characters;         commandprocessing(command[i], characters);      }      system("pause");     return 0; }   void commandprocessing(vector<string>& c1, pattern& a1)     {         reverse(c1.begin(), c1.end());         string str=" ";           (int j = 0; j < c1.size(); j++)         {              bool foundalpha = find(c1.begin(), c1.end(), "alphabetical") != c1.end();             bool foundall = find(c1.begin(), c1.end(), "all") != c1.end();             a1.istriangular = find(c1.begin(), c1.end(), "triangular") != c1.end() ? true : false;             a1.isouter = find(c1.begin(), c1.end(), "outer") != c1.end() ? true : false;              if (foundalpha ==false && foundall == false){                 a1.token = '*';             }             //if (c1[0] == "go"){             else if (c1[j] == "rows"){                 str = c1[++j];                 a1.rownum = atoi(str.c_str());                 j--;             }             else if (c1[j] == "columns"){                 str = c1[++j];                 a1.colnum = atoi(str.c_str());                 j--;             }             else if (c1[j] == "alphabetical")                 a1.token = 0;              else if (c1[j] == "all"){                 str = c1[--j];                 a1.token = *str.c_str();                 j++;             }          }      } 

command format

my output

before debugging (or posting) code, should try make cleaner. contains many strange / unnecessary parts, making code harder understand (and resulting in buggy behaviour described).

for example, have if in beginning:

if (foundalpha ==false && foundall == false){

if there no alpha , command, true, entire length of loop, , other commands placed in else if statements. they won't executed.

because of this, in second , third example, no commands read, except istriangular , isouter flags.

instead of mixed structure this, consider following changes:

  • add default constructor pattern struct, initializing members. example if initialize token *, can remove if, , 2 bool variables required it.
  • do parsing in 1 way, consistently - easiest moving triangular , outer bool same if structure others. (or if want keep find lookup, move them before loop - have set them once!)
  • do not modify loop variable ever, it's error magnet! okay, there rare exceptions rule, not 1 of them.

    instead of str = c1[++j];, , decrementing later, write str = c1[j+1]

  • also, sure need reverse? makes relative +/-1 indexing unclear. example, c1[j+1 j-1 in original command string.

about last one: that's bug in outer printing code, didn't post.


Comments