cringyfudge420 cringyfudge420 - 1 year ago 66
C++ Question

C++ condition is right but does not work

I'm trying to build a simple program that asks for a grade whether it is numerical or general weighted average e.g. 75, 1.5 respectively. And when the user input a non numerical number a stated in the if condition, it prints error as stated also in the else condition. The problem is when I enter a 3-digit,4 , 5 digit number and so on, I get an error that is supposed to be Grade is not within the scope as stated in the nested if (else inside the if condition). Also the same thing happens when I enter 0. But the good thing is, when I enter a negative number or integer, it properly works well. Can I ask what is the problem with my condition? (Tnx in advance)

#include <iostream>
using namespace std;
int main ()
double grade;
cout<<"Enter Grade or GWA: ";
if (grade!=grade>='a'&&grade<='z'||grade!=grade>='A'&&grade<='Z')
/*The first If condition states that the user will get an error if
input a non-numerical integer or number.*/
if (grade<=5&&grade>=1)
if (grade>2.5)


else if (grade<75&&grade>=0)
cout << "Failed" << endl;

else if (grade>=75&&grade<=100)
cout << "Passed" << endl;

cout<<"Grade is not within the scope!"<<endl;


return 0;

Answer Source

Okay, Here are some tips:


this just does not work like that. grade<='z' is good (syntactically correct). However, grade!=grade>='a' IS NOT. For example;

int a =10;
int b=10;
int c=10;

if (a==b==c){
    cout<< "GOOD";
    cout<< "NOT GOOD";

As you see you will get NOT GOOD all the time. unless you write (a==b && b==c)

Instead of using a double, use a float.

Also, you are asking for float values right? where the range is from 0 to 100 right? if so just check if (grade >= 0 && grade <= 100)

Then, if the user inputs 1 how can you say whether it is in the scale from 0-100 or 0-5. You see you have to keep that in mind as well otherwise it will just go into the first if statement it finds. Oh yeah I got a 4/100 however because I am being checked with the 0 to 5 scale I pass :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download