tags:

views:

88

answers:

4

For some reason, whenever I run this program it exits at permute(permutater, length, lenth); . This doesn't happen whenever I comment out the line and the function doesn't even run. Any help?

+9  A: 

First thing I noticed - you're not initializing the index variable hor.

int permute(string permutater,int length,int lenth)
{
    int hor,hor2,marker;
    cout << length/lenth;
    for (marker=0;marker !=(length/lenth);marker++)
        {
            hor2 = permutater[hor];     // <== hor is not initialized
            permutater[hor] = permutater[hor-1];
            permutater[hor] = hor2;
            hor--;
            cout << permutater;
        }

}
Michael Burr
This is the answer. Any old garbage could be in hor, pointing at any old bit of memory. Accessing that memory causes big trouble. Case closed.
Tom Smith
Thank you very much, in hindsight ( as always) that was obvious -_-. Thanks for putting up with me.
Galileo
+2  A: 

hor2 = permutater[hor];

What's the value of hor?

Anon.
+2  A: 

I got the following compile errors with MSVC

error C4716: 'permute' : must return a value
warning C4700: uninitialized local variable 'hor' used
MSN
A: 

Haven't got a chance to run it yet, but did you notice that you're missing return in the permute(string permutater,int length,int lenth) function.

Also, please #include <string>

thedp