Rotating an array of strings issue...

Hello !
I'm trying to do a rotation of strings but I'm having trouble with the last element. Everything rotates the way I want but the final element always has the exact same contents from another mystery string appended to the end. The mystery string is declared and used outside of this function (and is not passed as a parameter to this function).

**The really strange thing to me is that the commented out 'fout' within the function show the last element to be exactly the strings that I expected. So what's going on ???

THANKS !

Here is the part of the function:
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
//declared outside function
string sString[6]; //element 0 is not rotated and set to ""

//function start

for(int i=0; i<=6; i++) //to display results
{
   fout << endl << "sString[i] is " << sString[i];
}

for(int i=1; i<=zeroCounter; i++)
{
   theTempString = sString[1];

   for(int j=1; j<= zeroCounter; j++)  //zeroCounter > 3
   {				
      if(j == zeroCounter)
      {
      //   fout << "\nbefore    sString[j]: " << sString[j];
         sString[j] = "";
         sString[j] = theTempString;
     //    fout << "\nafter    sString[j]: " << sString[j];
     //    fout << "\ntheTempString: " << theTempString;
      }
      else
      {
         sString[j] = sString[j + 1];
      }
   }
}

for(int i=0; i<=6; i++)
{
   fout << endl << "sString[i] is " << sString[i];
}

example output start:
sString[i] is
sString[i] is Lars52
sString[i] is HeyWanna
sString[i] is Pekar
sString[i] is peon3
sString[i] is burim 12

end:
sString[i] is
sString[i] is peon3
sString[i] is burim 12
sString[i] is Lars52
sString[i] is HeyWanna
sString[i] is PekarTHIS is the extra string appendedwtf???

Last edited on
Why don't you just use the C++ function rotate()? This is what it is there for.

Something like rotate(sString+1, sString + zeroCounter, sString + 6), see reference for details:
http://www.cplusplus.com/reference/algorithm/rotate/
http://en.cppreference.com/w/cpp/algorithm/rotate
Thanks! That'll help. I didn't make the mental connection that it could be used to rotate strings.

And.......
For completeness, any explanation for the mystery string ? I don't understand how the content of the final element could have changed within the loop (where it isn't supposed to change anymore) to just outside of the loop.
Last edited on
Hey, looks like I'm having issues with rotate. I've tried a few variations like this one:

rotate(sString->begin() + 1, sString->begin() + zeroCounter, sString->end());

I keep getting offset out of range errors...
:/
Last edited on
I think that the problem is in incorrect using of subscriptors.

1
2
3
4
5
6
7
8
string sString[6]; //element 0 is not rotated and set to ""

//function start

for(int i=0; i<=6; i++) //to display results
{
   fout << endl << "sString[i] is " << sString[i];
}


For example your array has 6 elements and therefore the loop should look as


1
2
3
4
for( int i=0; i < 6; i++ ) //to display results
{
   fout << endl << "sString[i] is " << sString[i];
}

Fixed it !

for(int i=0; i<=6; i++) was a typo sorry (program would crash with that line)
But it made me look in the right direction !!!

The problem was that I forgot to place a newline character after a previous call to fout in another function. Once the functions cycled, it looked like there was an extra mystery string being tacked onto the end of these, when in fact it was the first string sent to fout from the next function.

Thanks ~
Topic archived. No new replies allowed.