0

So I'm creating a program to read in data and stuck on this part. So if someone choses "1" from the menu section, it should prompt the user to enter "Name of band of Singer", but when I actually run the code, it prints the prompt but also goes back to the menu screen.

I have only copied and pasted the part where I need help in. The stuff inside "fgets" is for a struct.

int main() {
    while (1) {
        printf("The following options are available \n"
            "Read in data [1] \n"
            "Print out catalogue to screen [2] \n"
            "Save data to file [3] \n"
            "Exit Program [4] \n"
            "Enter your choice now ");
        scanf_s("%d", &menu_num);
        if (menu_num == 1) {
            printf("Name of band of Singer: ");
            fgets(hold[0].singer, 20, stdin); }
  • 1
    `scanf` leaves a new line character in stdin. Add a `getchar()` call after scanf and that should fix it. – Lundin Oct 28 '21 at 10:41
  • 1
    When you type `1` [ENTER], the `scanf_s()` reads the `1` and the `fgets()` reads the [ENTER]. – Scheff's Cat Oct 28 '21 at 10:41
  • And btw. the `if` is not in charge. You yourself stated that the `printf()` is performed. Hence, the branch is entered (for sure). – Scheff's Cat Oct 28 '21 at 10:43

0 Answers0