用数字和字母C++显示期末成绩

Showing Final grade in numbers and letters C++

本文关键字:显示 C++ 数字      更新时间:2023-10-16

如果可以的话,请帮忙我做了一些更改,但仍然坚持显示输出的最终成绩:(很抱歉我没有发布确切的问题是什么(现在我发布了)。这是任务:教师需要输入成绩并获得最终成绩

#include<iostream>
#include<iomanip>
using namespace std;
double FinalGrade;
const int students=3;
const int grades=5;
void FillGrades(int[][grades],int,int);
void PrintGrades(int[][grades],int,int);
void GetFinal(int[][grades],int,int);
    int main()
    {
            double FinalGrade=0.0;
    int CSC212[students][grades];
    FillGrades(CSC212,students,grades);
    PrintGrades(CSC212,students,grades);
    double FinalValue[] = {0.05,0.1,0.2,0.25,0.4};
    GetFinal(CSC212,students,grades);

    system("pause");
    return 0;
}
void FillGrades(int setofgrades[][grades],int pupils,int tests)
{
cout<<"Please enter grades for student 1 :"<<endl;
    do{cout<<"Enter Grade 1 :"<<' '; cin>>setofgrades[0][0];}while(setofgrades[0][0]<0||setofgrades[0][0]>100);
    do{cout<<"Enter Grade 2 :"<<' '; cin>>setofgrades[0][1];}while(setofgrades[0][1]<0||setofgrades[0][1]>100);
    do{cout<<"Enter Grade 3 :"<<' '; cin>>setofgrades[0][2];}while(setofgrades[0][2]<0||setofgrades[0][2]>100);
    do{cout<<"Enter Grade 4 :"<<' '; cin>>setofgrades[0][3];}while(setofgrades[0][3]<0||setofgrades[0][3]>100);
    do{cout<<"Enter Grade 5 :"<<' '; cin>>setofgrades[0][4];}while(setofgrades[0][4]<0||setofgrades[0][4]>100);
    cout<<"Please enter grades for student 2 :"<<endl;
    do{cout<<"Enter Grade 1 :"<<' '; cin>>setofgrades[1][0];}while(setofgrades[1][0]<0||setofgrades[1][0]>100);
    do{cout<<"Enter Grade 2 :"<<' '; cin>>setofgrades[1][1];}while(setofgrades[1][1]<0||setofgrades[1][1]>100);
    do{cout<<"Enter Grade 3 :"<<' '; cin>>setofgrades[1][2];}while(setofgrades[1][2]<0||setofgrades[1][2]>100);
    do{cout<<"Enter Grade 4 :"<<' '; cin>>setofgrades[1][3];}while(setofgrades[1][3]<0||setofgrades[1][3]>100);
    do{cout<<"Enter Grade 5 :"<<' '; cin>>setofgrades[1][4];}while(setofgrades[1][4]<0||setofgrades[1][4]>100);
    cout<<"Please enter grades for student 3 :"<<endl;
    do{cout<<"Enter Grade 1 :"<<' '; cin>>setofgrades[2][0];}while(setofgrades[2][0]<0||setofgrades[2][0]>100);
    do{cout<<"Enter Grade 2 :"<<' '; cin>>setofgrades[2][1];}while(setofgrades[2][1]<0||setofgrades[2][1]>100);
    do{cout<<"Enter Grade 3 :"<<' '; cin>>setofgrades[2][2];}while(setofgrades[2][2]<0||setofgrades[2][2]>100);
    do{cout<<"Enter Grade 4 :"<<' '; cin>>setofgrades[2][3];}while(setofgrades[2][3]<0||setofgrades[2][3]>100);
    do{cout<<"Enter Grade 5 :"<<' '; cin>>setofgrades[2][4];}while(setofgrades[2][4]<0||setofgrades[2][4]>100);
}
void PrintGrades(int setofgrades[][grades],int pupils,int tests)
{
    cout<<"Std"<<setw(5)<<"Att"<<setw(14)<<"Ass"<<setw(14)<<"EI"<<setw(14)<<"EII"<<setw(12)<<"FE"<<endl;
    for(int i=0;i<pupils;i++){
        cout<<i+1;
        for(int j=0;j<tests;j++){
            cout<<"      "<<setofgrades[i][j]<<"     ";
        }
        cout<<endl;
    }
}
void GetFinal(int setofgrades[][grades],int pupils,int tests)
{   
    double a[] = {0.05,0.1,0.2,0.25,0.4};
    for(int i=0;i<pupils;i++){
        for(int j=0;j<tests;j++){
            double FinalGrade=0.0;
           FinalGrade+=setofgrades[i][j]*a[j];
        }
    }
    cout<<"Std"<<setw(5)<<" Final Grade "<<endl;
   for(int i=0;i<pupils;i++){

    cout<<i+1;
    double FinalGrade;
    cout<<"    "<<FinalGrade<<endl;
   }

}

尝试添加此行:

void GetFinal(int setofgrades[][grades],int pupils,int tests)
{
    for(int i=0;i<pupils;i++){
        for(int j=0;j<tests;j++)
            double FinalGrade=(setofgrades[i][j]*0.05)
                             +(setofgrades[i][j]*0.1)
                             +(setofgrades[i][j]*0.2)
                             +(setofgrades[i][j]*0.25)
                             +(setofgrades[i][j]*0.4);
/* --> */ cout << "Final grade: " << FinalGrade << "n";
     }    
}

您没有显示最终成绩,因为没有显示最终成绩变量内容的输出语句。

我不确定你是否希望cout在循环中。计算是针对每个循环执行的,但不会存储在任何位置。

循环内部看起来像:

    void GetFinal(int setofgrades[][grades],int pupils,int tests)
    {
        for(int i=0;i<pupils;i++){
            for(int j=0;j<tests;j++)
            {
                double FinalGrade=(setofgrades[i][j]*0.05)
                                 +(setofgrades[i][j]*0.1)
                                 +(setofgrades[i][j]*0.2)
                                 +(setofgrades[i][j]*0.25)
                                 +(setofgrades[i][j]*0.4);
    /* --> */ cout << "Final grade: " << FinalGrade << "n";
            }
        }
}

我修改了您的一些代码,并添加了计算最终成绩的函数。有很多改进需要进行(从传递给函数的参数开始),但至少现在它可以工作了。

#include<iostream>
#include<iomanip>
#include <limits>  // only for std::numeric_limits<std::streamsize>::max()

const int students = 3;
const int grades = 5;
const int MIN_GRADE = 0;
const int MAX_GRADE = 100;
constexpr size_t SS_MAX = std::numeric_limits<std::streamsize>::max(); // You can use SS_MAX = 128 or something else
void FillGrades( int[][grades], int, int);
void PrintGrades( int[][grades], int, int);
void GetFinal( int[][grades], double[students], int, int);
void GetChar( double [students], char[students], int);
int main() {
    int CSC212[students][grades];
    double FinalValue[students];
    char FinalChar[students];
    FillGrades(CSC212, students, grades);
    PrintGrades(CSC212, students, grades);
    GetFinal(CSC212, FinalValue, students, grades);
    GetChar(FinalValue,FinalChar, students);
    return 0;
}
void FillGrades( int setofgrades[][grades], int pupils, int tests ) {
    for ( int i = 0; i < pupils; ++i ) {
        std::cout << "Please, enter grades for student " << (i + 1) << ":n";
        for ( int j = 0; j < tests; ++j ) {
            int grade;
            std::cout << "Enter Grade " << (j + 1) << ": ";
            std::cin >> grade;
            while ( std::cin.fail() || grade < MIN_GRADE || grade > MAX_GRADE ) { 
                std::cout << "Wrong value, please enter a number between " << MIN_GRADE << " and " << MAX_GRADE << ": ";
                std::cin.clear();
                std::cin.ignore(SS_MAX,'n'); // ignore what remains in buffer
                std::cin >> grade;
            }
            setofgrades[i][j] = grade;
            std::cin.ignore(SS_MAX,'n'); // only one value per line entered
        }
    }
}
void PrintGrades( int setofgrades[][grades], int pupils, int tests ) {
    std::string border(45,'-');
    std::cout << 'n' << border << 'n';
    std::cout << std::setw(5) << "Std" << std::setw(8) << "Att" << std::setw(8) << "Ass" << std::setw(8) << "EI" << std::setw(8) << "EII" << std::setw(8) << "FEn";
    std::cout << border << 'n';
    for( int i = 0; i < pupils; i++ ) {
        std::cout << std::setw(5) << (i + 1);
        for( int j = 0; j < tests; j++ ) {
            std::cout << std::setw(8) << setofgrades[i][j];
        }
        std::cout << 'n';
    }
    std::cout << border << 'n';
}
void GetFinal( int setofgrades[][grades], double FinalValue[students], int pupils, int tests) {
    //double FinalValue[students];
    double credits[] = { 0.05, 0.1, 0.2, 0.25, 0.4 };
    for( int i = 0; i < pupils; i++ ) {
        double final = 0;
        for ( int j = 0; j < tests; j++ ) {
            final += setofgrades[i][j] * credits[j];
        }
        FinalValue[i] = final;     
    }
    std::cout << 'n';
    for( int i = 0; i < pupils; i++ ) {    // You don't really need another loop...
        std::cout << "Student " << ( i + 1) << " final grade: " << std::setw(6) << std::setprecision(2) << std::fixed << FinalValue[i] << 'n';
    }   
}
void GetChar( double FinalValue[students], char FinalChar[students], int pupils) {
    std::cout << "nFinal grades:n";
    for( int i = 0; i < pupils; i++ ) {
        if ( FinalValue[i] < 60 ) {
            FinalChar[i] = 'F';
        } else if ( FinalValue[i] < 70) {  // what happened to E?
            FinalChar[i] = 'D';
        } else if ( FinalValue[i] < 80) {
            FinalChar[i] = 'C';
        } else if ( FinalValue[i] < 90) {
            FinalChar[i] = 'B';
        } else {
            FinalChar[i] = 'A';
        }
        std::cout << "Student " << ( i + 1 ) << " final grade: " << FinalChar[i] << 'n';
    }
}