0

if I select case 1 nothing happens, I do not understand what is the problem, case 1 is ignored and it jumps to the next iteration.

this is the result I get by selecting case 1:

Enter -1 to quit                                                                                                                                                                                 
Enter your choice: 1                                                                                                                                                                             

Enter -1 to quit                                                                                                                                                                                 
Enter your choice:   

it should ask me for a user input and then print it

#include<stdio.h>
int main()
{
    int I;
    char* str[100];
    do
    {
        puts("Enter -1 to quit");
        printf("Enter your choice: ");
        scanf("%d",&I);
        switch(I)
        {            

            case 1:
            fgets(str,100,stdin);
            printf(str);
            break;
            case 2:
            printf("45\n");
            break;
            case -1:
            puts("Bye");
            break;
            default:
            printf("default\n");
        }
    }while(I != -1);
    return 0;
}
melpomene
  • 84,125
  • 8
  • 85
  • 148
culos
  • 11
  • 1
  • 3
    It does work. `fgets` reads the `\n` leftover by the previous `scanf`. – haccks Oct 01 '19 at 17:49
  • 3
    Danger! `printf(str);` is an exploit waiting to happen, please don't do that. It should be `printf("%s", str);` – Weather Vane Oct 01 '19 at 17:51
  • Put a printf before the fgets and you will see the fgets is actually executed. – Madhusoodan P Oct 01 '19 at 17:55
  • ...or enclose the data in delimiting marks, such as `printf("[%s]", str);` – Weather Vane Oct 01 '19 at 17:56
  • @WeatherVane chage the code to everything you said but still doesnt work, the result is the same – culos Oct 01 '19 at 19:32
  • 1
    That's because my remarks were general comments, not an answer. Did the duplicate questions provide you with a solution? It's a bad idea to mix input methods. I would enter the menu selection with `fgets` as well as the data, and then apply `sscanf` to the input string. – Weather Vane Oct 01 '19 at 19:35

0 Answers0