user13710970
user13710970

Reputation: 15

Sorting names in an array variable inside a structure alphabetically

I'm making this student record program that my prof asked us to do. It keeps saying cannot convert 'StudRec' to 'StudRec' in assignment and sometimes it says cannot convert char* to const char*. StudRec is my struct variable and this is the function that should sort the recorded names alphabetically.

void sort_name(StudRec name[], StudRec temp[], int studcount)
{
    if (studcount > 0)
    {
        for (int i=0; i<studcount; i++)
        {
           for (int j=studcount; j<=i; j++)
            {
                if(strcmp(name[j].nm, name[j+1].nm) > 0)
                {
                        temp=name[j];
                        name[j]= name[j+1];
                        name[j+1]= temp;
                }
            }
        }
        cout << "\t| |\t\t\t The records have been sorted alphabetically by name.\n";
    }

    else
    {
        cout << "\t| |\t\t\t There is no current record to sort by name.\n\n";
    }
}

Upvotes: 0

Views: 71

Answers (2)

user13710970
user13710970

Reputation: 15

This is the code in the fGrade function:

    void fGrade(StudRec rec[], int studcount)
    {
    string id;

if (studcount > 0)
{
    cout << "\n\n\t| |\t\t         =====================================================\n";
    cout << "\t| |\t\t\t\t         STUDENT'S FINAL GRADE \n";
    cout << "\t| |\t\t         =====================================================\n\n";;
    check:  cout << "\n\t| |\t\t\t Enter student's ID Number: ";
            cin >> id;

            int index = search(rec, id, studcount);

            if (index != -1 && studcount > 0)
            {
                rec[index].fgrd = (((rec[index].quiz / 150) * 100) * 0.2) + (((rec[index].hw / 20) * 100) * 0.1) + (((rec[index].midT / 100) * 100) * 0.15)
        + (((rec[index].fExam / 100) * 100) * 0.15) + (((rec[index].acts / 150) * 100) * 0.25) + (((rec[index].proj / 100) * 100) * 0.15);

        cout << left << setw(10) << "\n ID No." << setw(30) << "NAME" << setw(15) << "FINAL GRADE" << setw(10) << "REMARKS";
        cout << "\n ------------------------------------------------------------\n";
        cout << " " << left << setw(8) << rec[index].id << setw(30) << rec[index].nm << setw(15) << fixed << setprecision(2) << rec[index].fgrd;

        if (rec[index].fgrd >= 75 && rec[index].fgrd <= 100)
        {
            cout << setw(10) << "Passed\n\n";
        }

        else if (rec[index].fgrd < 75)
        {
            cout << setw(10) << "Failed\n\n";
        }
    }

    else
    {
        cout << "\t| |\t\t\t This record does not exist. Check your ID and try again.";
        goto check;
    }
}

else
{
    cout << "\t| |\t\t\t There is no current record to calculate a final grade.\n\n";
    return;
}
    }

and this is the code for the view_rec function:

    void view_rec(StudRec rec[], int studcount)
    {
if (studcount > 0)
{
    cout << "\n\n\t| |\t\t         ===================================================\n";
    cout << "\t| |\t\t\t\t         VIEW STUDENT RECORD \n";
    cout << "\t| |\t\t         ===================================================\n\n";
    int i=0;
    cout << "\n\t| |\t\t         " << left << setw(10) << "ID" << setw(30) << "NAME" << setw(7) << "SEX" << setw(10) << "QUIZ";
    cout << setw(14)<< "ASSIGNMENT" << setw(11) << "MIDTERM" << setw(14) << "FINAL EXAM" << setw(12) << "ACTIVITY";
    cout << setw(11)<< "PROJECT" << setw(9) << "TOTAL\n";
    cout << "\t| |\t\t         " << "----------------------------------------------------------------------------------------------------------------------------\n\n";

    while(i <= studcount)
    {
        if(rec[i].id != "")
        {

            cout << "\t| |\t\t         " << left << setw(10) << rec[i].id << setw(30) << rec[i].nm << setw(7) << rec[i].sex;
            cout << setw(10) << rec[i].quiz << setw(14) << rec[i].hw << setw(11) << rec[i].midT;
            cout << setw(14) << rec[i].fExam << setw(12) << rec[i].acts << setw(11) << rec[i].proj << setw(9) << rec[i].total;
            cout << endl << endl;
        }
        i+=1;
    }
}

else
{
    cout << "\t| |\t\t\t There is no current record to view.\n\n";
    return;
}

}

Upvotes: 0

Dmitry Kuzminov
Dmitry Kuzminov

Reputation: 6604

Ok, assuming that the StudRec has all necessary operations (assignment, default constructor, etc.), you don't need to pass an array of temp values:

void sort_name(StudRec name[], int studcount)
{
    StudRec temp;
    // ...
}

That should fix one issue: you are trying to assign an element to the whole array:

        temp=name[j];

Even better would be to define temp right where you use it:

        const StudRec temp = name[j];

Anyway, I guess you are trying to implement a BubbleSort, and your implementation is incorrect because of the indexing. Should be:

    for (int i = 1; i < studcount; ++i)
        for (int j = 0; j < studcount - i; ++j)

Upvotes: 1

Related Questions