Click here to Skip to main content
15,860,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this assignment to build a program in C using switch statement, which reads a value and if the value is grater than 50, it displays "You passed". If it's greater than 0 and less than 50, it displays "You didn't pass". And if it's less than 0, it displays "Not valid".
Every article I've read about this topic, the answers were use If/Else. I know how to build it using if/else, the problem is I have to use switch case statement.


What I have tried:

C#
#include <stdio.h>

int main() {
    int x=0;
    printf("Enter your score:\n");
    scanf("%i",&x);
    switch(x)
    {
        case (x>=50):
            printf("You passed");
            break;
        case 0:
        case (x>0 && x<50):
            printf("You didn't pass");
            break;
        case (x<0):
            printf("Not valid");
            break;
    }
    getch();
    return 0;
}
Posted
Updated 9-Nov-16 9:56am
Comments
CPallini 9-Nov-16 8:13am    
As stated, the problem is very tricky. Are you sure you reported the exact requirements? What kind of libraries and functions are you allowed to use?
Richard MacCutchan 9-Nov-16 8:35am    
Case statements can only take constant values so you need to translate the numbers first, into something like -1, 0, +1. But since that requires if statements, it rather defeats the object of the exercise. I would be interested to see your professor's solution.
Richard MacCutchan 9-Nov-16 16:29pm    
Solution 6 by Albert Holguin is the answer.

Always use the right tools for the job. The right tool here is if-else. Period.
 
Share this answer
 
Similar to John's solution, but using your requirement of a switch statement. It breaks the scores into 3 possible values, and performs the switch based on those values.
C++
int x=0;
printf("Enter your score:\n");
scanf("%i",&x);
switch (x >= 0 ? (x >= 50 ? 1 : 0) : -1) {
    case 0:
        printf("You didn't pass");
        break;
    case 1:
        printf("You passed");
        break;
    default:
        printf("Not Valid");
        break;
}
 
Share this answer
 
Comments
Klajdi Murati 9-Nov-16 8:55am    
Thank you very much. It was the actual solution I was looking for. :D
#realJSOP 9-Nov-16 8:57am    
My solution is a lot like the Kobiashi-Maru hack - it had the benefit of never having been tried before. :)
Albert Holguin 9-Nov-16 10:44am    
Although this meets the requirements of the OP, it sure is ugly.... NEVER use this in actual production code.
Call me biased but I like my solution better... :)
C
#include <stdio.h>
 
int main() {
    unsigned x=0;
    printf("Enter your score:\n");
    scanf("%u",&x);

    /*let's use math instead*/
    int val = x/50;
    switch(val)
    {
        case 0:
            printf("You didn't pass.");
            break;
        case 1:
        case 2:
            printf("You passed.");
            break;
        default:
            printf("Not valid");
            break;
    }
    getch();
    return 0;
}
 
Share this answer
 
Comments
Richard MacCutchan 9-Nov-16 11:46am    
I believe that is actually the right answer. Assuming (which is most likely) that score is in the range [1..100].
Albert Holguin 9-Nov-16 11:54am    
Well, this does account for negatives as well (you'll get "not valid" for negatives since the result val will be huge). It does also allow values up to 149 though, which I though would be proper given that "extra credit" can usually boost a grade slightly over a 100.
Midi_Mick 9-Nov-16 12:21pm    
I actually thought of that, but that then means anything over 100 is valid up to 149, and invalid if greater than that, which didn't sit well with me, so I threw it out. It is also incorrect in that scores between -1 and -49 also return 0, not -1, so appear as a fail rather than invalid.
Albert Holguin 9-Nov-16 13:15pm    
-1 to -49 do not return 0 if you use an unsigned int, and yes, 100-149 are also valid but I thought that was acceptable given the OP's requirements.
The switch statement is not done for this usage.
The natural usage of switch statement is to match specific values.
Example: you have a menu with a letter for each option.
C++
switch (MenuOption) {
    case 'a': // first option
        // do something
        break;
    case 'b': // second option
        // do something
        break;
    default:
        // option unknown
]

For your assignment, the if statement is more fit:
C++
if (x>=50) {
    printf("You passed");
}
else if (x>0) {
    printf("You didn't pass");
}
else {
    printf("Not valid");
}


Here is links to references books on C and C++ by the authors of the languages. Note than C is the ancestor of C++, so knowing C is always useful with C++.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]

C++ Programing Language[^]
 
Share this answer
 
It is not recommended to use a case statement without some preprocessing that would probably involve using the dreaded if/else if... construct, but if you're just trying to be different, you could try this:

C#
int x = 0;
string status = "";
status = (status == "" && x >= 50)          ? "You passed" : status;
status = (status == "" && x >= 0 && x < 50) ? "You failed" : status;
status = (status == "" && x < 0)            ? "Not valid"  : status;


The code above was written in C# but it should translate to C quite easily. The code above was tested with "x" set to -1, 0, 30, and 51
 
Share this answer
 
v4
This is the best I could come up with:
C++
switch (score > 50) // switch statement is not a recommended to use expressions
{
case TRUE:
    printf("You passed\n");
    break;
case FALSE:
    if (score > 0)
        printf("You failed\n");
    else
        printf("Bad score\n");
    break;
}
 
Share this answer
 
(Please note: I agree with others on using the proper construct, namely an if chain).
C
#include <stdio.h>

int main() 
{
  int x=0;
  printf("Enter your score:\n");
  scanf("%i",&x);

  switch ( ((unsigned int)x) >> (sizeof(int) * 8 -1))
  {
  case 0:
    switch(x/50)
    {
    case 0:
      printf("you din't passed.\n");
      break;
    default:
      printf("well done.\n");
      break;
    }
    break;
  default:
    printf("invalid score.\n");
    break;
  }
  getchar();
  return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900