Program Issue, if and else if statements

This is a discussion on Program Issue, if and else if statements within the C++ Programming forums, part of the General Programming Boards category; For some reason everytime i run my program the answer is always B. Im not sure why this is, please ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    28

    Program Issue, if and else if statements

    For some reason everytime i run my program the answer is always B. Im not sure why this is, please help!

    Code:
        //Figure out letter grade
        if (the_student.percentTotal>= 90)
        {
            the_student.letterGrade = 'A';                           
        }
        else if ((the_student.percentTotal>= 80) || (the_student.percentTotal < 90))
        {
             the_student.letterGrade = 'B';
        }
        else if ((the_student.percentTotal>= 70) || (the_student.percentTotal < 80))
        {
             the_student.letterGrade = 'C';
        }
        else if ((the_student.percentTotal>= 60) || (the_student.percentTotal < 70))
        {
             the_student.letterGrade = 'D';
        }
        else if (the_student.percentTotal<60)
        {
             the_student.letterGrade = 'F';
        }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Can you come up with a number, call it percentTotal, where the if before the "B" clause is false? (Hint: No.) (Double hint: || does not mean "and".)

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    In addition, if you have established that n >= 90 is not true (the first condition is false), there is no point checking in the else block if n is smaller than 90. You wouldn't have got to the else block if it wasn't so.
    Similarly for all comparisons down the chain.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21