0

I'm using a while, switch, case statement for my menu and when it runs it keeps saying enter choice, I know while(1) creates an infinite loop but is there a way to avoid this?

while(1)
{
    printf("\nEnter Choice \n");
      scanf("%d",&i);
      switch(i)
      {
            case 1:
            {
             printf("Enter value to add to beginning: ");
             scanf("%c",&value);
             begin(value);
             display();
             break;
             }
             case 2:
             {
             printf("Enter value to add last: ");
             scanf("%c",&value);
             end(value);
             display();
             break;
             }
             case 3:
             {
             printf("Value to enter before\n");
             scanf("%c",&loc);

             printf("Enter value to add before\n");
             scanf("%c",&value);

             before(value,loc);
             display();
             break;
             }
             case 4 :
             {
             display();
             break;
             }

      }
}

Any help would be appreciated.

Hamas4
  • 51
  • 1
  • 1
  • 9

4 Answers4

2

While(1) is ok. But you have to have some conditions to finish the loop. Like :

while(1){
.........
if(i == 0)
  break;
............
}

Add a space at the beginning of every "%d" and "%c",because scanf always leaves a newline characters in buffer:

"%d"->" %d" 
"%c"->" %c"
diwatu
  • 5,641
  • 5
  • 38
  • 61
  • I have those conditions, for case 1-5, but the reason I'm trying to use a loop is because it's for a doubly linked list, which needs to keep adding to the list. – Hamas4 May 10 '13 at 15:45
  • Look at full code above for the menu. What I'm trying to say is I understand that while(1) creates an infinite loop, that's not a problem, I can stop it at while(i!=4), but it keeps saying Enter Choice. For example, if I run it and press 1, I get Enter choice which is followed by Enter at beginning, but it doesn't allow me to enter anything there, but if I press 1 and the next loop I add 'a', it works. – Hamas4 May 10 '13 at 16:15
  • Sorry, still trying to understand what is your real problem.But probably because of the 'i''s data type is integer, after you type 1, you have to type a 'enter' or non-numeric char to let it know you already typed a integer. But for the 'value', it is different, its datatype is char, no matter what you type, as long as you type a char or number, it will immindiatly accept it, you don't need to wait. – diwatu May 10 '13 at 16:31
  • As they say a picture tells a thousand words, it'll be best explained if you look at this pic. http://s7.postimg.org/pwuxcixkr/null.png . Even if I enter the choice, it still asks to enter choice, but once I choose one and put a char, it adds it to the linked list, what I'm trying to get at is it's not letting me add it after value to add to beginning, so it's not letting me choose an option then enter value in two different steps – Hamas4 May 10 '13 at 16:39
  • try this: change scanf("%d",&i); To: scanf("%d\n",&i); – diwatu May 10 '13 at 16:44
  • or change i's data type to char, but you have to change the case like case (int)'1': – diwatu May 10 '13 at 16:47
  • scanf("%d\r",&i); should be better – diwatu May 10 '13 at 16:49
  • Once I do either \r or \n it creates a blank line after I press an int, which then I have to press a char for it to bring up the enter at beginning line. also the case (int) did not work well – Hamas4 May 10 '13 at 16:53
  • I am going to try your code on my machine, late i will let you know – diwatu May 10 '13 at 17:00
  • 1
    This problem is from c, not from you. solution is change all of your "%d" ->" %d" and "%c"->" %c". Add a space at the beginning of every "%d" and "%c" – diwatu May 10 '13 at 17:17
2

Alternative solution,

int i = !SOME_VALUE;

while(i != SOME_VALUE)
{
   printf("\n\nEnter Choice ");
   scanf("%d",&i);

    switch(i)
    {
        case SOME_VALUE: break;
         .
         .
         .
       // the rest of the switch cases
    }
}

SOME_VALUE is any integer number notify to stop loop.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Navnath Godse
  • 2,233
  • 2
  • 23
  • 32
1

Alternatively, you may want to put a condition in the loop that relates to the input, e.g.

do
{
    printf("\n\nEnter Choice ");
    scanf("%d",&i);
    // the rest of the switch is after this
} while (i != SOME_VALUE);

Note the use of the do loop, which tests the condition at the end, after a value has been read into i.

1''
  • 26,823
  • 32
  • 143
  • 200
1

I would probably write a function that can be called in the loop:

while ((i = prompt_for("Enter choice")) != EOF)
{
     switch (i)
     {
     case ...
     }
}

And the prompt_for() function might be:

int prompt_for(const char *prompt)
{
    int choice;
    printf("%s: ", prompt);
    if (scanf("%d", &choice) != 1)
        return EOF;
    // Other validation?  Non-negative?  Is zero allowed?  Retries?
    return choice;
}

You can also find relevant discussion at:

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278