-6

I'm trying to do switch then if else in it. I don't know where I missed but this doesn't work.

Here's my code:

include<stdio.h>
include<conio.h>

int main()
{
  clrscr();

  int grade;

  printf("Input Grade");
  scanf("%d",&grade);

  switch(grade<101)
  {
    case 1:
      if (grade>=95)
        printf("A+");
      else
        printf("Invalid");
    break;
    case 2:
      if (grade>=85)
        printf("A");
      else
        printf("Invalid");
      break;
    default:
      printf("Invalid");
  }
getch();
}

So I don't know what's the matter. Only the first case work, when i enter a lower number than 95 'A' isn't the result. Please help me out.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
majojoi
  • 1
  • 1
  • 1
  • 2
  • 3
    This is not how switch statements work. `grade<101` is a boolean value converted to an `int`, so when it is ok, it will be a non-`0` value, probably `1`. – Gábor Bakos Sep 03 '16 at 11:01
  • @sudomakeinstall2 What on earth has this to do with c++? – πάντα ῥεῖ Sep 03 '16 at 11:21
  • `switch(grade<101)` doesn't do what you think it does. You're switching over a `bool` value. – πάντα ῥεῖ Sep 03 '16 at 11:23
  • You appear to be writing code for a 1980s primordial dialect of "C++", specifically for Microsoft DOS (an operating system that doesn't actually exist any more). Why don't you write code for standard C++, introduced in 1998 (that's almost two decades ago), instead? Then you'll be able to use it in a job and make money with a career. Learn C++ from [a good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), not whatever antiquated resource you have at the moment. – Lightness Races in Orbit Sep 03 '16 at 11:26
  • Just noticed you tagged as C and C++. Which is it? Remove whichever tag is wrong. (My advice above applies either way.) – Lightness Races in Orbit Sep 03 '16 at 11:29
  • @GáborBakos, why probably? it will be `1` – David Ranieri Sep 03 '16 at 11:34
  • @AlterMann as I remember it was (?) not specified how bools values are converted to ints, it might be optimized to return the difference between the numbers (for example a condition `grade!=101` and `grade` value `99` it could be `2` or `-2` too as I remember, both are not `0`), probably not in this case, though I can be wrong. – Gábor Bakos Sep 03 '16 at 11:44
  • @GáborBakos, maybe you are right, but I have never seen other result than `0` or `1` using comparison and logical operators. – David Ranieri Sep 03 '16 at 11:48
  • @gabor: your memory is incorrect. In C, the value of a comparison operator is an `int`, not a `bool`, and the value is always 1 or 0. See section 6.5.8 paragraph 6 and 6.5.9 paragraph 3. – rici Sep 04 '16 at 00:21
  • @rici you mean [here](http://eli-project.sourceforge.net/c_html/c.html)? ;) Thanks for the detailed pointer, I have found that you are right in [this newer version](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf). Today I learned. :) – Gábor Bakos Sep 04 '16 at 05:48
  • @Gabor: The Eli project is not an official C document. The section numbers are from C99, but you can find the same statement on page 42 of Kernighan&Ritchie, 2nd edition (1988): "By definition, the numeric value of a relational or logical expression is 1 if the relation is true, and 0 if the relation is false." That was written just when C was being standardized; prior to that, the only possible definitive statement could have come from K&R. – rici Sep 04 '16 at 05:59
  • @rici Thanks. (I have to note though that K&R 2nd edition does not mention the equality operators in that sentence.) You are right. – Gábor Bakos Sep 04 '16 at 06:27
  • @Gabor: On the previous page, the relational, equality and logical operators are all grouped together in a single section, called "Relational and Logical Operators"; I don't think there is any doubt that the equality operators are intended to be included in the category of "relational or logical expression". But the subsequent standardization makes it completely clear. (Of course, that doesn't mean that every compiler was or is compliant. But if you assume your compiler can break the standard arbitrarily,, then portable code becomes impossible.) – rici Sep 04 '16 at 07:15

2 Answers2

2

If you have to use switch you should bring the if else statements outside of your switch in order to switch over a proper integer value. Something like this:

int state = 0;
if ( grade < 101 &&‌ grade >= 95 )
  state = 1;
else if ( grade < 101 && grade >= 85 )
  state = 2;

switch state {
   case 1:
      printf("A+");
   break;
   case 2:
      printf("A");
   break;
   default:
      printf("invalid");
   break;
}
Saeid
  • 4,147
  • 7
  • 27
  • 43
0

You can express this with switch statements in the following way (though you probably do not want to):

switch(grade)
{
  case 100:
  case  99:
  case  98:
  case  97:
  case  96:
  case  95:
      printf("A+");
  break;
  case  94:
  case  93:
  case  92:
  case  91:
  case  90:
  case  89:
  case  88:
  case  87:
  case  86:
  case  85:
      printf("A");
  break;
  default:
      printf("Invalid");
      //or an if statement if more options are required.
  break;
}
Gábor Bakos
  • 8,982
  • 52
  • 35
  • 52