1

This is a bit of a long post so bear with me. The gist of what I am trying to do is get some user input at the beginning of a loop, do some things depending on that input and possibly repeat. Simple enough right? Here is what I have.

void displayMain(Album albums[], int num_albums){

    int break_condition = 1;
    int user_choice; 

    //keep looping until we decide to quit
    while (break_condition){

            user_choice = displayMenu();

            switch(user_choice){

                    case 0 :
                            return; //does this need to be return 0 or can it just be return (or is it the same thing?)  
                    case 1 :
                            displayAlbums(albums, num_albums);
                            break;
                    case 2 :
                            printf("display tracks\n");
                            break;

                    default : 
                            printf("Invalid choice!\n\n");
                            break;

            }

    }

    return;

} 

The function displayMenu() basically grabs some user input and returns an int.

int displayMenu(void){

    printf("Display Menu\n1. Display all albums\n2. Display the tracks in an album\n0. Return to the mainmenu\nPlease enter the choice:\n");

    //grab and return the choice
    int choice;
    scanf("%d", &choice);
    return choice;

}

So when I finally run all of this code, if I enter a "bad" choice, ie: one that takes the default path in the switch statement, the code enters an infinite loop. Repeating the contents

Display Menu
1. Display all albums
2. Display the tracks in an album
0. Return to the main menu
Please enter the choice:
Invalid choice!

I am pretty much a newb at C, so I believe I know why this is. user_choice is not ever being reset, even though the function displayMenu is clearly being called (because of the infinite loop output)

Why isn't displayMenu asking the user for input anymore after one instance of "bad" input is entered? Is it because it isn't an integer? When I tried debugging and printing out user_choice, it was some large number, so likely a register or something like that.

Please help, as I said, I suck at C, so an experienced C programmer could probably lend a hand :)

Zack
  • 13,454
  • 24
  • 75
  • 113
  • I can't reproduce the error. This code seems to work correctly. Can you post a [minimal complete example](http://sscce.org)? – Beta Nov 25 '13 at 00:32
  • 1
    If you enter a non-numeric character rather than an out of range number, then you get stuck in a loop because the `scanf("%d", &choice)` fails, but you pay no attention to its return status telling you that it failed (or not). And if it does fail because of the non-numeric character, you need to gobble the rest of the line to reset the input operations. – Jonathan Leffler Feb 08 '14 at 11:39

1 Answers1

0

Running your code in Visual Studio 2012 (in the Cygwin Terminal executing it as a .c extension that I also tested , it works just fine) the only error that I got was that I should use the safe version of scanf( scanf_s ) instead.

If you don't want doing that the whole time when you already have declared more than one scanf in your code and the specific error pops up, you could add to your preprocessor definitions the _CRT_NO_SECURE_WARNINGS sentence . Still, I don't know the exact implications of using the scanf_s over scanf and the opposite and how it affects the input of a program.

[ Right Click on your project--> Properties--> C/C++ -->Preprocessor Definitions and you add the sentence . ]

All the above I guess only apply if your IDE is Visual Studio and that is your case.

As for the return statement in case 0. This will force the loop to terminate and thus, it will exit the function . You can use the break statement to end processing of a particular case within the switch statement and to branch to the end of the switch instead, so the loop can be continued and display your menu again.

Returning a 0 value type will not match your function type ( as long as you have it declared as void ).

Menios
  • 145
  • 1
  • 9