-1

I have a problem with a switch statement. I tried many of different ways to handle it and I also googled it and I can't find a solution for probably a basic mistake.

Errors:

switch(dnevi) -> switch quantity not an integer
case "Pet" -> case label does not reduce to an integer constant
case "Sob" -> case label does not reduce to an integer constant

Code:

char dnevi[5];
printf("Vnesi dan:\n  P–ponedeljek, T–torek, S–sreda,C–cetrtek, Pet–petek, Sob–sobota, in N–nedelja" );
scanf("%d", dnevi);
switch(dnevi)
{
    case 'P': 
        printf("To je ponedeljek");
        break;
    case 'T': 
        printf("To je torek");
        break;
    case 'S': 
        printf("To je sreda");
        break;
    case 'C': 
        printf("To je cetrtek");
        break;
    case 'Pet': 
        printf("To je petek");
        break;
    case 'Sob': 
        printf("To je sobota");
        break;
    case 'N': 
        printf("To je nedelja");
        break;
    default: 
        printf("Vnos je bil napacen!");

}
Fred Larson
  • 60,987
  • 18
  • 112
  • 174
Matic1295
  • 13
  • 2
  • 4
    You can't use strings as `case` labels. Also strings are usually written insif `""` and not `''`. But you have other problems too. You read `dnevi` as an integer, but storing in `char` array and comparing with characters. – Eugene Sh. Apr 18 '19 at 20:01
  • Possible duplicate of [C/C++: switch for non-integers](https://stackoverflow.com/questions/4165131/c-c-switch-for-non-integers) – Fred Larson Apr 18 '19 at 20:02
  • 'Pet' and 'Sob' should be something like 'p' and 's'. And you must scanf("%c", ch) character. – purec Apr 18 '19 at 20:02
  • C string literals are *always* written with `""`, not `''`. A sequence of characters enclosed in `''` is a character constant, with type `int`, not in any way a string. That's why you can use character constants as case labels, but the resulting behavior is unlikely to be what the OP wants. – John Bollinger Apr 18 '19 at 20:12

1 Answers1

2

It seems in addition to the single character matches, you also want to match against a string. So, you are trying to read a string as input. Simple error handling is illustrated, you may want something fancier.

if (scanf("%4s", dnevi) != 1) {
    strcpy(dnevi, "?"); /* simple error handling */
}
scanf("%*[^\n]");       /* get the rest of the input */

However, as the error states, you need to provide an integer to switch. You can do so on the first character.

switch(dnevi[0])

However, you now have to deal with the fact you have two P choices and two S choices. Handle that with special cases in those particular cases. String literals are surrounded by ".

case 'P': 
    if (dnevi[1] == '\0')
        printf("To je ponedeljek");
    else if (strcmp(dnevi, "Pet") == 0)
        printf("To je petek");
    else
        printf("Vnos je bil napacen!");
    break;
...
case 'S': 
    if (dnevi[1] == '\0')
        printf("To je sreda");
    else if (strcmp(dnevi, "Sob") == 0)
        printf("To je sobota");
    else
        printf("Vnos je bil napacen!");
    break;
...
default: 
    printf("Vnos je bil napacen!");
jxh
  • 69,070
  • 8
  • 110
  • 193