Im supposed to write a program that reads in 10 c strings and will sort them alphabetically using (strcmp(list[h],list[j])>0) which basically says if list h is closer to the start of the alphabet then it is true else it is false here is my code

#include <iostream>
#include <cstdlib>
using namespace std;
 
// ----------- Function definitions -------------
     int main()
{
       // -------- Constant declarations --------

       // -------- Variable declarations -------- 
   int i,j,h,count;
    char list[9][31];
char sorted[9][31];
        //--------Main Program Body----------
    cout << "****Alphebetical Sorting Program***"<<endl<<endl;
    cout<<"please enter 10 strings you wish to sort"<<endl;  
    
    for(h=0;h<10;h++){
    cin.get(list[h],31);
	cin.ignore();
}
for(h=0;h<10;h++){ //move through each array
for(j=h+1;j<9-h;j++){//select every element to compare to the first arrray
if (strcmp(list[h],list[j])>0){//if list h comes after list j,
count++;} //increase the count because list h belongs later on the list
}//end comparisons
	strcpy(sorted[count],list[h]); //copy the position of current string into correct position according to count
}//end c string move to next one
cout<<"the sorted list is";
for(h=0;h<10;h++){
cout <<sorted[h];}

cout << endl << endl;
       system("PAUSE");     // Pause the output window for reading
       return 0;
       }

i realize the last bits incredibly criptic so ill breifly explain
basically im trying to say if the 1st element of the list comes after the second element of the list then the count(its proper position) is incremented and moves through the rest of the list until it finds its correct place. eg if "cat" is the first element of the list but "at" and "bat" are before it, when its compared against them count will be incremented twice meaning it belongs in sorted array pos 2

the thing complies properly but freezes after i put in the c strings any advice would be greatly appreciated. thanks!

Put them into an std::vector of std::string's and then std::sort() them:
http://programmingexamples.net/index.php?title=CPP/AlphebetizeString

unfourtantly i dont have a clue what this is and i doubt i could even use it as we havent been taught it yet in my CMPT103 class, ill see if i can figure it out and run it by my instructor before its due and see if i can get away with it, id just like to know why the windows freezing

unfourtantly i dont have a clue what this is and i doubt i could even use it as we havent been taught it yet in my CMPT103 class, ill see if i can figure it out and run it by my instructor before its due and see if i can get away with it, id just like to know why the windows freezing

Ov course you don't, but many people here feel that vectors are the only solution to using arrays. They love suggesting things obviously beyond your level.

Problem 1 is your formatting. Please format your code so we can read it easier. We can't tell your program structure the way you have it. This is something that for some reason instructors seem to think is unnecessary. It isn't.

Based on what I can see, your value count does nothing useful, and definitely not what you think it's doing. Rethink your sort -- it is not a sort, it's just a string mover.

Go through your code by hand and you'll see what I mean.

I see a problem right away

char list[9][31];
char sorted[9][31];
//--------Main Program Body----------
cout << "****Alphebetical Sorting Program***"<<endl<<endl;
cout<<"please enter 10 strings you wish to sort"<<endl;

Your arrays are 9 X 31 and should be 10 X 31.

commented: wrong comment, i did that and it messed it up, i did what i had originally and it was fine +0

Ov course you don't, but many people here feel that vectors are the only solution to using arrays. They love suggesting things obviously beyond your level.

Problem 1 is your formatting. Please format your code so we can read it easier. We can't tell your program structure the way you have it. This is something that for some reason instructors seem to think is unnecessary. It isn't.

Based on what I can see, your value count does nothing useful, and definitely not what you think it's doing. Rethink your sort -- it is not a sort, it's just a string mover.

Go through your code by hand and you'll see what I mean.

i apologize ive only been working with c++ for a couple months so my knowlege base is obviously far inferior, ill go through and format it, but do you at least understand what im trying to do? how exactly should the count work then or is there a better way to do this that im just not seeing?

i apologize ive only been working with c++ for a couple months...

Really? You aren't a seasoned professional? :icon_twisted:
Naturally we can tell. It's obvious...

do you at least understand what im trying to do?

Of course. You want to sort strings. Nothing complicated about understanding. Doing is a little more complicated though.

how exactly should the count work then or is there a better way to do this that im just not seeing?

You don't want to count anything. You want to find two strings that need to be switched and switch them.

What sort are you trying to implement? Do you know how a sort works? I think you need to read up on the concept so you know the basic ideas you need to implement.

And heed what gerard4143 said. He's correct.

commented: no need to be rude, im a begginer and he cant appreciate that +0

Really? You aren't a seasoned professional? :icon_twisted:
Naturally we can tell. It's obvious...


Of course. You want to sort strings. Nothing complicated about understanding. Doing is a little more complicated though.


You don't want to count anything. You want to find two strings that need to be switched and switch them.

What sort are you trying to implement? Do you know how a sort works? I think you need to read up on the concept so you know the basic ideas you need to implement.

And heed what gerard4143 said. He's correct.

you dont have to be such a <SNIPPED>. i got count working without your help why are you here? isnt this supposed to be a forum to help people? the only useful peice of information i obtained was what gerard said here:

#include <iostream>
#include <cstdlib>
using namespace std;
 
// ----------- Function definitions -------------
     int main()
{
       // -------- Constant declarations --------

       // -------- Variable declarations -------- 
   int i,j,h,count;
    char list[9][31];
char sorted[10][31];
        //--------Main Program Body----------
    cout << "****Alphebetical Sorting Program***"<<endl<<endl;
    cout<<"please enter 10 strings you wish to sort"<<endl;  
    
    for(h=0;h<10;h++){
    cin.get(list[h],31);
	cin.ignore();
}
for(h=0;h<10;h++){ //move through each array
count=0;
for(j=0;j<10;j++){//select every element to compare to the first arrray
if (strcmp(list[h],list[j])>0){//if list h comes after list j,
count++;} //increase the count because list h belongs later on the list
}//end comparisons
	strcpy(sorted[count],list[h]); //copy the position of current string into correct position according to count
}//end c string move to next one
cout<<"the sorted list is ";
for(h=0;h<10;h++){
cout <<sorted[h]<<endl;}

cout << endl << endl;
       system("PAUSE");     // Pause the output window for reading
       return 0;
       }

all i was doing wrong was not setting the count to 0 after each pass, an easy fix, if i can see that and you cant maybe im a better programmer than you

you dont have to be such a <SNIPPED>. i got count working without your help why are you here? isnt this supposed to be a forum to help people? the only useful peice of information i obtained was what gerard said here:

And you still got it wrong: char list[9][31];

all i was doing wrong was not setting the count to 0 after each pass, an easy fix, if i can see that and you cant maybe im a better programmer than you

As I said, I couldn't read your code because of the formatting. Still can't.

Because I have no idea what you know, I was justified in asking. I'll remember not to offer help next time since you take offense at it.

And you still got it wrong: char list[9][31];

oh yeah? stick it into your compiler and test it out clearly you are a poser who doesnt know anything about C++

commented: Stop being a beligerant ass. -3

And you still got it wrong: char list[9][31]; As I said, I couldn't read your code because of the formatting. Still can't.

Because I have no idea what you know, I was justified in asking. I'll remember not to offer help next time since you take offense at it.

oh and another thing since arrays start at 0 they should both be 9 both of the suggestions you gave me suck, also formatting means putting spaces in your code. your meaning to tell me you have no brain capacity to seperate cout<< from cout << ?

@beejay321 as you may have already found from personal message sent to you, your behavior is inappropriate. If you want help you need to cooperate and investigated recommended resources. If you believe answer from another member is inappropriate then use "Flag Bad Post" button let the moderators/admins know there is a possible issue, instead of "taking law in your hands" and abuse supposed offender

PS: WaltP was correct in saying that two dimensional array is wrong and you been right it will compile. The thing is that WaltP is right because you are supposed to used arrays of same size. In this scenario so far it is harmless since random array size is smaller then sorted, but if you start using array size method instead of hard coding number of loops in for statement (10) you will get in trouble since there will be null values in last set

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.