【发布时间】:2020-10-02 21:49:56
【问题描述】:
该计划的目的是为学生制作成绩单。在显示功能中显示最高和最低标记时出错。我在全局范围内声明了变量 int high 和 int low ,认为这可以解决问题-但是它仍然在显示中打印零。-有趣的事情是打印最高和最低 int min max 函数 #包括 使用命名空间标准;
int high; int low;
void studentDetails(string Name, string Surname, string School)
{
cout << "Please provide the following details below.";
cout << "\nName: ";
cin >> Name;
cout << "Surname: ";
cin >> Surname;
cout << "Name of School: ";
cin >> School;
}
getMarks (int& Eng, int& Maths, int& LO, int& His, int& CL, int& Art){
cout << "Please provide the mark obtained (0-100) for the following subjects.";
cout << "\nEnglish: ";
cin >> Eng;
while (Eng>=0 && Eng<=100){
cout << "Mathematics: ";
cin >> Maths;
while (Maths>=0 && Maths<=100){
cout << "Life Orientation: ";
cin >> LO;
while (LO>=0 && LO<=100){
cout << "History: ";
cin >> His;
while (His>=0 && His<=100){
cout << "Computer Literacy: ";
cin >> CL;
while (CL>=0 && CL<=100){
cout << "Art: ";
cin >> Art;
while (Art>=0 && Art<=100){
cout << "All marks have been recorded";
return 0;
}}}}}}}
void AverageYearMark(int avg, int& Eng, int& Maths, int& LO, int& His, int& CL, int& Art){
avg = (Eng + Maths + LO + His + CL + Art)/6;
cout << "\nThe average of the above marks is: ";
cout << avg;
}
minMax(int& Eng, int& Maths, int& LO, int& His, int& CL, int& Art){
int arr[] = {Eng,Maths,LO,His,CL, Art};
int high = arr[0]; //highest mark
for(int i = 1; i<6; i++){
if(arr[i]>high){
high = arr[i];
}
}
cout << "Highest mark";
cout << high;
int low = arr[0]; // lowest mark
for(int i = 1; i<6; i++){
if(arr[i]<low){
low = arr[i];
}
}
cout << "Lowest mark";
cout << low;
return 0;
}
void passorFail (string dec, int& Eng, int& Maths, int& LO, int& His, int& CL, int& Art){
int arr[] = {Maths,LO,His,CL, Art};
int counta = 0;
if(Eng>= 50){
counta++;
for(int i =0; i<5; i++){
if(arr[i]>50){
counta++;
}}
if(counta>=4){
dec = "Pass";
}
else{
dec = "Fail";
}
}
else{
dec = "Fail";
}
cout << dec;
}
void awardDistinction(double& avg, int& Eng, int& Maths, int& LO, int& His, int& CL, int& Art){
int arr1[] = {Eng,Maths,LO,His,CL, Art};
string arr2[6];
string overall;
string distinction = "distinction";
for(int i= 0; i<6;i++){
if(arr1[i]>=75){
arr2[i]= distinction;
}
else{
arr2[i] = " ";
}
}
for (int i = 0; i<6; i++)
cout << arr2[i];
if(avg>=75){
overall = "distinction";
}
}
void codeSymbol(string (&symbol)[6], int (&code)[6],double& avg, int& Eng, int& Maths, int& LO, int& His, int& CL, int& Art){
int arr1[] = {Eng,Maths,LO,His,CL, Art};
//symbol<string,6>;
//code<int,6>;
for (int i=0; i<6; i++){
if(arr1[i]>=80 && arr1[i]<=100){
symbol[i] = "A";
code[i] = 7;
}
if(arr1[i]>=70 && arr1[i]<=79){
symbol[i] = "B";
code[i] = 6;
}
if(arr1[i]>=60 && arr1[i]<=69){
symbol[i] = "C";
code[i] = 5;
}
if(arr1[i]>=50 && arr1[i]<=59){
symbol[i] = "D";
code[i] = 4;
}
if(arr1[i]>=40 && arr1[i]<=49){
symbol[i] = "E";
code[i] = 3;
}
if(arr1[i]>=30 && arr1[i]<=39){
symbol[i] = "F";
code[i] = 2;
}
if(arr1[i]>=0 && arr1[i]<=29){
symbol[i] = "FF";
code[i] = 1;
}
}
int avcode;
string avsym;
if(avg<=100 && avg>=80 ){
avcode = 7;
avsym = "A";
}
if(avg<=79 && avg>=70 ){
avcode = 6;
avsym = "B";
}
if(avg<=69 && avg>=60 ){
avcode = 5;
avsym = "C";
}
if(avg<=59 && avg>=50 ){
avcode = 4;
avsym = "D";
}
if(avg<=49 && avg>=40 ){
avcode = 3;
avsym = "E";
}
if(avg<=39 && avg>=30 ){
avcode = 2;
avsym = "F";
}
if(avg<=29 && avg>=0 ){
avcode = 1;
avsym = "FF";
}
}
在下面的显示函数中打印最高和最低标记时出现错误-它是从 min max 函数打印的,但当我全局获取时却没有
void Dispay(string& Name, string& Surname, string& School, string (&symbol)[6] , int (&mark)[6], int (&code)[6], string (subject)[6]){
cout <<"\n*******************************************************************************" << endl;
cout <<" STUDENT ACADEMIC RECORD " << endl;
cout <<" This program input the learner marks of matric level subjects and prints the student final report" << endl;
cout <<"*******************************************************************************" << endl;
cout <<"Name: " + Name + " " + Surname + " School: " + School;
cout <<"\n\nSubject \t\tMark\t\tSymbol\t\tCode" << endl;
for(int i = 0; i<6;i++){
cout << subject[i] << "\t" << mark[i]<< "%\t\t" + symbol[i] << "\t\t" << code[i] << "\n";
}
cout << "\nAverage Year Mark: " << " 32 " << " with symbol " << " D " << " and code " << " 4 " << endl;
cout << "\nOutcome: " << "Passed" << endl;
cout << "\nThe highest mark was: " << " -" <<high<< endl;
cout << "\nThe lowest mark was: " << " -" <<low<< endl;
cout <<"*******************************************************************************" << endl;
}
int main (){
string n; string sn; string s; string dec;
int Eng; int Maths; int LO; int His; int CL; int Art; int low; int high;
double avg = 0;
string subject[] = {"English ","Mathematics ","Life Orientation ","History ","Computer Literacy","Art "};
string symbol[6];
int code[6];
studentDetails(n,sn,s);
getMarks(Eng, Maths, LO, His, CL, Art);
int mark[] = {Eng, Maths, LO, His, CL, Art};
AverageYearMark(avg, Eng, Maths, LO, His, CL, Art);
minMax(Eng, Maths, LO, His, CL, Art);
passorFail(dec,Eng, Maths, LO, His, CL, Art);
awardDistinction(avg, Eng, Maths, LO, His, CL, Art);
codeSymbol(symbol,code,avg,Eng,Maths,LO,His,CL,Art);
Dispay(n,sn,s,symbol,mark,code,subject);
}
///Output:
Please provide the following details below.
Name: Imraan
Surname: JAcobs
Name of School: WIndereme
Please provide the mark obtained (0-100) for the following subjects.
English: 60
Mathematics: 85
Life Orientation: 62
History: 90
Computer Literacy: 96
Art: 97
All marks have been recorded
The average of the above marks is: 81Highest mark97Lowest mark60Pass distinction distinctiondistinctiondistinction
*******************************************************************************
STUDENT ACADEMIC RECORD
This program input the learner marks of matric level subjects and prints the student final report
*******************************************************************************
Name: School:
Subject Mark Symbol Code
English 60% C 5
Mathematics 85% A 7
Life Orientation 62% C 5
History 90% A 7
Computer Literacy 96% A 7
Art 97% A 7
Average Year Mark: 32 with symbol D and code 4
Outcome: Passed
The highest mark was: -0
The lowest mark was: -0
*******************************************************************************
Process returned 0 (0x0) execution time : 23.190 s
Press any key to continue.
【问题讨论】:
-
请慢慢阅读How to Ask。然后edit你的问题提供minimal reproducible example
-
仅供参考,
std::string类型应通过 reference 传递,或者如果您的函数未更改参数,则应通过const参考传递。这将避免编译器制作副本并将副本传递给您的函数。 -
建议:1) 使用空格而不是制表符。 2)格式(缩进你的代码)。
-
顺便说一句,您可以在同一遍数据中找到最高和最低标记。