1. I wonder whats the problem. Has no error and warning.. but can not sort names. Why? Whats the mistake(s)? "red color"
2. why if i use tolower the character.. got problem...whats the problem? "blue color"


thank you in advance...

#include <iostream>
#include <string>
using namespace std;
int main()
{
string *name;
string temp;
int num;

/* ********Enter number of students*********** */
cout << "Please enter the numbers of student: ";
cin >> num;
name = new string [num];

/* *********Sorting names by alphabet************* */
for (int i=0; i < num; i++)
{
cout << "Enter student name [" << i+1 << "]: ";
getline(cin,name);
name = tolower(name);
}

for (int pass=0; pass <3; pass++)
for (int i=0; i <3; i++)
if ( name > name[i+1] )
{
temp = name;
name = name[i+1];
name[i+1] = temp;
}

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

cout << "\nList of student <sorted>:\n";

for ( i = 0; i < num; i++ )
cout << name << endl;

/* ********* Listing names that contain "Mo "************ */
cout << "\nList of student name that contains the string Mo:\n";

for ( int b= 0; b < num; b++ )
{
if ( name.find( "mo" ) == 0 )
cout << name << endl;
}
delete [] name;

return 0;
}

Your sort seems like it would work if you had exactly 4 names, because you have all these hardcoded '3's in there. Change that to 'num' an it should work better. An improvement still would be to loop through passes until there are no swaps made (because then everything is in the right order already).

The tolower() makes everything the same lower case, but without it things should still sort right, except that "anthony" is before "Betty" because all upper case letters are lower than lower case letters. So, it depends on what kind of output you want.

name[i] = tolower(name[i]);

The function tolower operates on a single character, not a std::string.

for ( int pass=0; pass <3; pass++ )
 	  for ( int i=0; i <3; i++ )
 		 if ( name[i] > name[i+1] )
 		 {
 			temp = name[i];
 			name[i] = name[i+1];
 			name[i+1] = temp;
 		 }

If i is 2, then i + 1 is 3, which is off the end of the array.

This article has been dead for over six months. Start a new discussion instead.