1

This is the function that has a setprecision to round a grade by two. By the way, I'm making a student record program so it is for the grades.

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 << "\n\t| |\t\t         " << left << setw(10) << "ID No." << setw(30) << "NAME" << setw(15) << "FINAL GRADE" << setw(10) << "REMARKS";
        cout << "\n\t| |\t\t         ------------------------------------------------------------\n";
        cout << "\n\t| |\t\t         " << left << setw(10) << 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.\n";
        goto check;
    }
}

and this is the view_record function where the setprecision in the fGrade is also applying. This is where the raw scores such as 150. But when I execute first the fGrade function then I go to view function, the 150 becomes 150.00

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

}

however if i execute the view function first, it does not have .00 in it.

any help would be very much appreciated. :> I am using a struct by the way.

  • Both functions write to the same `cout` and most manipulators have lasting effects (including `setprecision`, excluding `setw`). See for example [Which iomanip manipulators are 'sticky'?](https://stackoverflow.com/questions/1532640/which-iomanip-manipulators-are-sticky). – dxiv Jun 10 '20 at 04:44
  • So is there any way to unstick it? – user13710970 Jun 10 '20 at 05:04
  • It seems the real question here is "how to return to the default precision", which is answered by https://stackoverflow.com/questions/12560291/set-back-default-floating-point-print-precision-in-c – Tas Jun 10 '20 at 05:14
  • @user13710970 One way to do it is described at the end of the accepted answer in the link from my previous comment. See also [Restore the state of std::cout after manipulating it](https://stackoverflow.com/questions/2273330/restore-the-state-of-stdcout-after-manipulating-it). – dxiv Jun 10 '20 at 05:16

0 Answers0