954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

names in ascending order problem

this program should output the names in ascending order which entered by the user. i know there is wrong under if(strcmp...) part..because i do not how to order it...
can someone show me how to continue..
thank you..

#include
#include
using namespace std;
int main()
{
char name[4][50];
//char temp[50];


for (int i=0; i <=3; i++)
{
cout << "Enter student name [" << i+1 << "]: ";
cin.getline(name[i], 50 );
}


if ( strcmp(name[i], name[i+1]) > 0)
{
for (int i=0; i<4 ; i++){
//strcpy(temp,name[i]); //not sure!!
strcpy(name[i+1],name[i]);
//strcpy(temp,name[i+1]);
//strcpy(name[i+1],name[i]);
}

for (int a=0; a < 4 ; a++)
cout << name[a][50] << endl;

}


else

for (int b=0; b<=3 ; b++)
cout << name[b]<< endl;


return 0;
}

koh
Junior Poster in Training
73 posts since Jul 2004
Reputation Points: 11
Solved Threads: 0
 

Have you done swapping of integers? The same principle applies. Make a temporary copy of a, write b to a, write the temporary copy to b. With integers, you could use simple assignment; with C-style strings, you use strcpy.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You