0

I'm stuck at a pretty awkward thing. Here's the code:

#include <stdio.h>
#include <stdlib.h>

void processCommand(){
    char *c = malloc(sizeof(char) * 128);

    scanf("%s", c);

    switch(*c){
    case '!': 
        printf("Action");
        break;
    case '?': 
        printf("Question");
        break;
    default: 
        printf("Unknown Action");
        break;
    }
}

So what I want is, if the first char of my input is something different than ! or ?, it should just say it's an unknown action. So it works for typing other characters, but everytime i type a whitespace / tab / nothing, it doesn't do anything ?

Jonas
  • 6,915
  • 8
  • 35
  • 53
TanguyB
  • 1,954
  • 3
  • 21
  • 40

1 Answers1

1

As other users pointed out: scanf ignores whitespaces, fgets doesn't.

void processCommand(){
    char *c = malloc(sizeof(char) * 128);
    fgets (c, sizeof(char) * 128, stdin);

    switch(*c){
        case '!': 
            printf("Action");
            break;
        case '?': 
            printf("Question");
            break;
        default: 
            printf("Unknown Action");
            break;
        }
}
7hibault
  • 2,371
  • 3
  • 23
  • 33