-3

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: ";
cin>>grade;
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)
            cout<<"Failed"<<endl;

        else
            cout<<"Passed"<<endl;
    }

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

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

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

else
    cout<<"Error!"<<endl;


cin.get();
return 0;
}
Steve
  • 1,553
  • 2
  • 20
  • 29
  • 2
    `grade!=grade>='a'` makes no sense. What is that supposed to do? – melpomene Oct 30 '16 at 07:06
  • @melpomene It means that if grade is not equal to letters 'a' and above (and) 'z' and below the process will continue.The letters are kind of represented by also numbers like a is to 1, b is to 2 and so on that makes the program read it. – cringyfudge420 Oct 30 '16 at 07:31
  • Why not use [isalnum](http://en.cppreference.com/w/cpp/locale/isalnum) – graham.reeds Oct 30 '16 at 07:34
  • What it actually means is that it will check whether `grade` is greater than or equal to `97` (`'a'` is `97` in ASCII), which will return `1` (for true) or `0` (for false), and then compare that to `grade` again to see whether it's not equal. – melpomene Oct 30 '16 at 07:36
  • `grade!=grade>='a'` is a really convoluted way of writing `grade != 0`. – melpomene Oct 30 '16 at 07:38

2 Answers2

1

Okay, Here are some tips:

grade!=grade>='a'&&grade<='z' 

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";
}
else{
    cout<< "NOT GOOD";
}

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

After reading @melpomene comments it made me research more about what datatype should be used, float, double or long double. What I can tell is that it all depends. However the main points almost everybody agrees are:

  • doubles are the common use for Floating-point-types.
  • doubles outperforms floats in modern hardware.
  • doubles have equal or better precision that floats. float(6 decimal places precision) doubles(15 decimal places precision).
  • double storage size 8 bytes vs float storage size 4 bytes.

Therefore, it all depends, in this case I would agree on doubles :) but again it all depends on the conditions and what exactly we are trying to achieve; performance?, precision?, space?.

Also, you are asking for double 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 :)

skypjack
  • 49,335
  • 19
  • 95
  • 187
ArtiomLK
  • 2,120
  • 20
  • 24
  • "*Instead of using a double, use a float.*" - what the hell? Why? – melpomene Oct 30 '16 at 07:38
  • float is 4 bytes while double is 8 bytes. Why would you go with a double when you are storing such small numbers? https://www.tutorialspoint.com/cprogramming/c_data_types.htm – ArtiomLK Oct 30 '16 at 07:50
  • Because all calculations are done with double precision anyway, and 4 bytes is nothing. If you have a loop from 1 to 100, do you make sure your iterator variable is a `char` because it's smaller than `int`? – melpomene Oct 30 '16 at 08:16
-1

Any number bigger then 122 will fail because ascii of z is 122 so grade!=grade>='a'&&grade<='z' will fail.

and also grade!=grade>='A'&&grade<='Z'will fail (Z ascii is 90)

BTW, since grade is a double it will be zero is user enters an illigal number.Just check if(grade !=0)

O_Z
  • 1,515
  • 9
  • 11