如何将一系列值初始化为变量例如80至100 = A和75至79 = B等

问题描述

**问题在于它不起作用,因为它会一直为所有平均值打印A。即使一个人的平均分是30,它仍然说您的平均分是30,而您的成绩是A。**

#include<iostream>
#include<string>
using namespace std;
#define newline '\n';
int main()
{
    int studentno;
    string name;
    float marks[8];
    float sum=0;
    double average;


    cout<<"Please enter your full names"<<newline;
    getline(cin,name);
    cout<<endl;

    cout<<"Please enter your student number :"<<newline;
    cin>>studentno;
    cout<<endl;

    for(int i = 0; i<8; i++)
    {
        cout<<"Please enter the marks of the student :";
        cin>>marks[i];
        cout<<endl;
        sum += marks[i];
    }

    average = sum/8;

    #So from this point,I have subdivided the sections into different ranges whereby if a person has an average of 85 then its an A,and if an average of 76 then its an A-. The problem is it doesn't work since it keeps printing an A for all averages.  

    if(80.0<= average <=100)
    {
      cout<<"Your average is :"<<average<<" and your grade is A "<<endl;
    }

    else if(75.0<= average <=79.9)
    {
        cout<<"Your average is :"<<average<<" and your grade is A- "<<endl;
    }

    else if(70.0<= average <=74.9)
    {
        cout<<"Your average is :"<<average<<" and your grade is B+ "<<endl;
    }

    else if(65.0<= average <=69.9)
    {
        cout<<"Your average is :"<<average<<" and your grade is B "<<endl;
    }

    else if(60.0<= average <=64.9)
    {
        cout<<"Your average is :"<<average<<" and your grade is B- "<<endl;
    }

    else if(55.0<= average <=59.9)
    {
        cout<<"Your average is :"<<average<<" and your grade is C+ "<<endl;
    }

    else if(50.0<= average <=54.9)
    {
        cout<<"Your average is :"<<average<<" and your grade is C "<<endl;
    }

    else if(45.0<= average <=49.9)
    {
        cout<<"Your average is :"<<average<<" and your grade is C- "<<endl;
    }

    else if(40.0<= average <=44.9)
    {
        cout<<"Your average is :"<<average<<" and your grade is D+ "<<endl;
    }

    else if(35.0<= average <=39.9)
    {
        cout<<"Your average is :"<<average<<" and your grade is D "<<endl;
    }

    else if(31.0<= average <=34.9)
    {
        cout<<"Your average is :"<<average<<" and your grade is D- "<<endl;
    }

    else if(0.0<= average <=30.9)
    {
        cout<<"Your average is :"<<average<<" and your grade is E "<<endl;
    }

    else
    {
        cout<<"Invalid marks input please check again and correct where you went wrong"<<endl;
    }

    return 0;
}

这是输出

Please enter your full names
James Bond
Please enter your student number :
007
Please enter the marks of the student :12
Please enter the marks of the student :20
Please enter the marks of the student :30
Please enter the marks of the student :23
Please enter the marks of the student :24
Please enter the marks of the student :21
Please enter the marks of the student :7
Please enter the marks of the student :13
Your average is :18.75 and your grade is A

如您所见,它说平均分数18.75等于A,这不是我在代码上写的。

解决方法

这种情况

if(80.0<= average <=100)

不检查average是否在80到100之间。

相反,条件的计算结果为if((80.0<= average) <=100),结果为if(0 <=100)if(1 <=100),两者始终为真,因此您总是得到平均成绩为{ {1}}。

进行检查的正确方法是:

A