This was on a test sometime ago. I wanna practice creating this but for some reason the swap did not show and not complete. I think there are some logic error with (count). How do i get this to work?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#pragma once
#include <iostream>
usingnamespace std;
//contanst
constlong BUFFER = 100;
class RChange
{
public:
//function that reversa the order
void setCh();
private:
//array
char nameCHAR[BUFFER];
long count;
};
nameCHAR's elements go from 0 to count-1, so nameCHAR[count] is out of range.
Also, your current swap will always switch nameCHAR[i] with the last character in nameCHAR. You want to swap nameCHAR[i] with the character that is 'i' characters from the end.
Finally, you don't want iterate through the entire array. If you do that, it will swap all the characters, then swap them right back. So, you want to stop looping at the halfway point.
The first for loop is not the loop that want to use and it didnt meet the requirement i want. So as long as I enter something the count in the first loop wont be out of range.
1 2 3 4 5 6 7 8 9 10
for (long i = 0; i < count; i++)
{
char temp;
temp = nameCHAR[i];
nameCHAR[i] = nameCHAR[count];
nameCHAR[count] = temp;
}
cout << nameCHAR;
I am not quiet sure how to stop the halfway. I saw some people use (count / 2) inside the for loop. Is this what you mean?