Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Repeating switch statement in while loop problem.

$
0
0
Hello everyone,

I need help understanding what is wrong with my code below:
The function works perfectly fine, it's just the output I receive baffles me completely.
For some strange reason, my value I input passes twice when it goes to the default case.

bool pick_basic_or_advanced()
{
     bool is_good = true;
     bool done_picking = false;
     char ctrl_pick;

     while(!done_picking)
     {
           printf("A = advanced, B = basic: ");
           scanf("%c", &ctrl_pick);

           switch(ctrl_pick)
           {
                case 'a':
                case 'A':
                     advanced_pick(is_good);
                     done_picking = true;
                     break;
                case 'b':
                case 'B':
                     basic_pick(is_good);
                     done_picking = true;
                     break;
                default:
                     printf("This is an invalid input.\n");
                     break;
           }
     }
     return is_good;
}



The output looks like this:

A = advanced B = basic: f
That is an invalid input.
A = advanced B = basic: That is an invalid input.
A = advanced B = basic:


As you see above, it seems that the value I pass it 'f' gets passed twice in the while loop and prompts me that I had passed it an invalid input twice. Even though I only typed it in and passed it once.

I've been stuck on this for a while and I have no idea how to fix this.

I am not sure if this helps, but I did find people on other forums (couldn't find anything regarding this on DIC) talking about "flushing out" old inputs to solve this, but I could not find any code showing how this is done.

Thanks a bunch :)
Sahle

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>