jeudi 26 novembre 2020

What's the problem with fgets() input in c within a switch, loop or if statement?

I tried to ask for some string input with the fgets() function, it is working as long as I don't try to construct a loop or if statement around it. I thought I need to solve this with a pointer, but the char array named "city" already acts as one. What's my fault?

#include <stdio.h>
//#include <string.h>

int main(){
int anfangEnde;
char city[20];

/*This is a comment*/
do{
    printf("Tippe 1 fuer fortfahren und 2 fuer beenden:\n");
    scanf("%d", &anfangEnde);
    switch(anfangEnde){
    case 1:
        printf("gebe Stadtnamen ein: \n");
        fgets(city, 20, stdin);
        //Input geht nicht in if Statements. KP wieso.
        printf("%sund es hat hingehauen.\n", city);
        break; 
    case 2:
        printf("Programm beendet\n");
        break; 
    default: 
        printf("Fehlerhafte Eingabe\n");
    }
}while((anfangEnde != 1) && (anfangEnde != 2));
return 0;
}

Aucun commentaire:

Enregistrer un commentaire