I'm very new to C++ (less than 2 days). I've written some code to sort the letters of a word into alphabetical order. If given "jack" I expect it to return "acjk". It is working but when I tried to add more code to save the result to a file it no longer sorts (and gives very weird results):
I feel I have totally missed some concept (pointer/arrays?), but I'm having trouble identifying what the exact problem is. Any advise would be very appreciated.
Hi,
I am also very new to C++. I found a first problem with your code as you do not put a "break" to exit the loop when the condition (theword[i] == '\0') is satisfied. As it is now, you got the length of "theword" longer than the one read, thus passing some uninitialized garbage to the sort function. Could this be the problem? With this modification your code runs well on two systems I tried. Also, isn't sort supposed to work with iterators? It seems to be working also with pointers to char, though....
Sincerely
Max