2 Replies - 381 Views - Last Post: 26 March 2019 - 04:32 PM Rate Topic: -----

#1 albert003   User is offline

  • D.I.C Addict

Reputation: 26
  • View blog
  • Posts: 677
  • Joined: 15-December 14

Program to put words in alphabetical order.

Posted 21 March 2019 - 01:27 PM

I'm on lesson #7 and I have two ideas and one I already worked on and works, but its over complicated so I'll disregard it.

My first idea is to use something like this.. Keep in mind this is missing details and I still have to work out things.

#include<iostream>
#include<string>
int main()
{
    std::string n;
    std::cout << "Enter 5 names and the computer will" <<std::endl;
    std::cout << "put them in alphabetical order./n" <<std::endl;
    //std::cin >> n;

    std::cout << n << " has " << n.length();
    
    std::string alphabet = "abcdefghijklmnopqrstuvwxyz";
    
    for(auto x: alphabet)
    {
        std::cout << x <<std::endl;
        if(x == n)
        {
            //store letter
        }
    }
}



My second idea is to use getline(). I'd have the user just key in the words without using the enter button on the computer. Then I would sort the strings.

I'm leaning more towards the first idea, just because it seems like an easier thing to do.

Is This A Good Question/Topic? 0
  • +

Replies To: Program to put words in alphabetical order.

#2 jimblumberg   User is offline

  • member icon

Reputation: 5702
  • View blog
  • Posts: 17,481
  • Joined: 25-December 09

Re: Program to put words in alphabetical order.

Posted 21 March 2019 - 01:43 PM

Quote

I'm on lesson #7

It probably would help if you learned to actually read and understand the assignment.

Quote

Do exercise 6 , but with three string values. So if the user enters the values "Steinbeck", "Hemingway", "Fitzgerald", the output should be "Fitzgerald", "Hemingway", "Steinbeck".

Those highlights are important clues.

For your first idea, why are you trying to do this with 5 names? Where are your three string variables?

Quote

My second idea is to use getline().

Well you could use getline() to get the three strings, but you still need three strings.

Quote

I'd have the user just key in the words without using the enter button on the computer.

This would only give you one string.

Did you forget to read section 3.5.1? Did you do the two "TRY THIS" associated for this section? What about section 3.6? These sections should give you enough information you need to accomplish this exercise. They are discussing how std::strings work and should give enough clues to solve this simple, simple, simple problem.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 albert003   User is offline

  • D.I.C Addict

Reputation: 26
  • View blog
  • Posts: 677
  • Joined: 15-December 14

Re: Program to put words in alphabetical order.

Posted 26 March 2019 - 04:32 PM

I read it and had an idea which paid off. I looked online to verify my hunch. Each letter has a ASCII value so I used the program I previously used to sort the numbers to make the words entered sort in alphabetical order. I was able to make it work on the program I made, I am going to try it on the example you gave me.
#include<iostream>
#include<string>
int main()
{
    std::string name1;
    std::string name2;
    std::string name3;
    std::cout << "Enter 3 names and the computer will" <<std::endl;
    std::cout << "put them in alphabetical order./n" <<std::endl;
    std::cin >> name1 >> name2 >> name3;
    std::cout <<"The words before being put in alphabetical order:"<<std::endl;
    std::cout <<name1<<std::endl;
    std::cout <<name2<<std::endl;
    std::cout <<name3<<std::endl;
    std::string temp;
    if(name1 > name2)
    {
        temp = name1;
        name1 = name2;
        name2 = temp;
    }
    if(name2 > name3)
    {
        temp = name2;
        name2 = name3;
        name3 = temp;
    }
    if(name1 > name2)
    {
        temp = name1;
        name1 = name2;
        name2 = temp;
    }
    std::cout <<"\nThe words before being put in alphabetical order:"<<std::endl;
    std::cout <<name1<<"\n"<<name2<<"\n"<<name3<<std::endl;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1