hi, so i am currenty working on a program that needs to ask the user for student name, and test score. it must use parallel arrays and must sort the scores ascending order from lowest to highest, with corresponding student name entered. and it must average the amount of scores and display the average. the program runs fine. but incase i try to use 5 students and scores, it only displays the name AND score for the last entry.(5th entry). it must do it for all entries.

here is the code. thank you for your help. it is much appreciated.

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

struct StudentGrades
{
    string name;
    double grade;
};

using namespace std;
void sortData(StudentGrades * scores, int size);
double showAverage(StudentGrades *score, int size);
int main()
{
    StudentGrades *sGrade;
    int mass;

    cout << "How many test scores do 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;

    sortData(sGrade, mass);
    //cout << fixed << showpoint << setprecision(2);
    cout << "The average of all the student's scores are:" << showAverage(sGrade, mass);
    cout << endl;

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

void sortData(StudentGrades *score, int size)
{
    int minId;
    double minGrade;
    string id;

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

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

        (score + minId)->grade = (score + scan)->grade;
        (score + scan)->grade = minGrade;
        (score + minId)->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;
    }
}


void getData(int size, int testScore[], char studentName[][6]) {

    int index;

    for (index = 0; index <= size - 1; index++)
    {
        for (index = 0; index <= size - 1; index++)
        {
            cout << "Enter" << (index + 1) << " the student name: "
                << ": ";
            cin >> studentName[index];

            cout << "Enter test score for " << studentName[index]
                << ": ";
            cin >> testScore[index];

        }
    }

    while (0 > testScore[index] || testScore[index] > 100) {
        cout << "Re-enter test score for " << studentName[index]
            << ": ";
        cin >> testScore[index];

    }

}

double showAverage(StudentGrades *score, int size)
{
    double total = 0.0,
        average;

    for (int count = 0; count <size; count++)
    {
        total += (score + count)->grade;
    }

    average = total / size;
    return average;

}

here is the output i am generating at the current time.

How many test scores do you wish to enter?
5
Please enter the name of student #1: kenny
please enter the score of student #1: 10
Please enter the name of student #2: robert
please enter the score of student #2: 20
Please enter the name of student #3: charyan
please enter the score of student #3: 30
Please enter the name of student #4: john
please enter the score of student #4: 40
Please enter the name of student #5: benny
please enter the score of student #5: 60
The sorted names and scores are:
10
20
30
40
benny 60
The average of all the student's scores are:32
Press any key to continue . . .

Dear kenshinbatto92,

apart from a million things I would do differently, the described effect has nothing to do with the amount of students you enter. Variable id is not initialized after line 62, where you should insert id = (score + scan)->name;.
You call this C++, yet your program resembles C programming in most parts. Try to use objects as objects (including their copy or move operators) and put them into collections (STL). Use the library sort algorithm, unless you are specifically attempting to implement something like that for learning reasons. If you try to learn C++, look at the features C++ gives you and only do C coding, when you have to interface with C code, which you can not change for some reason.

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.