473,382 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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.
Expand|Select|Wrap|Line Numbers
  1. string tranString( string letters){
  2.      //determines length of letters
  3.      int stringLength = letters.size();
  4.      //preps before mix dimensions
  5.      int arraySize = 1;
  6.      int arrayHeight = 1;
  7.      //sets extralength = 0
  8.      int extraLength = 0;
  9.      //determines if stringlength is a perfect square
  10.      for ( int n = 1; n <= stringLength; n++){
  11.          if( stringLength % n == 0 && (stringLength / n) == n){
  12.          system("PAUSE");
  13.          arraySize = stringLength / n;
  14.          arrayHeight = arraySize;
  15.          n = stringLength;
  16.          system("PAUSE");
  17.          }
  18.          else if( (stringLength - n) % n == 0 && (stringLength  - n) / n == n ){
  19.               cout << n << endl;
  20.               system("PAUSE");
  21.               arraySize = (stringLength - n) / n;
  22.               arrayHeight = (stringLength - n) / n;
  23.               extraLength = n;
  24.               system("PAUSE");
  25.               }
  26.  
  27.      } 
  28.      //initalizes array for text
  29.      string beforeMix[arraySize][arrayHeight];
  30.      int row = 0;
  31.      int subStr = 0;
  32.      cout << "arraySize:" << arraySize << " arrayHeight" << arrayHeight << endl;     
  33.      for (row = 0, subStr = 0 ; row <= arrayHeight - 1, subStr != stringLength - extraLength; row++){
  34.          system("PAUSE");    
  35.               for ( int column = 0; column <= arraySize - 1; column++, subStr++){
  36.               beforeMix[column][row] = letters.substr(subStr, 1);
  37.               cout << beforeMix[column][row] << endl;
  38.               cout << "column:" << column << " row:" << row << " subSt:" << subStr << endl;
  39.               }
  40.           cout << "exited first row" << endl;  
  41.          //}
  42.      }
  43.      string newOrder;
  44.      for( int column = arraySize - 1, subStr = 0; column >= 0, subStr <= (letters.length() - 1); column--){
  45.  
  46.            for( row = arrayHeight - 1; row >= 0; row--){
  47.                  newOrder.replace(subStr, beforeMix[column][row].length(), beforeMix[column][row]);
  48.                  subStr = (beforeMix[column][row].length()) - 1;
  49.                  }
  50.      }
  51.      if( newOrder.length() % 2 == 0){
  52.      newOrder.insert( (newOrder.length() / 2), letters.substr( (stringLength - extraLength) - 1, extraLength));
  53.      }
  54.      else
  55.      {
  56.          newOrder.insert( ((newOrder.length() - 1) / 2), letters.substr( (stringLength - extraLength) - 1, extraLength));
  57.      }
  58.  
  59.  
  60.     return newOrder;
  61. }
Mar 18 '09 #1
3 1211
weaknessforcats
9,208 Expert Mod 8TB
I expect the error is here:
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. 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.
Mar 18 '09 #2
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?
Mar 18 '09 #3
nvm i figured it out.
Mar 18 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Harlan Messinger | last post by:
A publication style guide indicates that for a table heading like the following, Table 3. Wheat and rye harvest in European countries in years that end in 3 or 7 or when a new prime minister...
25
by: Shannon Jacobs | last post by:
Maybe there is a simple trick here, and I'm not spotting it... Is there a guru of CSS hanging around here who can help out? The page in question has a multi-column table with a list of links in...
8
by: briforge | last post by:
I am writing a program for the Palm OS in c++. I have already posted this to a palm development forum but I'm desperate for a fix, so I hope this cross-post isn't offensive. My program is...
4
by: SStory | last post by:
Have noticed that my app sometimes hangs in memory. It is a multithreaded app with a main thread and a worker thread but I call something to spin it down on quiting. Any ideas as to why this...
5
by: David C. Allen | last post by:
I have a vb.net app that is controling excel 2000 thru the com interop interface. I have referenced the excel 9.0 library and have cut down the code in the problem subroutine to this: Dim...
2
by: JRough | last post by:
I cannot log into our web site. I have a test web site and a real site. On Friday I could log in and today Monday I cannot log in. I have 2 databases In PHPMyAdmin, the real database and and a...
5
by: Stanimir Stamenkov | last post by:
I'm trying to style an icon "hanging" below the first line of a heading and I've found interesting difference between Mozilla and the other browsers I'm trying with - Safari 3.1.1, Opera 9.27 and...
4
by: Anil Gupte/iCinema.com | last post by:
Apparently there are plenty of utilities out there that can dissasemble my progam. My colleague found a progam that could read my exe file and show all my source code! In any language (even though...
2
by: kj | last post by:
Hi! I have a Pythonoob question. I have a script that hangs indefinitely at random times; the only thing to do at this point is to kill it. I'm looking for suggestions on how to troubleshoot...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.