Hi, so I was practicing the cin.get function and the question is that u input values in and the compiler will sort the letters out. However, when i run the code nothing happens, the compiler simply skips and moves on. Any help? It's supposed to work with the user writing a sentence with spaces.
#include <iostream>
int main()
{
const size_t SIZE { 40 };
char Sentence[SIZE];
std::cin.get(Sentence, SIZE);
std::cout << '\n';
std::cout << Sentence << '\n';
for (int i { }; i < SIZE; i++)
{
for (int j { i + 1 }; j < SIZE; j++)
{
if (Sentence[j] > Sentence[i])
{
char LetterChange = Sentence[i];
Sentence[i] = Sentence[j];
Sentence[j] = LetterChange;
}
}
}
std::cout << Sentence << "|\n"; // spaces get stuck at the end
// need visual indicator to
// show end of string
}
Hello World!
Hello World!
roollledWH! |
[ETA]:
At pointed out by seeplus below, sorting the entire char array instead of just the entered C string has problems.
After the user enters the sentence getting the length of the entered string should be done, and comparing against that value should be used as the terminating conditions of the for loop.
#include <iostream>
#include <cstring>
int main()
{
const size_t SIZE { 40 };
char Sentence[SIZE];
std::cin.get(Sentence, SIZE);
std::cout << '\n';
size_t str_size { strlen(Sentence) };
std::cout << Sentence << '\n';
for (int i { }; i < str_size; i++)
{
for (int j { i + 1 }; j < str_size; j++)
{
if (Sentence[j] > Sentence[i])
{
char LetterChange = Sentence[i];
Sentence[i] = Sentence[j];
Sentence[j] = LetterChange;
}
}
}
std::cout << Sentence << "|\n"; // spaces get stuck at the end
// need visual indicator to
// show end of string
}
Now the sorting is done only on the entered characters, with the added benefit of allowing for an ascending sort instead of a descending sort, change > to < in line 21. Sorting the entire C string in an ascending sort produces garbage.
The sort premise isn't right. sort (however done) should just sort the entered chars - not the whole of Sentence - especially where Sentence isn't initialised.
Yeah, seeplus, I fooked up, not noticing* at the time I was doing the sorting of the C string in a wrong fashion. Mea culpa. Good catch of a stupid mistake.
Sorting the entire char array instead of just the entered C string produces garbage output when doing an ascending sort, while it appeared to work properly for the descending sort.
After changing the code to work on the actual size of the entered C string the change made it possible to sort a C string in descending and ascending order properly.
Enter a sentence (39 characters MAX!):
Hello World!
Entered sentence (12 characters):
Hello World!
Sorted sentence:
roollledWH!
Sorted sentence:
!HWdellloor
*Well I knew something wasn't correct, I tried to do an ascending sort on the entire char array and got garbage results. For the life of me I couldn't figure out why.
I did do a test of a std::string sorted both ways and it worked. At the time I still didn't make the connection of the differences between a char array and the C string data. *annoyed grunt!*
Then before I saw your reply, seeplus, I figured it out. Still, have to give you credit for fixing my screw-up.