0

When the user inputs Yes,Only Maths,Only Science, the compiler executes Default. I've tried with the same code but with Char replaced with Int it works as intended.. but with char it dosen't..

#include<stdio.h>

int main(){
    
    char a;

    printf("Have you passed in both maths and science?[Yes,Only Maths,Only Science]");
    scanf(" %c", &a);

    switch (a){
    case 'Yes':
        printf("That's amezing!");
        break;

    case 'Only Maths':
        printf("That's good!");
        break;

    case 'Only Science':
        printf("That's good!");
        break;

    case 'none':
        printf("oof work harder next time!, i hope you will rock on that exam!");
        break;

    default:
        printf("bozo do the typing right!");
        break;
    }

    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 5
    `char` means a single character – M.M Jul 25 '22 at 04:52
  • 2
    You can't have multiple characters stored in a single `char` variable. – Adrian Mole Jul 25 '22 at 04:52
  • 3
    [Multi-character character constants](http://port70.net/~nsz/c/c11/n1570.html#6.4.4.4) such as `'none'` are 'allowed' but have an implementation-defined value. That value is an `int` value (it's unlikely to be a `char` value, but the result is implementation-defined), and it almost certainly won't equal any single-byte `char`. You should end the `printf()` format strings with a newline, too. – Jonathan Leffler Jul 25 '22 at 04:56
  • 1
    always turn on all warnings and read them. The compiler will give you the issue right away – phuclv Jul 25 '22 at 07:36

1 Answers1

-1

A char by itself cannot store a string like "Yes". It will only store the first 'Y' and leave the rest. This is why none of the switch cases will match. Consider using string or char array to store an array of chars which can be compared. However, then the problem becomes that you cannot use switch with strings in C so you must use if, else.

#include <stdio.h>

int main(){
    char a[15]; //15 should be a reasonable size

    printf("Have you passed in both maths and science?[Yes,Only Maths,Only Science]");
    fgets(a, 15, stdin);
    
    if (strcmp(a, "Yes") == 0) printf("That's amezing!");

    else if (strcmp(a, "Only Maths") == 0) printf("That's good!");

    else if (strcmp(a, "Only Science") == 0) printf("That's good!");

    else if (strcmp(a, "none") == 0) printf("oof work harder next time!, i hope you will rock on that exam!");

    else printf("bozo do the typing right!");
    
    return 0;
}

It is also recommended to use fgets instead of scanf to read strings.

  • 3
    _"It is also recommended to use gets instead of scanf to read strings"_ - No: [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used). `fgets` is on the other hand fine – Ted Lyngmo Jul 25 '22 at 05:27
  • `gets` has been from the standard more than a decade ago – phuclv Jul 25 '22 at 07:37
  • `It will only store the first 'Y' and leave the rest.` nope, the value of a multi-character constant is implementation defined. If it's the first character then the above snippet will get a compilation error because there are 2 cases that are `'O'` – phuclv Jul 25 '22 at 11:49