sorting words using strings

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1631 Views - Last Post: 28 January 2009 - 06:21 PM Rate Topic: -----

#1 maria_isabel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 20-January 09

sorting words using strings

Posted 28 January 2009 - 03:46 PM

 This program will read 10 strings and will sort them in alpjhabetical order. */

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{ 

int i;
char list[10][31];
int j;

 
 cout<<"Please enter the 10 strings to sort"<<endl;
//read in 10 strings
for (i=0;i<9;i++){
  cin.get(list[i],31);
  cin.ignore();
  }
for (i=0; i<9; i++) {
cout<<list[i]<<endl;
}


for (i=0;i<9;i++){
	for (j=0;j<9; j++);
	   cout<< list[1] [j];
}
 if (strcmp(list[j],list[j++])<0){
		cout<<list[j]<<endl;
		}//end if
  else {
	   cout<<list [j+1]<<endl;
	   }//end else
   
  //strings have the same contents if strcmp comes back with 0	 
if (strcmp(list[j],list[j+1])==0){
	   cout<<list [j]<<list[j+1]<<endl;
	   }//end if
  
  
  system("PAUSE");
  return 0;
}//end main







sort 10 words in alphabetical order...is not working.

Is This A Good Question/Topic? 0
  • +

Replies To: sorting words using strings

#2 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: sorting words using strings

Posted 28 January 2009 - 04:00 PM

Does it fail to compile or do you just not get the correct output?
Was This Post Helpful? 0
  • +
  • -

#3 maria_isabel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 20-January 09

Re: sorting words using strings

Posted 28 January 2009 - 04:01 PM

View PostGloin, on 28 Jan, 2009 - 03:00 PM, said:

Does it fail to compile or do you just not get the correct output?

It compiles and it just gives me the list of words i put in, it doesnt actually sort them in alphabetical order. :v:
Was This Post Helpful? 0
  • +
  • -

#4 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: sorting words using strings

Posted 28 January 2009 - 04:03 PM

Does it change the order at all?
Was This Post Helpful? 0
  • +
  • -

#5 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: sorting words using strings

Posted 28 January 2009 - 04:05 PM

It simply repeats what you said back at you!
(Hi echo)
Was This Post Helpful? 0
  • +
  • -

#6 maria_isabel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 20-January 09

Re: sorting words using strings

Posted 28 January 2009 - 04:05 PM

no it just prints the words
Was This Post Helpful? 0
  • +
  • -

#7 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: sorting words using strings

Posted 28 January 2009 - 04:06 PM

You have a part that is supposed to compare and eventually swap words, but it's outside of the loop so it only iterates once.
You should take a look at the bubblesort algorithm because as it is now you only compare the words but you never take any action according to the result.

Edited: typo

This post has been edited by Gloin: 28 January 2009 - 04:09 PM

Was This Post Helpful? 0
  • +
  • -

#8 maria_isabel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 20-January 09

Re: sorting words using strings

Posted 28 January 2009 - 04:07 PM

View PostHyper, on 28 Jan, 2009 - 03:05 PM, said:

It simply repeats what you said back at you!
(Hi echo)

yeah but i dont know what to do to sort the list like the if statements are not working or printing out annything
Was This Post Helpful? 0
  • +
  • -

#9 tommod  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 32
  • Joined: 21-July 08

Re: sorting words using strings

Posted 28 January 2009 - 04:34 PM

I think this code does what you wanted. Using the sort algorithm on the string array is a lot less hastle.

// This program will read 10 strings and will sort them in alpjhabetical order. */

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


using namespace std;

int main()
{
	string list[10];

	cout<<"Please enter the 10 strings to sort"<<endl;
	
	//read in 10 strings
	for (int i=0;i<9;i++){
	  cin >> list[i];
	}

	sort(list, list+10); //sort array A-Z

	//output sorted array
	for (int i=0;i<9;i++){
		cout<< list[i] << endl;
	}

	system("PAUSE");
	return 0;
}//end main



t
Was This Post Helpful? 0
  • +
  • -

#10 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: sorting words using strings

Posted 28 January 2009 - 04:36 PM

tommod, If you're going to go that far, why not just go the rest of the way?

Use a vector or list and call upon SORT.
Was This Post Helpful? 0
  • +
  • -

#11 tommod  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 32
  • Joined: 21-July 08

Re: sorting words using strings

Posted 28 January 2009 - 04:54 PM

Correct, you could use a vector and it would be better to do so.

t
Was This Post Helpful? 0
  • +
  • -

#12 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: sorting words using strings

Posted 28 January 2009 - 05:26 PM

View Posttommod, on 28 Jan, 2009 - 03:54 PM, said:

Correct, you could use a vector and it would be better to do so.

t



But ... if the OP needs to use an array of C++ strings and to write the sort routine, then try something like this ...

Shalom,
David
http://developers-he...index.php/topic,46.0.html
http://developers-he.../index.p...opic,106.0.html


//This program asks for N strings to be input and then sorts them in ascending order ...

#include <iostream>  // Since your are using this ...
#include <string> // why not use C++ strings ?

using namespace std;

const int N = 10;


void bubbleSort( string arrayStrings[], int sizeArray )
{
	bool swap;
	int j = 0;
	string tmpStr;
	do
	{
		swap = false;
		// on each pass, the appropriate string is 'bubbled' to the 'top' ...
		for( int i=1; i<sizeArray-j; ++i)
		{
			if( arrayStrings[i-1] > arrayStrings[i] ) // swap
			{
				tmpStr = arrayStrings[i];
				arrayStrings[i] = arrayStrings[i-1];
				arrayStrings[i-1] = tmpStr;
				swap = true;
			}
		}
		++j;
		
	}while( swap ); // done ... when no swaps occured on 'a pass'
}



int main()
{
	cout << "Please enter " << N << " strings to sort ..." << endl;
	string myStrs[N];
	for(int i=0; i<N; ++i)
	{
		cout << "Enter string " << i+1 << " : ";
		getline(cin, myStrs[i]);
	}
	
	cout << "\nThe " << N << " strings NOT sorted ..." << endl;
	for(int i=0; i<N; ++i) cout << myStrs[i] << endl;
	
	bubbleSort( myStrs, N );
	
	cout << "\nThe " << N << " strings sorted ..." << endl;
	for(int i=0; i<N; ++i) cout << myStrs[i] << endl;

	cout << "\nPress 'Enter' to continue ... ";
	cin.get();
}

Was This Post Helpful? 0
  • +
  • -

#13 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: sorting words using strings

Posted 28 January 2009 - 05:34 PM

David, You're trying to compare strings using the > operator. That isn't gonna work, is it? You need to use the strcmp-function.
Was This Post Helpful? 0
  • +
  • -

#14 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: sorting words using strings

Posted 28 January 2009 - 06:10 PM

View PostGloin, on 28 Jan, 2009 - 04:34 PM, said:

David, You're trying to compare strings using the > operator. That isn't gonna work, is it? You need to use the strcmp-function.


You are thinking about C strings ...

C++ strings are readily compared with ==, >, >=, <, <=, != ...

Shalom,
David
http://developers-he...index.php/topic,46.0.html
http://developers-he.../index.p...opic,106.0.html
Was This Post Helpful? 0
  • +
  • -

#15 KYA  Icon User is offline

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

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: sorting words using strings

Posted 28 January 2009 - 06:12 PM

Doing that only compares the first letter doesn't it? I recall a snippet where the person was trying to order an array of C++ strings and using those conditional operators only returned the first letter of each, which threw of her program.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2