Hi everyone...I am building a program to input students names(first and last) and also GPA. I have everything working but the sort by last name. Everything works fine, even the GPA sort but the last name sort will not. Please help. Below is the function in my program i am trying to use to do the sort. I am using case 1 of the switch statement to do the GPA and case 2 the last name.

void Sort(int length, struct student allstud[], int func)
{

      int i, j, flag = 1;    // set flag to 1 to start first pass
      struct student temp;             // holding variable
      for(i = 1; (i < length) && flag; i++)
     {
          flag = 0;
          for (j=0; j < (length-1); j++)
         {
			
			 switch(func)
			 {

			case 1:

               if (atof(allstud[j+1].GPA) > atof(allstud[j].GPA))      // ascending order simply changes to <
                { 
				  temp = allstud[j+1];
                  allstud[j+1]=allstud[j];
                  allstud[j]=temp;
                    
                    flag = 1;               // indicates that a swap occurred.
               }
			   break;
			case 2:
				{
				if (allstud[j+1].Lastname[0] > allstud[j].Lastname[0])      // ascending order simply changes to <
				{ 
				  temp = allstud[j+1];
				  allstud[j+1]=allstud[j];
				  allstud[j]=temp;
                    
                    flag = 1;               // indicates that a swap occurred.
               }
				 break;
	
	}
	}
 }
}

Look at your code, and tell us if what you see in your IDE resembles the mess you posted?

Also, why are you comparing only one letter of the name, rather than the whole name?

Hi everyone...I am building a program to input students names(first and last) and also GPA. I have everything working but the sort by last name. Everything works fine, even the GPA sort but the last name sort will not. Please help. Below is the function in my program i am trying to use to do the sort. I am using case 1 of the switch statement to do the GPA and case 2 the last name.

void Sort(int length, struct student allstud[], int func)
{

      int i, j, flag = 1;    // set flag to 1 to start first pass
      struct student temp;             // holding variable
      for(i = 1; (i < length) && flag; i++)
     {
          flag = 0;
          for (j=0; j < (length-1); j++)
         {
			
			 switch(func)
			 {

			case 1:

               if (atof(allstud[j+1].GPA) > atof(allstud[j].GPA))      // ascending order simply changes to <
                { 
				  temp = allstud[j+1];
                  allstud[j+1]=allstud[j];
                  allstud[j]=temp;
                    
                    flag = 1;               // indicates that a swap occurred.
               }
			   break;
			case 2:
				{
				if (allstud[j+1].Lastname[0] > allstud[j].Lastname[0])      // ascending order simply changes to <
				{ 
				  temp = allstud[j+1];
				  allstud[j+1]=allstud[j];
				  allstud[j]=temp;
                    
                    flag = 1;               // indicates that a swap occurred.
               }
				 break;
	
	}
	}
 }
}

i think this might b helpful to u. use str.compare

# include <iostream>
# include <string>
using namespace std;
struct student
{
	string Lastname;
};
void Sort(int length, struct student allstud[], int func)
{
	int i, j, flag = 1; // set flag to 1 to start first pass
	struct student temp; // holding variable
	for(i = 1; (i < length) && flag; i++)
	{
		flag = 0;
		for (j=0; j < (length-1); j++)
		{
			switch(func)
			{
			case 1:
				{
					//your code for case 1 here
				}
				case 2:
				{
					if ( allstud[j+1].Lastname.compare(allstud[j].Lastname) < 0) // ascending order simply changes to <
					{
						temp = allstud[j+1];
						allstud[j+1]=allstud[j];
						allstud[j]=temp;
						flag = 1; // indicates that a swap occurred.
					}
					break;
				}
			}
		}
	}
}
int main()
{
	student *st = new student[5];
	int x = 0;
	while (x < 5)
	{
		cout<<"enter last name\n";
		cin>>st[x].Lastname;
		x++;
	}
	Sort(5,st,2);
	x = 0;
	while (x < 5)
	{
		cout<<st[x].Lastname<<" ";
		x++;
	}
	return 0;
}
commented: At least the OP used code tags (grumble) -4

Code tags ppl!

i think this might b helpful to u. use str.compare

# include <iostream>
# include <string>
using namespace std;
struct student
{
string Lastname;
};
void Sort(int length, struct student allstud[], int func)
{
int i, j, flag = 1; // set flag to 1 to start first pass
struct student temp; // holding variable
for(i = 1; (i < length) && flag; i++)
{
flag = 0;
for (j=0; j < (length-1); j++)
{
switch(func)
{
case 1:
{
//your code for case 1 here
}
case 2:
{
if ( allstud[j+1].Lastname.compare(allstud[j].Lastname) < 0) // ascending order simply changes to <
{
temp = allstud[j+1];
allstud[j+1]=allstud[j];
allstud[j]=temp;
flag = 1; // indicates that a swap occurred.
}
break;
}
}
}
}
}
int main()
{
student *st = new student[5];
int x = 0;
while (x < 5)
{
cout<<"enter last name\n";
cin>>st[x].Lastname;
x++;
}
Sort(5,st,2);
x = 0;
while (x < 5)
{
cout<<st[x].Lastname<<" ";
x++;
}
return 0;
}

Ok.... adcodingmaster.... look at your first post, then your second post. The ONLY thing that changed was now you have colors for keywords....

commented: And it still looks like crap! +19
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.