0

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";
    }
}
3
  • You need to provide at least the declaration of StudRec, the actual compiler error, and the caller code. anyway, even if that would compile, your inner loop would always be skipped becausej is always greater than i. Commented Jun 9, 2020 at 4:49
  • First, j will always be more than i, since you're initializing it at studcount, which i will never reach. I'm assuming you want that to be int j = studcount; j>i; j--. Second, show StudRec and which line the errors are happening on. Commented Jun 9, 2020 at 4:50
  • struct StudRec { string id; char nm[50]; char sex, temp; float acts, quiz, hw, midT, fExam, proj, total=0, fgrd=0; }; Oh, I'm sorry here is the declaration of StudRec and the lines of error is in the temp=name[j] The studcount variable is the one that counts how many records is added Commented Jun 9, 2020 at 4:54

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

3 Comments

It fixes my problem man. It compiles. But the problem now is that I input 3 students and their names are "JJ Perez", "Abad Ry", and "Sassy Dy". But when the function sort_name is executed then I view all the records, "'Abad Ry" did not come first in the record list. JJ is still in the first line
See my comment regarding the indexing.
Last question man, there is this problem with my setprecision. I have a function to get the final grade of students with setprecision when I cout it so that it rounds the grade off by 2. When the code runs and I calculate the final grade. It is correct, but when I view all the records,(I also have a different function to view all records) the raw scores are also displaying with setprecision in it(such as 150.00) . But when I view first all the records it does not have any .00.
0

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;
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.