HELP! (output words in alphabetical order)

how do i ask the user to input 10 words of 5 letters each and then output them in alphabetical order??!
Last edited on
Maybe I'm misunderstanding the question but this seems appropriate:
std::cout << "Input 10 words of 5 letters each: ";
You have to be including iostream to use std::cout of course.
Last edited on
no no, i know how to do that!
the question is how do i make an array that only accepts 10 words with 5 letters each and then sort them in alphabetical order!
thanks anyway!
Ah I see, in that case what you want to do is store an array of 10 strings and have an input loop kind of like this:
1
2
3
4
5
6
7
8
9
10
11
std::cout << "Input 10 words of 5 letters each: ":

for(int i = 0; i < 10; i++)
{
   std::cin >> Input; //The Variable Input Is An "std::string"
   if(strlen(Input) != 5)
   {
       std::cout << "\nPlease Use Words 5 Characters Long\n";
       i--;
   }
} 


This isn't compiled or tested but it should give you an idea of what you are trying to do.
Last edited on
thank you!!!
im just a little confused, i don't see how it works!
if you have time, can you elaborate?
i really appreciate all the time you are spending on this! =D
Hello :)

you should use strcmp(string, string) to compare the 2 strings and arrange them in order. I leave that part up to you :).

As mentioned at http://www.cplusplus.com/reference/clibrary/cstring/strcmp/

A zero value indicates that both strings are equal.
A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2; And a value less than zero indicates the opposite.

Hope I helped :)

Edit:

As for
im just a little confused, i don't see how it works!

what it does is loop the user 10 times in order to input a value. (hence the for(int i = 0; i < 10; i++)

The user inputs a string and
1
2
3
4
5
if(strlen(Input) != 5)
   {
       std::cout << "\nPlease Use Words 5 Characters Long\n";
       i--;       //i--; is to undo the increase of i;
   }

checks that the length of the input is 5, if not the user is asked to enter another string.

(Also I would suggest an array of strings if you intend to compare the strings string str[5];)

Again, I hope I was of some help :)
Last edited on
OMG!!! thanks!!! =D
Topic archived. No new replies allowed.