问题描述
该程序的目的是为学生创建成绩单。我在显示功能中显示最高和最低标记时出错。我全局声明了变量int high和int low,以为这可以解决问题-但是它仍然在显示屏上显示零。-Funnt的事情是打印最大和最小的int max函数 #包括 使用命名空间std;
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& Art){
avg = (Eng + Maths + LO + His + CL + Art)/6;
cout << "\nThe average of the above marks is: ";
cout << avg;
}
minMax(int& Eng,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& Art){
int arr[] = {Maths,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& Art){
int arr1[] = {Eng,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,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";
}
}
在下面的显示功能中打印最高和最低标记时会出现错误-它是从最小最大功能中打印出来的,但是当我全局获取它时不会出现
void dispay(string& Name,string& Surname,string& School,string (&symbol)[6],int (&mark)[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,Art);
int mark[] = {Eng,Art};
AverageYearMark(avg,Eng,Art);
minMax(Eng,Art);
passorFail(dec,Art);
awarddistinction(avg,Art);
codeSymbol(symbol,code,avg,Art);
dispay(n,s,symbol,mark,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.
解决方法
如果已经在文件开头定义为全局变量,则不要在int low
函数中再次定义int high
和minmax
。
还要在main
函数中将它们删除,因为您是在第三次定义它们时。您永远不会将后者作为函数的参数传递,因此幸运的是它们最终未使用。
这是代码的有效版本:
#include <iostream>
using namespace std;
int high,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& Art){
avg = (Eng + Maths + LO + His + CL + Art)/6;
cout << "\nThe average of the above marks is: ";
cout << avg;
}
minMax(int& Eng,int& Art){
int arr[] = {Eng,Maths,LO,His,CL,Art};
high = arr[0]; //highest mark
for(int i = 1; i<6; i++){
if(arr[i]>high){
high = arr[i];
}
}
cout << "Highest mark";
cout << high;
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& Art){
int arr[] = {Maths,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& Art){
int arr1[] = {Eng,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,Art};
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";
}
}
void Dispay(string& Name,string& Surname,string& School,string (&symbol)[6],int (&mark)[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,Art);
int mark[] = {Eng,Art};
AverageYearMark(avg,Eng,Art);
minMax(Eng,Art);
passorFail(dec,Art);
awardDistinction(avg,Art);
codeSymbol(symbol,code,avg,Art);
Dispay(n,s,symbol,mark,subject);
}
,
好吧,这真是一团糟! -您已经在许多不同的地方声明了int high
和int low
。我做了一些整理工作(距离不够远)。在这里,我基本上删除了int low
和int high
的所有“影子”实例,以仅使用全局实例:https://godbolt.org/z/6KWvz5
您应该做的是帮助自己的工作(然后查看缩进和代码间距)是打开每个编译器警告,您可以动手操作并确保您的代码没有警告。
还要查找“名称阴影”,这是您的问题之一(还有其他问题):https://www.learncpp.com/cpp-tutorial/variable-shadowing-name-hiding/
这又是带有警告标记-pthread -std=c++11 -Wall -Wextra -Wshadow
的经过编辑的代码(然后我将其中一个用// THIS SHADOWS THE GLOBAL INT
注释的阴影变量vars放在其中:https://godbolt.org/z/9W3vY3
请查看所有编译器警告-将警告视为错误! -实际上,您可以使用标志-Werror
告诉编译器执行此操作。