Broken menu loop help! - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Broken menu loop help!

psychofox19psychofox19 Posts: 41Member
Hi I have a do...while loop in C and I want it to stop only after a user has entered the character 'q' or 'Q' during the "Enter Command:- " section of the menu and send it through the switch case.

It will break the loop when the integer quit becomes a number other than 0 (a simple substitute for a boolean in C) which only happens during the 'q' or 'Q' case on the menu.

All other functions running off main work and do not bring up any errors as I have thoroughly tested them even with the broken menu.

However I think because of the scanf("%ch", &command); line the menu does extra unncessary loops until it takes in the character I originally entered because it doesn't ignore extraneuous characters when it should. Is there a character ignore method that I could use like there is in C++ to use on this menu?

[code]
int main()
{
int quit = 0;
char command;
char character;
node *head = (node*)malloc(sizeof(node));
initList(head);
node *cur = head;

do
{
printf("

**Word Editor Menu**
");
printf("(R) Move Cursor Right
");
printf("(L) Move Cursor Left
");
printf("(I) Insert character
");
printf("(D) Delete To Right of Cursor
");
printf("(Q) Quit
");
displayWord(head);
printf("
Enter command:- ");
scanf("%ch", &command);

switch(command){
case 'R':
case 'r':
moveRight(head);
break;
case 'L':
case 'l':
moveLeft(head);
break;
case 'I':
case 'i':
printf("
Enter character:- ");
scanf("%ch", &character);
insertChar(head, character);
break;
case 'D':
case 'd':
deleteChar(head);
break;
case 'Q':
case 'q':
quit = 1;
break;
default:
printf("
Not a valid command
");
break;
}
}while(!quit);
return 0;
}
[/code]

Comments

  • LundinLundin Posts: 3,711Member
    For each character entered, the user also presses the enter key. This leaves a "line feed" character ('
    ') in the input buffer. You need to discard this line feed after each input:

    scanf("%ch", &command);
    getchar();
  • psychofox19psychofox19 Posts: 41Member
    Thank you, but command = getchar() works better.
Sign In or Register to comment.