I think i have a problem ..with having the user enter an array of strings ...I know how to compare the strings and sort them...i just can't grasp the concept
practice makes perfect..:D
Printable View
I think i have a problem ..with having the user enter an array of strings ...I know how to compare the strings and sort them...i just can't grasp the concept
practice makes perfect..:D
So, your question is?
>I know how to compare the strings and sort them...i just can't
>grasp the concept
If you don't get the concept, then how do you know to compare and sort strings??
I suggest you write another post to tell us your problem, since
>practice makes perfect..
Please try harder on the board before sending private requests for assistance! Ok?
Quote:
Can you help me with something
How do I make this program more user
friendly..I want the USER to enter the names as oppossed to names already entered...
#include<iostream.h>
#include<string.h>
#include<stdio.h>
void SortApplicants( char*[]);
//SORTING STRINGS ARE HARD
main()
{
//I have to have the user enter the input
const int size = 80;
char sentence[size];
cin.getline( sentence, size);
//for ( int i = 0; i < 10; i++ )
SortApplicants(sentence);//Sort routine called
//ERROR BECAUSE???
return 0;
}
//********************************
void SortApplicants(char *alpha[])
{
char *temp;
int pass, k, row;
for(pass = 0; pass < 10; pass++)
{ for(k = pass; k >=1; k--)
{ if(strcmp(alpha[k], alpha[k-1]) > 0)
{ temp = alpha[k];
alpha[k] = alpha[k-1];
alpha[k-1] = temp;
}
}
}
for(row = 0; row < 5; row ++)
{ printf("%s", alpha[row]);
printf("\n");
}
return;}
//end program
__________________
I guess no ones perfect
Code:
#include<iostream.h>
#include<string.h>
#include<stdio.h>
#include<conio.h>
#include<conio.c>
void Print(char *str)
{
printf("%s", str);
getch();
}
bool SortApplicants(char applicants[][80], int number_of_applicants);
//SORTING STRINGS ARE HARD
int main(void)
{
//I have to have the user enter the input
int number_of_applicants = 0;
const int size = 80;
cout << "\nHow many applicants?\n" << endl;
cin >> number_of_applicants;
cout << "\nYou May Begin:\n" << endl;
char applicants[ number_of_applicants ][size];
for ( int i = 0; i < number_of_applicants; i++ )
fgets( applicants[i], size, stdin ); //...cin.getline() is buggy! Don't use it!
bool necessary = SortApplicants(applicants, number_of_applicants);
if( !necessary ) //...just for fun
Print("\nThe Names Were Already In Order!");
cout << "\n And Here Are The Results:\n" << endl;
for ( int i = 0; i < number_of_applicants; i++ )
Print(applicants[i]);
Print("Done!");
//SortApplicants(sentence);
//...No. "sentence" is an array of 80 chars...
//..."applicants" is an array of arrays of 80 chars, see? - (80 X number_of_applicants)
return 0;
}
bool SortApplicants(char applicants[][80], int number_of_applicants)
{
int pass;
bool done = false;
bool already_sorted = true;
char temp[80];
while( !done )
{
done = true; //...reset so we can break out when done...
for(pass = number_of_applicants -1; pass > 0 ; pass--)
{
if(strcmp(applicants[pass], applicants[pass - 1]) < 0)
{
strcpy(temp,applicants[pass - 1]);
strcpy(applicants[pass - 1],applicants[pass]);
strcpy(applicants[pass],temp);
done = false; //...keep going till this doesn't happen in a full pass...
already_sorted = false;
}
}
}
if(already_sorted)
return false;
else
return true;
}