1

I know that scanf before fgets its interfering my program but I dont know how to solve it. I tried thing like 'scanf("%d , &op)'(add space), 'scanf("%d\n", &op)' and getchar() but it doesnt working anyway. When I used getchar() I could write the sentence later, but it just reads the first character. Does anyone know how can I solve it?

'''

int main(){

int op=0;
    printf("MENU\n");
    printf("1. Code\n2. Decode\n\n");
    scanf("%d", &op);
    getchar()

    switch(op){

        case 1:{
            char line[TAM];
            int i,tam=0;
            printf("Write somethingg: ");
            fgets(line,TAM, stdin);
            for (i=0;line[i];i++){
                tam++;
            }if (tam>TAM){
                printf ("ERROR");
                main();
            }else{
                process(line);
            }
            break;
        }

'''

Ognum
  • 19
  • 6
  • 1
    The best solution is to not use `scanf` at all. See the linked q&a for what to do instead. – zwol Jan 11 '20 at 19:27
  • Use `fgets` and then `sscanf` to input the number - don't mix methods. – Weather Vane Jan 11 '20 at 19:30
  • For example, you can use `fgets` with a buffer of size 100 and then you can use `strtol` to convert that into an integer. With `fgets` you have more possibilities of error checking. – S.S. Anne Jan 11 '20 at 19:30

0 Answers0