用数字和字母C++显示期末成绩
Showing Final grade in numbers and letters C++
如果可以的话,请帮忙我做了一些更改,但仍然坚持显示输出的最终成绩:(很抱歉我没有发布确切的问题是什么(现在我发布了)。这是任务:教师需要输入成绩并获得最终成绩
#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';
}
}
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 字符串-C++后显示的随机字符
- 继承期间显示未知行为的子类
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 程序崩溃并显示"std::out_of_range"错误
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 从数据库实时显示QT c++中的数据
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 显示错误输出的简单数组排序程序
- Qt自定义QPush按钮未显示在布局上
- C++射线示踪剂ppm表示没有足够的数据来显示图像
- 显示基于用户输入的整数的字符
- 使用QTreeView,如何通过调用函数只突出显示特定的行/列
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- 密码登录程序将永远循环并显示不正确的结果
- std::strftime 在 Windows 中使用 GMTIME 将时区 UTC 显示为 UTC+1