1

I have some bug in my while(1) loop implementation. It might be a silly issue but I couldn't realize the issue.

int main()
{
    char choice;
    unsigned short item = 0;

    while(1)
    {
            printf("\nSingly Linked List\n");
            printf("\n1. Add an item to the list");
            printf("\n2. Remove an item from the list");
            printf("\n3. Search for an item in the list");
            printf("\n4. Print the list");
            printf("\n5. Exit\n");
            scanf("%c",&choice);
            switch(choice)
            {
                    case '1':
                            printf("\nYet to be implemented\n");
                            break;
                    case '4':
                            printf("\nYet to be implemented\n");
                            break;
                    case '5':
                            return 0;
                    default:
                            printf("\nInvalid, please do re-enter\n");
            }
    }
    return 0;

}

When I run this code I'll get the output as,

Singly Linked List

  1. Add an item to the list
  2. Remove an item from the list
  3. Search for an item in the list
  4. Print the list
  5. Exit

Now no matter what ever input I enter except 5, after executing the particular case, in the next loop it prints the menu and automatically takes some input and executes the default case. And then it displays the menu again and finally I get the control to enter my input.

1

Yet to be implemented

Singly Linked List

  1. Add an item to the list
  2. Remove an item from the list
  3. Search for an item in the list
  4. Print the list
  5. Exit

Invalid, please do re-enter

Singly Linked List

  1. Add an item to the list
  2. Remove an item from the list
  3. Search for an item in the list
  4. Print the list
  5. Exit
pearleye
  • 29
  • 4
  • While the problem here is the same as in the proposed duplicate, the solutions for the proposed duplicate don't include the simple "Use `" %c"` for the format string" solution to the problem. Consequently, it isn't a good duplicate. There must be better duplicates; it crops up several times a week as a question, I'd guess. – Jonathan Leffler Apr 01 '14 at 22:36
  • get input as a line using gets(), then scan that line using sscanf(" %c", &ch). scanf(" %c ") should also be ok in your case. – wacky6 Apr 02 '14 at 02:12

4 Answers4

3

Add a space before %c, you leave the newline in the input butter and in the next iteration scanf will read and consume it, causing the loop to continue.

scanf(" %c",&choice);
this
  • 5,229
  • 1
  • 22
  • 51
1

Never use scanf for the brings with itself a lot of complication. fegts() is what will save you from all the trouble that you are going throuh now.

attaboy182
  • 2,039
  • 3
  • 22
  • 28
0

You must consume the enter you pressed, too. try:

scanf(" %c", &choice);

instead. Also, you should test if scanf hit an error condition, so change again:

if(scanf(" %c", &choice) != 1)
    return 0;

Alternatively, use pattern "%c\n".

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
0

Here's a good answer to a similar question:

https://stackoverflow.com/a/7898516/2769157

The problem lies in the fact that you have to hit return to send the input, and that is still sitting in the input buffer, as explained here:

http://c-faq.com/stdio/scanfc.html

The answers in this thread work by consuming the return character:

scanf(" %c",&choice);

Here's a quick modification to see the character added:

int main()
{
    char choice;
    unsigned short item = 0;
    char str[15];

    while(1)
    {
            printf("\nSingly Linked List\n");
            printf("\n1. Add an item to the list");
            printf("\n2. Remove an item from the list");
            printf("\n3. Search for an item in the list");
            printf("\n4. Print the list");
            printf("\n5. Exit\n");
            scanf("%c",&choice);
            sprintf(str,"%d",choice);
            printf("%s",str);
Community
  • 1
  • 1
rpf
  • 49
  • 3