tags:

views:

62

answers:

4

Hi,

I'm just starting to study C. I have a program that prints a menu and let users choose what to do step by step. Now I would like to return to the main menu whenever the user enters an empty line, but how can I do that?
I think I can make a function that return the program to the main menu, but when to call that function? I know it's not good to put an if-else whenever I scanf something...

I am used to the OO world, so this is a bit unfamiliar to me, please help :)

A: 

C language is not an OO world, so I'd say stick with if-else. Anyway, when creating text menus you usually end up with switch(user_choice) of if-else.

Do you need a help with scanf()?

pajton
+1  A: 

If you're doing something along the lines of:

printf("0)     do something\n");
printf("1)     do something else\n");
printf("enter) main menu\n");

...then scanf isn't really your friend.

You could do something like this:

char buf[80];  
int choice;
printf(menu_text);
fgets(buf, 80, stdin);
if(strlen(buf))
{
    sscanf(buf, "%d", &choice);
    switch(choice)
    {
    case 0:
        /* etc */
        break;
    case 1:
        /* etc */
        break;
    }
}
else
{
    go_back_to_main_menu();
}
Thanks for the code example, but this is not really what I need. Let's say I'm inside a sub-function (i.e inside the switch already), and I want to go out in case I detect an empty line input.
phunehehe
...then you're probably doing something like switch(choice[0]){case '0': call_func_1; break; case '1': call_func_2; break;} ? If that's the case, you cane get some traction from the fact that the zeroth entry in the array is '\0': switch(choice[0]){ case '\0': go_back_to_main_menu();}
+1  A: 

It depends on if the user is communicating by sending a string or a single character/keypress.

If communicating by string, try starting with:

char  buffer[MAX_BUF_LEN];
char* pBuffer = buffer;
scanf("%s%*c",pBuffer);
if (strlen(pBuffer) == 0)
  goto_main_menu();
else
  process_user_input(pBuffer);

If communicating by character/keystroke, try starting with:

int inkey = getchar();
if (inkey == '\n')
  goto_main_menu();
else
  process_user_input(inkey);

Using an "if/else" after you scanf something is perfectly valid. Anything sent from the user should be checked and validated before it is used anyway.

bta
Hello, I tried this piece of code but it doesn't work. Instead of going to another function it place the cursor in a new line, but still accepting input
phunehehe
Which piece of code doesn't work? If you can't get the first one to work, try the second one and add a line after the call to `getchar` that logs the hex value of the keypress to a file (so you can see what exactly your program is receiving as input).
bta
I tested with both of them, and they don't do anything after I press Enter. The correct behaviour would be to do whatever in goto_main_menu(), right? Control only move on after I enter something (i.e not a blank line)...
phunehehe
@phunehehe: Correct. If the input character is a newline, `goto_main_menu()` should run. If `getchar` isn't returning when you hit enter, then your terminal might be buffering your input (and only delivering it to your application after you enter something). You might want to try setting whatever terminal you are using to unbuffered input and see if your behavior changes. However, the code for doing this is platform-specific. What platform are you currently running?
bta
oh really it's platform specific? that's very bad... I'm working on slackware, and the code will be upload to the server which runs redhat, and it may be accessed through putty :(
phunehehe
Slackware vs. Red Hat shouldn't be a problem. By "platform specific" I meant the way you do it on (for example) Linux won't be the same as on Windows (meaning, it's not something that's built into the C language). If you are working on Linux, a library like `ncurses` can make text interfaces much easier to develop. You can also do this if you play around with `termios`, but it's much less error-prone to let a library handle it for you.
bta
+1  A: 

I'm guessing you're using a switch statement to filter your input, so just make an option for '\n', or the newline character, to output the print the menu.

weird as it seems, this is the only way that works.I had to write myself a function to read a line, then break if the new line is empty.
phunehehe