I feel really stupid by asking such question

I have the following code

char choice;
 
while( ( choice = MenuChoice() ) != 'QUIT' )
   {
      PrintMenu();
      {
         switch (choice)
         {
        case STRING:
        case 's':
          checkInput(strings);
          break;

        case QUIT:
         break:

        default:
         break:
}
}
}



char MenuChoice(void)
{

char menuchoice;
printf("please enter:");
scanf("%c",&menuchoice;

scanf("%c", &newLine);

return menuchoice;

}

Recommended Answers

All 9 Replies

When I enter 's' as an input it keeps repeating the please enter: enter until I put two 's' like ss.
Why is this happening?

try fgets()

char  menuchoice[2];
printf("please enter:");
fgets(menushoice, sizeof(menuchoice), stdin);
return menuchoice[0];

Thanks its much better. But when I put 4 for QUIT as mentioned in while statement, it will keep asking me for input/

how is 'QUIT' defined ? Your compiler should have generated an error on that line.

QUIT was defined as 3. Do I have to define as an character also like #define QUIT q

if QUIT is defined like this: #define QUIT 3 Then while( ( choice = MenuChoice() ) != 'QUIT' ) will not work. You have to make two changes
1) QUIT should be defined as '3'
2) The while loop should be changed as shown below

#define QUIT   '3'
<snip>
while( ( choice = MenuChoice() ) != QUIT )

>>But when I put 4 for QUIT
You entered the wrong value -- should be 3 not 4

Ok thanks. But what if user is entering character as an input, like

please enter: q

Then redefine QUIT as 'q'

Alright , thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.