Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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

Sign In or Register to comment.