0

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...

4
Contributors
9
Replies
10
Views
8 Years
Discussion Span
Last Post by Spring_1000
0

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?

0

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.

0

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...
      */

   }
}
0

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

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

0

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?

0

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.

0

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?

0

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.

0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.