How to alphabetize words using c++

how to go about it?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1884 Views - Last Post: 25 December 2010 - 09:08 PM Rate Topic: -----

#1 theaj11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 29-August 10

How to alphabetize words using c++

Posted 22 December 2010 - 10:04 PM

I was wondering how do you go about making a C++ program that alphabetizes words. I got it so it asks how many words your gunna use and that kind of stuff. but I need some help actully getting it to alphabetize. NOTICE: I'm not done with switch statment yet.I'm only to case 11 I need to go to case 20.{
#include <iostream>   

int main()

{
 using namespace std;  
    
int numberOfWords = 0;
string word1;
string word2;
string word3;
string word4;
string word5;
string word6;
string word7;
string word8;
string word9;
string word10;
string word11;
string word12;
string word13;
string word14;
string word15;
string word16;
string word17;
string word18;
string word19;
string word20;
    
cout << "How many words do you want to aphabetize (2-20 all letters lower cased)?";
cin >> numberOfWords;

if (numberOfWords > 20 | numberOfWords < 2 ) {
   cout <<"\n" << "Not in valid range";
system ("pause");
return 1;
}
switch(numberOfWords)
{
case 2:
    
cout << "Enter both words"<<"\n";
cin >> word1;
cin >> word2;
//aphabetizing code here
break ;

case 3:

cout << "Enter all three words"<<"\n";
cin >> word1;
cin >> word2;
cin >> word3;
//aphabetizing code here 
break;

case 4:
     
cout << "Enter all words"<<"\n";
  cin >> word1 ; 
cin >> word2 ;
  cin >> word3 ;
cin >>word4;
//aphabetizing code here
break;

case 5:
     
cout << "Enter all words"<<"\n";
  cin >> word1 ; 
cin >> word2 ;
  cin >> word3 ;
cin >>word4;
  cin >> word5;
  
//aphabetizing code here
break;

case 6:
     
cout << "Enter all words"<<"\n";
  cin >> word1 ; 
cin >> word2 ;
  cin >> word3 ;
cin >>word4;
  cin >>word5;
  
cin >> word6;
//aphabetizing code here
break;

case 7:
     
cout << "Enter all words"<<"\n";
  cin >> word1 ; 
cin >> word2 ;
  cin >> word3 ;
cin >>word4;
  cin >>word5;
  
cin >>word6;
  cin >> word7;
//aphabetizing code here
break;

case 8:
     
cout << "Enter all words"<<"\n";
  cin >> word1 ; 
cin >> word2 ;
  cin >> word3 ;
cin >>word4;
  cin >>word5;
  
cin >>word6;
  cin >> word7;
cin >> word8;
//aphabetizing code here
break;

case 9:
     
cout << "Enter all words"<<"\n";
  cin >> word1 ; 
cin >> word2 ;
  cin >> word3 ;
cin >>word4;
  cin >>word5;
  
cin >>word6;
  cin >> word7;
cin >> word8;
//aphabetizing code here
break;

case 10:
     
cout << "Enter all words" << "\n";
  cin >> word1 ; 
cin >> word2 ;
  cin >> word3 ;
cin >>word4;
  cin >>word5;
  
cin >>word6;
  cin >> word7;
cin >> word8;
  cin >> word9;
  
cin >> word10;  
//aphabetizing code here
break;

case 11:
     
cout << "Enter all words" << "\n";
  cin >> word1 ; 
cin >> word2 ;
  cin >> word3 ;
cin >>word4;
  cin >>word5;
  
cin >>word6;
  cin >> word7;
cin >> word8;
  cin >> word9;
  
cin >> word10;
  cin >> word11;  
//aphabetizing code here
break;
}

system ("pause");

return 0;    
}


so how did I do so far for my first program I started/coded myself

This post has been edited by theaj11: 22 December 2010 - 10:06 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to alphabetize words using c++

#2 bnc  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 108
  • Joined: 18-March 10

Re: How to alphabetize words using c++

Posted 22 December 2010 - 10:22 PM

what you are doing is very complex..

in c++ you can make use of STL...

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

int main()
{
        int n;
        std::vector<std::string> myStrings;
        std::cout << "Enter no of words between 2 and 20" << std::endl;

        std::cin >> n;

        while(n < 2 || n > 20)
        {
                std::cout << "ReEnter the no of words you want to Enter " << std::endl;
                std::cin >> n;
        }
        std::cout << "n is " <<  n << std::endl;
        std::string str;
        std::cout << "Enter Strings " << std::endl;
        for(int i = 0; i < n; ++i)
        {
                std::cin >> str;
                myStrings.push_back(str);
        }
        sort(myStrings.begin(),myStrings.end());

        std::cout << "myStrings Contains" << std::endl;

        std::vector<std::string>::iterator it = myStrings.begin();

        for(;it != myStrings.end(); ++it)
        {
                std::cout << *it << std::endl;
        }

        return 0;
}



Was This Post Helpful? 0
  • +
  • -

#3 Slumdog  Icon User is offline

  • D.I.C Head

Reputation: 34
  • View blog
  • Posts: 116
  • Joined: 26-November 10

Re: How to alphabetize words using c++

Posted 22 December 2010 - 10:22 PM

Um... I recommend that you plan out your program in a notepad before you put it to work because from what I see, it is... yeah... I'd rather not say anything.

Instead of using switch statements, why not use for loops?
for(int i = 0; i < numberOfWords; i++
	std::cin >> stringVariableHere;



As you can see from that piece of, the variable 'stringVariableHere' will be overwritten and cannot be used later to sort them alphabetically. To solve this issue, we can use dynamic memory allocation to allocate some character pointers OR you can use vectors<char *>, which is something I would prefer.

Do sort your strings, use the sort function, which resides inside the algorithm header.

This post has been edited by Slumdog: 22 December 2010 - 10:24 PM

Was This Post Helpful? 1
  • +
  • -

#4 oscode  Icon User is offline

  • D.I.C Regular

Reputation: 109
  • View blog
  • Posts: 257
  • Joined: 24-October 10

Re: How to alphabetize words using c++

Posted 23 December 2010 - 01:17 AM

View Postbnc, on 23 December 2010 - 04:22 AM, said:

what you are doing is very complex..

in c++ you can make use of STL...


I agree, but you should pre-allocate vectors where necessery. std::vector myStrings(n);.

This post has been edited by oscode: 23 December 2010 - 01:19 AM

Was This Post Helpful? 0
  • +
  • -

#5 bnc  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 108
  • Joined: 18-March 10

Re: How to alphabetize words using c++

Posted 23 December 2010 - 01:31 AM

View Postoscode, on 23 December 2010 - 12:17 AM, said:

View Postbnc, on 23 December 2010 - 04:22 AM, said:

what you are doing is very complex..

in c++ you can make use of STL...


I agree, but you should pre-allocate vectors where necessery. std::vector myStrings(n);.


I am using push_back() for inserting the elements. Initially the vector is of size 0. so nothing wrong in that..
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: How to alphabetize words using c++

Posted 23 December 2010 - 03:51 AM

Chances are guys that this student has not yet covered vectors, and needs to be using arrays. However, it's difficult to ascertain what the assignment is supposed to be covering. Seems to me it's likely arrays and dynamic allocation, but the OP has shown absolutely no inclination to use either. In fact, the OP hasn't even responded.

This post has been edited by JackOfAllTrades: 23 December 2010 - 04:08 AM

Was This Post Helpful? 0
  • +
  • -

#7 oscode  Icon User is offline

  • D.I.C Regular

Reputation: 109
  • View blog
  • Posts: 257
  • Joined: 24-October 10

Re: How to alphabetize words using c++

Posted 23 December 2010 - 03:57 AM

View Postbnc, on 23 December 2010 - 07:31 AM, said:

I am using push_back() for inserting the elements. Initially the vector is of size 0. so nothing wrong in that..


Yes, you should pre-allocate to prevent repeated re-allocations. How you are using vectors works, but there is still something wrong with it.
Was This Post Helpful? 0
  • +
  • -

#8 theaj11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 29-August 10

Re: How to alphabetize words using c++

Posted 23 December 2010 - 05:26 AM

I just learned loops a few days ago so still not very good at them.Its true I havent learned vectors or arrays yet.And Im not doing this as an assignment I'm doing because I want to.
Was This Post Helpful? 0
  • +
  • -

#9 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: How to alphabetize words using c++

Posted 23 December 2010 - 03:10 PM

If you are doing this for self-learning then you are not restricted by a teacher's requirements so you should be looking at
arrays
http://www.cplusplus...utorial/arrays/
and vectors
http://www.cplusplus...nce/stl/vector/

These are language constructs that you should be using to solve this problem.

BTW
Well done for going further than your class.
Personal interest is what makes the difference between the good and the good enough.
Was This Post Helpful? 1
  • +
  • -

#10 theaj11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 29-August 10

Re: How to alphabetize words using c++

Posted 23 December 2010 - 09:04 PM

I am self teaching myself C++. I got the anwser from bnc but I dont understand what most of it means. I know that it sorts out the words alpabetically but other than that I know nada about a lot of it. Here is my new code. I put comments by the parts I dont understand. Also Do you think this would be good enough to sumbit it as a snippet?
#include <iostream>

 #include <string>  

 #include <vector>  

 #include <algorithm>  

int main()

{
 using namespace std;  
    
int numberOfWords  = 0;
int response;

vector<string> myStrings;
system("cls");
cout << "Do you want to alphabetize a list of words? yes=1 no=2 \n";  
cin >> response;
while (response==1) {    
system("cls");
cout << "How many words do you want to alphabetize? 2-20 (All letters must be lower cased when making list) ";
cin >> numberOfWords;

while (numberOfWords > 20 | numberOfWords < 2 ) {
   cout <<"\n" << "Not in valid range. Reenter number of words you want to sort\n";
  cin>>numberOfWords;
}

cout<<"You chose " <<numberOfWords<<" words\n"; 

 string str;//what does this do?
 
 cout << "Enter the words to sort\n";

for(int i=0; i <numberOfWords; ++i)

{
 cin >> str;//what does all this do through     
     myStrings.push_back(str);   
        
        }

sort (myStrings.begin(),myStrings.end());
system ("cls");
cout << "Your words now in alphabetical order are\n";

vector<string>::iterator it = myStrings.begin();

for(;it != myStrings.end(); ++it)
{
        cout << *it <<"\n";
}
//this mean?


system ("pause>NUL");

cout<< "Do you have another list to alphabetize? 1=yes 2=no \n";

cin >> response;
} //end of while
cout << "Thanks for using the Alphabetizer program!";
system("pause>NUL");
return 0;    
}


Was This Post Helpful? 0
  • +
  • -

#11 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: How to alphabetize words using c++

Posted 24 December 2010 - 03:58 AM

View Posttheaj11, on 24 December 2010 - 01:04 PM, said:

Do you think this would be good enough to sumbit it as a snippet?

It is not for me to say.
You should await a comment from the legion of forum leaders, unfortunately none of them seem to be online so you are stuck with the second class citizens :).

I do have few comments that you should feel free to ignore.

1 - Good code layout makes it far easier to write correct code.
A good cook has a clean and well organised kitchen. You need to have a clean and well organised code layout if you are going to see easily what you have written and where you have made a mistake. You will also find that once you are no longer a beginner you may find people refuse to help you if your code is so badly laid out that they have to struggle to read it.

So I strong suggest you choose one of the common indentation styles and use it consistently.
http://en.wikipedia....ki/Indent_style

2 - Don't use system("pause")
Read this:
http://www.gidnetwork.com/b-61.html

3 - This
for(;it != myStrings.end(); ++it)
{
        cout << *it <<"\n";
}
is an ugly use of a for() loop.
It will work but a more common (and therefore easier to read) loop to use in the case in hand is a while() loop.

Your answerable question
string str;//what does this do?

Declares a string with the name 'str'.

i think you are also asking how the 'guts' of the operations of the program works but it is not that clear to me what your specific question/s is/are.

If you spell out the details of each of your questions it will help people help you.
Was This Post Helpful? 0
  • +
  • -

#12 theaj11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 29-August 10

Re: How to alphabetize words using c++

Posted 24 December 2010 - 09:53 AM

I have alrady tried
cin.get();
itstead of
system ("pause>NUL");
but for some reason
cin.get();
does not work for me it plainly just skips it. Instead of waiting to display
cout<< "Do you have another list to alphabetize? 1=yes 2=no \n";
it displays it right away.As for the parts I dont understand are this
vector<string> myStrings;

and all this
for(int i=0; i <numberOfWords; ++i)

{
 cin >> str;   
     myStrings.push_back(str);   
        
        }

sort (myStrings.begin(),myStrings.end());

cout << "Your words now in alphabetical order are\n";

vector<string>::iterator it = myStrings.begin();

for(;it != myStrings.end(); ++it)
{
        cout << *it <<"\n";
} 


This post has been edited by theaj11: 24 December 2010 - 09:55 AM

Was This Post Helpful? 0
  • +
  • -

#13 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: How to alphabetize words using c++

Posted 24 December 2010 - 10:04 AM

It skips the get() because you have "stuff" left in the buffer. Put a cin.sync(); in there to clear it out (or an ignore(), up to you really).
Was This Post Helpful? 1
  • +
  • -

#14 theaj11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 29-August 10

Re: How to alphabetize words using c++

Posted 24 December 2010 - 10:40 AM

thanks kya cin.get() works now
Was This Post Helpful? 0
  • +
  • -

#15 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: How to alphabetize words using c++

Posted 25 December 2010 - 07:21 AM

View Posttheaj11, on 25 December 2010 - 01:53 AM, said:

the parts I dont understand are this
vector<string> myStrings;
This declares a vector of datatype string called 'myStrings'.
Check the link I provided above and read about vectors.

View Posttheaj11, on 25 December 2010 - 01:53 AM, said:

and all this

What are your specific questions?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2