如何让全局变量 int 高和低以在显示功能中打印? (这两个变量显示最高和最低分)

Posted

技术标签:

【中文标题】如何让全局变量 int 高和低以在显示功能中打印? (这两个变量显示最高和最低分)【英文标题】:How do I get Global variables int high and into low to print in display function? (These two variables display the highest and lowest marks) 【发布时间】: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)格式(缩进你的代码)。 顺便说一句,您可以在同一遍数据中找到最高和最低标记。 【参考方案1】:

如果您已在文件开头定义为全局变量,请不要在您的 minmax 函数中再次定义 int lowint high。 在您的 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& 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;
    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& 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;
    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], 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);

【讨论】:

【参考方案2】:

好吧,这真是一团糟! - 你已经在很多不同的地方声明了int highint low。我做了一点整理(有点远不够)。在这里,我基本上删除了 int lowint 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 注释的阴影变量之一:https://godbolt.org/z/9W3vY3

请查看所有编译器警告 - 将警告视为错误! - 事实上,您可以使用标志 -Werror 告诉编译器执行此操作。

【讨论】:

以上是关于如何让全局变量 int 高和低以在显示功能中打印? (这两个变量显示最高和最低分)的主要内容,如果未能解决你的问题,请参考以下文章

FUSE 中 FS 的介绍性文档(高和低 API 理解)

php扩展在全局变量中不存在但是打印phpinfo却存在?

Axure中怎么实现变量值在页面中的传递

从高到低! 英语怎么讲

如何让外部活动的 startActivityForResult 工作?

如何让静态变量在inspector里面显示