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 <iostream>
#include <string>
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 \n";
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 \n";
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.