5 Replies - 618 Views - Last Post: 04 January 2013 - 06:04 AM Rate Topic: -----

#1 Sahle123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 26-January 11

Repeating switch statement in while loop problem.

Posted 04 January 2013 - 04:23 AM

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

Is This A Good Question/Topic? 0
  • +

Replies To: Repeating switch statement in while loop problem.

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2133
  • View blog
  • Posts: 3,267
  • Joined: 21-June 11

Re: Repeating switch statement in while loop problem.

Posted 04 January 2013 - 04:56 AM

Quote

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.


You can test that theory by inserting a printf that prints the offending input when complaining about invalid input.

Spoiler

Was This Post Helpful? 0
  • +
  • -

#3 sunilchintu2468  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 37
  • Joined: 29-May 11

Re: Repeating switch statement in while loop problem.

Posted 04 January 2013 - 05:13 AM

View PostSahle123, on 04 January 2013 - 04:23 AM, said:

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


Give a space in the scanf statement and run your code :
scanf("%c ", &ctrl_pick);

Hopefully it works
Was This Post Helpful? 1
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2133
  • View blog
  • Posts: 3,267
  • Joined: 21-June 11

Re: Repeating switch statement in while loop problem.

Posted 04 January 2013 - 05:42 AM

View Postsunilchintu2468, on 04 January 2013 - 01:13 PM, said:

Give a space in the scanf statement and run your code :
scanf("%c ", &ctrl_pick);


Don't discard space at the end, discard it at the beginning. If you discard space at the end, scanf won't return until it sees the next non-space character (so it knows it has discarded all whitespace). This will make it look like your program hangs if the user enters only one input at a time.
Was This Post Helpful? 1
  • +
  • -

#5 Sahle123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 26-January 11

Re: Repeating switch statement in while loop problem.

Posted 04 January 2013 - 05:53 AM

Thank you sepp2k sunilchintu2468 for the very quick replies.

So from what I am understanding from both of you is that I need to put a space, like so: ("%c ") for the extra \n I put in my printf in the default case.

If that is so, what if I were to put 3 new line characters. Do I need to use 2 spaces? Or is there a better way to break multiple lines?

View PostSahle123, on 04 January 2013 - 05:49 AM, said:

Thank you sepp2k sunilchintu2468 for the very quick replies.

So from what I am understanding from both of you is that I need to put a space, like so: ("%c ") for the extra \n I put in my printf in the default case.

If that is so, what if I were to put 3 new line characters. Do I need to use 2 spaces? Or is there a better way to break multiple lines?


Sorry, I meant (" %c"), not ("%c").
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2133
  • View blog
  • Posts: 3,267
  • Joined: 21-June 11

Re: Repeating switch statement in while loop problem.

Posted 04 January 2013 - 06:04 AM

View PostSahle123, on 04 January 2013 - 01:53 PM, said:

So from what I am understanding from both of you is that I need to put a space, like so: ("%c ") for the extra \n I put in my printf in the default case.


No, this has nothing to do with your printf. If the user enters a letter, he will press enter afterwards, right? This press of the enter key will leave a \n in the input stream. And that's what gets discarded by the space in the scanf format.

Quote

If that is so, what if I were to put 3 new line characters. Do I need to use 2 spaces? Or is there a better way to break multiple lines?


One whitespace in the format string will discard an arbitrary amount of whitespace in the input stream. So if there are 3 newlines in the stream (because the user pressed enter three times, say), they will all be discarded - no additional spaces needed.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1