Can't figure out why this progam is hanging

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • erictheone
    New Member
    • Mar 2009
    • 13

    Can't figure out why this progam is hanging

    So what this code should do is take a string and change its location in the string. But when it gets to line 35 it doesn't exit the loop, I think my conditions are messed up but I can't see the problem. Any help would be appreciated.
    Code:
    string tranString( string letters){
         //determines length of letters
         int stringLength = letters.size();
         //preps before mix dimensions
         int arraySize = 1;
         int arrayHeight = 1;
         //sets extralength = 0
         int extraLength = 0;
         //determines if stringlength is a perfect square
         for ( int n = 1; n <= stringLength; n++){
             if( stringLength % n == 0 && (stringLength / n) == n){
             system("PAUSE");
             arraySize = stringLength / n;
             arrayHeight = arraySize;
             n = stringLength;
             system("PAUSE");
             }
             else if( (stringLength - n) % n == 0 && (stringLength  - n) / n == n ){
                  cout << n << endl;
                  system("PAUSE");
                  arraySize = (stringLength - n) / n;
                  arrayHeight = (stringLength - n) / n;
                  extraLength = n;
                  system("PAUSE");
                  }
    
         } 
         //initalizes array for text
         string beforeMix[arraySize][arrayHeight];
         int row = 0;
         int subStr = 0;
         cout << "arraySize:" << arraySize << " arrayHeight" << arrayHeight << endl;     
         for (row = 0, subStr = 0 ; row <= arrayHeight - 1, subStr != stringLength - extraLength; row++){
             system("PAUSE");    
                  for ( int column = 0; column <= arraySize - 1; column++, subStr++){
                  beforeMix[column][row] = letters.substr(subStr, 1);
                  cout << beforeMix[column][row] << endl;
                  cout << "column:" << column << " row:" << row << " subSt:" << subStr << endl;
                  }
              cout << "exited first row" << endl;  
             //}
         }
         string newOrder;
         for( int column = arraySize - 1, subStr = 0; column >= 0, subStr <= (letters.length() - 1); column--){
               
               for( row = arrayHeight - 1; row >= 0; row--){
                     newOrder.replace(subStr, beforeMix[column][row].length(), beforeMix[column][row]);
                     subStr = (beforeMix[column][row].length()) - 1;
                     }
         }
         if( newOrder.length() % 2 == 0){
         newOrder.insert( (newOrder.length() / 2), letters.substr( (stringLength - extraLength) - 1, extraLength));
         }
         else
         {
             newOrder.insert( ((newOrder.length() - 1) / 2), letters.substr( (stringLength - extraLength) - 1, extraLength));
         }
         
              
        return newOrder;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I expect the error is here:
    Code:
    string beforeMix[arraySize][arrayHeight];
    This array is on the stack. That means when the compiler generates code to create it the arraySize is 1 and the arrayHeight is 1 because the program hasn't run yet. So I don't think your are getting the array you think you are.

    I believe you are rewriting vector.

    If you use a vector whose members are vectors of strings, all this code is already written for you.

    I recommend you ditch this code in favor of:
    Code:
    vector<vector<string> > v;
    for your two dimensional array.

    Since a vector implements an array, the v container can be accessed using
    v[i][j] after it has been loaded.

    Comment

    • erictheone
      New Member
      • Mar 2009
      • 13

      #3
      so what your saying is remove the array and replace it with a vector so I don't need to determine the dimension of my array?

      Comment

      • erictheone
        New Member
        • Mar 2009
        • 13

        #4
        nvm i figured it out.

        Comment

        Working...