Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

sorted names and scores

juanpijuanpi Posts: 1Member
I am a new in programming and I try to compiler this code but I have problem with sorted part. yo want to get this out out put:
josh 89
Jeff 98
but I got
89
Jeff 98

this is my code

#include
#include

using namespace std;

struct StudentGrades
{
string name;
double grade;
};

void sort (StudentGrades *score, int size);


int main ()
{
StudentGrades *sGrade;
int mass;

cout << "Please enter the amount of student grades you wish to enter
";
cin >> mass;
sGrade = new StudentGrades[mass];

for (int index = 0; index < mass; index ++)
{
cout << "Please enter the [NAME] of student #" << (index + 1) << ": ";
cin >> (sGrade + index)->name;

cout << "Please enter the [SCORE] of student #" << (index + 1) << ": ";
cin >> (sGrade + index)->grade;



while (sGrade[index].grade < 0)
{
cout << "Negative numbers are not accepted, please try again with positive values
";
cin >> (sGrade + index)->grade;
}
}

cout << "The sorted names and scores are:";
cout << endl;
sort (sGrade, mass);

//cout << fixed << showpoint << setprecision(2);


delete []sGrade;
sGrade = 0;
system ("pause");
return 0;
}

void sort (StudentGrades *score, int size)
{
int minIdx;
double minGrade;
string id;

for (int scan = 0; scan < (size - 1); scan ++)
{
minIdx = scan;
minGrade = (score + scan)->grade;

for (int count = scan + 1; count < size; count ++)
{
if ((score + count)->grade < minGrade)
{
minGrade = (score + count)->grade;
minIdx = count;
id = (score + count)->name;
}
}

(score + minIdx)->grade = (score + scan)->grade;
(score + scan)->grade = minGrade;
(score + minIdx)->name = (score + scan)->name;
(score + scan)->name = id;

}

for (int count = 0; count < size; count ++)
{
cout << (score + count)->name << " ";
cout << (score + count)->grade << " ";
cout << endl;
}
}


thanks for your help.
Sign In or Register to comment.