problem string

the error is it can't take the next name that user input.
and can someone tell me how i can sort the name that user input in alphabetically order then print the first name and last name in the order. i try search the google, but i can't understand it.
Last edited on
1
2
3
4
5
6
do
{
cout <<"Max size between (1 to 10)" <<endl;
cout <<"Please enter again :"<<endl;
cin>>people;
}while(people < 1 || people > 10);


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
char *first_name[10], *last_name[10];
	int people = 0 ; 
	char fname[50] = {0} , lname[50] = {0}; 
	cout<<"\n Enter the number of the people ";
	cin>>people ;
	int i = 0 ; 

	for (i=0;i < people;i++)
	{
		cout <<"enter next name:"<<endl;
		cin >> fname;
		first_name[i] = new char[ strlen( fname) + 1 ] ; 
		strcpy ( first_name[i] , fname ); 
		cout <<"enter last name:"<<endl;
		cin >> lname;
		last_name[i] = new char[ strlen( lname) + 1 ] ; 
		strcpy ( last_name[i] , lname ); 
	}

	for (i=0;i < people;i++)
	{
		delete first_name[i];
		delete last_name[i];
	}


i try, but the input prompt the user enter 6 times names,but i just put 3 people.
it suppose to be when the user put 3 people, the program will prompt the user to enter 3 names only.then the program must arrange the name alphabetically.
Last edited on
do you want to sort it by first name or last name ?
nope...just sort it by the first alphabet...

example like:
John
Gary
Ben

the sorder will be:
first:Ben
last:John
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void sort ( char& firtname , char & lastname , int people )
{

		char *temp_name  ; 
		for( int i = 0 ; i < people ; i+)
		{
					if( firtname[0][i]  <   firtname[0][i+1] ) 
				{
					strcpy(temp_name  ,  firtname[i]);
					strcpy(firtname[i] ,  firtname[i+1]);
					strcpy(firtname[i + 1] ,  temp_name);

					//for last name 
					strcpy(temp_name  ,  lastname[i]);
					strcpy(lastname[i] ,  lastname[i+1]);
					strcpy(lastname[i + 1] ,  temp_name);


				}
						
		}
}
Last edited on
but its still get an errors...
i put it before the main, and its says"expression must have object to pointer type".
can i just delete the [0]?
and please explain the code...
what error you are gettin g..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void sort ( char** firtname , char ** lastname , int people )
{

		char *temp_name  ; 
		for( int i = 0 ; i < people ; i++)
		{
					if( firtname[0][i]  <   firtname[0][i+1] ) 
				{
					strcpy(temp_name  ,  firtname[i]);
					strcpy(firtname[i] ,  firtname[i+1]);
					strcpy(firtname[i + 1] ,  temp_name);

					//for last name 
					strcpy(temp_name  ,  lastname[i]);
					strcpy(lastname[i] ,  lastname[i+1]);
					strcpy(lastname[i + 1] ,  temp_name);


				}
						
		}
}
Topic archived. No new replies allowed.