Dec 1, 2011 at 9:07am UTC
Hi, I am having trouble with this array. I am shuffling some characters and at the end of one shuffling process I want to copy the "shuffled" array back to the "word" array so that it can be shuffled again. But when I copy it back to the old array, for some reason it is adding a couple of strange characters to it "aebfcgdhei┬jF☻". How can I fix that? Thanks alot.
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
int main() {
int len;
int first = 0;
int last;
char shuffled[100];
char word[100];
cout << "Please enter your string to be shuffled" << endl;
cin.getline(word, 100);
len = strlen(word);
last = len/2;
cout << "Length of word is " << len << endl;
for (int shuffling = 0; shuffling < 10; ++shuffling){
for (int o = 0; o <= len; ++o){
shuffled[o] = word[first];
cout << o << " ---------- " << shuffled[o] << endl;
++o;
shuffled[o] = word[last];
cout << o << " ---------- " << shuffled[o] << endl;
first++;
last++;
}
cout << endl;
for (int p = 0; p <= len; ++p){
word[p] = shuffled[p];
}
cout << "-----------------------Shuffle Number:" << shuffling << " finished-----------------------" << endl;
}
system("pause" );
}
Last edited on Dec 1, 2011 at 9:09am UTC
Dec 1, 2011 at 9:47am UTC
It is due to this:
for (int o = 0; o <= len; ++o)
and
for (int p = 0; p <= len; ++p)
The problem is '<='. It overwrites the 0 at the end. Write '<' instead
Dec 1, 2011 at 10:01am UTC
Thanks alot, that worked for the first shuffle, but when it does the loop a second time, it has wierd characters again
Last edited on Dec 1, 2011 at 10:01am UTC
Dec 1, 2011 at 10:03am UTC
actually nevermind, I figured it out, I was not resetting the first and last increments so it kept incrementing where left off Thanks alot for help