Hjarnor Hjarnor - 3 months ago 16
C++ Question

one element of list is sorted others aren't

Ok so i'm writing a program in wich i have to input the names, first names, and grades of n students.After i've done that i have to arrange them alphabetically and if the names match go after the first names.Then i have another option to sort after grades.This is my code so far:

#include<iostream>
#include<string.h>
#include<conio.h>
#include<stdio.h>

using namespace std;

struct student
{
char name[30];
char firstname [50];
int grade;
}s[50];

int i,n,o,done=0;


void add_student()
{
cout<<"Number of students:"<<" ";
cin>>n;
cout<<endl;


for(i=1; i<=n; i++)
{

fflush(stdin);
cout<<"Student"<<" "<<i<<":"<<endl;

cout<<"name of student:"<<" ";
gets(s[i].name);

cout<<"first name of student:"<<" ";
gets(s[i].firstname);

cout<<"grade of student:"<<" ";
cin>>s[i].grade;
cout<<endl;
}
}


void sort_name()
{
char temp[30];
while(!done)
{
done=1;
for(i=1; i<n; i++)
{
if(strcmp(s[i].name,s[i+1].name)>0)
{
strcpy(temp,s[i].name);
strcpy(s[i].name,s[i+1].name);
strcpy(s[i+1].name,temp);

}
}
}

for(i=1; i<=n; i++)
{
cout<<"Student"<<" "<<i<<":"<<endl;
cout<<"Name"<<":"<<s[i].name<<endl;
cout<<"Firstname"<<":"<<s[i].firstname<<endl;
cout<<endl;
}
}


void sort_grade()
{
int temp;
while(!done)
{
done=1;
for(i=1;i<n;i++)
{
if(s[i].grade>s[i+1].grade)
{
temp=s[i].grade;
s[i].grade=s[i+1].grade;
s[i+1].grade=temp;
done=0;
}
}
}

for(i=1; i<=n; i++)
{
cout<<"Student"<<" "<<i<<":"<<endl;
cout<<"Name"<<":"<<s[i].name<<endl;
cout<<"Firstname"<<":"<<s[i].firstname<<endl;
cout<<"Grade"<<":"<<s[i].grade<<endl;
cout<<endl;
}


}

void list_students()
{
int i;
for(i=1; i<=n; i++)
{
cout<<"Student"<<" "<<i<<":"<<endl;
cout<<"Name"<<":"<<s[i].name<<endl;
cout<<"Firstname"<<":"<<s[i].firstname<<endl;
cout<<"Grade"<<":"<<s[i].grade<<endl;
cout<<endl;
}
}



int main()
{
do
{

cout<<"Menu:"<<endl;
cout<<"1.Add students"<<endl;
cout<<"2.Sort by name"<<endl;
cout<<"3.Sort by grade"<<endl;
cout<<"4.List students"<<endl;
cout<<"5.Exit"<<endl<<endl;
cout<<"Pick option : ";
cin>>o;
cout<<endl;

switch (o)
{
case 1:add_student();
break;
case 2:sort_name();
break;
case 3:sort_grade();
break;
case 4:list_students();
break;
}
}while (o!=5);
}


Problem is whenever i sort by grade if for example student A has a grade of 9 and student B has a grade of 7 after sorting it will tell me that student A has a 7 and student B a 9.I know that i am telling the sorting function to only sort the grade and not thouch the name but i dont know how to fix it.It is probably something really simple and i think it involves some kind of pointer but i really dont know.oh and if you can give some kind of info on how to do the sorting after firstname if names are the same bit.I would be really grateful if someone could help me.thanks in advance!!(p.s if anyone can give me any pointers as to how this problem would look like in c i would be really really grateful)

Answer

Instead of swapping only parts of you structure (s[i].grade inside sort_grade() and s[i].name inside sort_name()) you should swap the entire structure. It should be something like the following:

void swap(int n, int m)
{
    char temp_string[50];
    int  temp_int;

    strcpy(temp,s[n].name);
    strcpy(s[n].name,s[m].name);
    strcpy(s[m].name,temp);

    strcpy(temp,s[n].firstname );
    strcpy(s[n].firstname ,s[m].firstname );
    strcpy(s[m].firstname ,temp);

    temp_int = s[n].grade;
    s[n].grade = s[m].grade;
    s[n].grade = temp_int;
}

void sort_name()
{
    char temp[30];
    while(!done)
    {
        done=1;
        for(i=1; i<n; i++)
        {
            if(strcmp(s[i].name,s[i+1].name)>0)
            {
                swap(i, i+1);
            }
       }
   }
}