Hey, I've been stuck on something all day, when I first looked at it I thought it was going to be relatively easy.

I want to be able to let the user return to a menu whenever they press enter without typing anything else.
I've looked on google, but I havent found anything, it's probably out there somewhere and I'm just putting in the wrong key words.
I've also searched on this forum but with the same results, I did find one thread, but it ended up being for C++...

So, I hope you guys can help me...

We would need a little more information than that to help you. Is it a console application? or a GUI? Windows? Linux? Can you post any code so we can see exactly what you are trying to get at?

It's a console application, on linux, but would be nice if it worked on both...
What I've got is simply a switch statement menu, all the options link to their own function, the function prints out a sentence and then lets the user write something. If the user only presses enter, without writing anything, it'll go back to the menu, otherwise it'll just keep looping the sentence.

So the code for the function would be something like this:

void option1(int n)
{
   char dob[10 + 2];

   for( ; ; )
   {
      printf("Option 1.... %d", n);
      fgets(dob, sizeof (dob), stdin);
      /*
      If statement that breaks out of the for loop when enter is pressed by itself.
      */
      if ( ??? ) // Not sure what to put here, tried many things
      {
         break;
      }

      /*
      More code here...
      */

   }
}

When you press ENTER all by itself, where does it go? What does it look like?

That's what you put in place of ???.

I've tried " ", I've tried "", I've tried \r, I've tried \n, I've tried fgets(dob, sizeof (dob), stdin) == NULL, could someone give me some ideas please?

In C Language, we always use

getch();

When any key pressed in keyboard, the key wasn't displayed.

When you press ENTER, it will do the next statement...

Hope it can help you.

Hey thanks for the reply, would getch work though if the user can either just press enter, and go back to the main menu, or write something?

No, you do NOT want to use getch() . It is a non-standard function and very few compilers have it defined.

The character you need to compare your value to is '\n'. That's as much help as I can give since you won't show us what you did and therefore can't tell what is wrong with how you did it.

The character you need to compare your value to is '\n'. That's as much help as I can give since you won't show us what you did and therefore can't tell what is wrong with how you did it.

I don't know what I did to compare it to \n last time but I obviously did it wrong, because it worked! Awesome, thanks for all your help!

This article has been dead for over six months. Start a new discussion instead.