Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. While Statement Issue.

While Statement Issue.

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestiontestingbeta-testingtutorial
13 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D David Crow

    Mike Certini wrote:

    What is happening is that after I select two menu selections or two loops of the while statement the program skips over the scanf statement. Can someone help me with this issue?

    Likely because something is in the keyboard buffer. Try using fflush() after scanf().

    "One man's wage rise is another man's price increase." - Harold Wilson

    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

    "Man who follows car will be exhausted." - Confucius

    A Offline
    A Offline
    Andrew Brock
    wrote on last edited by
    #4

    This is correct. It will be the carriage return ('\n' = 0x0A) character that is left in the input buffer. And as you suggested, fflush(stdin); should fix the problem

    M 1 Reply Last reply
    0
    • L Luc Pattyn

      Before you start guessing, why not collect some information? How about printing the values of type and pounds right after your scanf got them? add a simple printf line, and you will probably know what is going on. :)

      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

      M Offline
      M Offline
      Mike Certini
      wrote on last edited by
      #5

      Luc, I will do this on my next post. Thank you for the recommendation.

      1 Reply Last reply
      0
      • A Andrew Brock

        This is correct. It will be the carriage return ('\n' = 0x0A) character that is left in the input buffer. And as you suggested, fflush(stdin); should fix the problem

        M Offline
        M Offline
        Mike Certini
        wrote on last edited by
        #6

        Andrew, Excellent! Thank you for your reply. Can you answer another question? Why is my "q" selection or default not working?

        A M 2 Replies Last reply
        0
        • M Mike Certini

          Andrew, Excellent! Thank you for your reply. Can you answer another question? Why is my "q" selection or default not working?

          A Offline
          A Offline
          Andrew Brock
          wrote on last edited by
          #7

          there are 2 reasons: 1. the scanf function wont return until it gets a char and a float 2. You are only breaking out of the switch statement, not the while loop

          switch (type) {
          //other cases
          case 'q':
          break;
          default:
          break;
          }

          those are simply breaking out of the switch statement. you need to tell the while loop to exit

          #include <conio.h> //for _getch()

          //#defines

          int main() {
          //declare variables
          while (ordercom == 'Y') {
          //printf ...
          type = _getch(); //get a single character
          putc(type, stdout); //print that character to stdout
          if (type == 'q') {
          break;
          }
          scanf("%f",&pounds);
          switch (type) {
          //other cases
          default:
          ordercom = 'N'; //Is this really what you want? this will close the program if they make the wrong choice
          break;
          }
          }
          }

          M 1 Reply Last reply
          0
          • A Andrew Brock

            there are 2 reasons: 1. the scanf function wont return until it gets a char and a float 2. You are only breaking out of the switch statement, not the while loop

            switch (type) {
            //other cases
            case 'q':
            break;
            default:
            break;
            }

            those are simply breaking out of the switch statement. you need to tell the while loop to exit

            #include <conio.h> //for _getch()

            //#defines

            int main() {
            //declare variables
            while (ordercom == 'Y') {
            //printf ...
            type = _getch(); //get a single character
            putc(type, stdout); //print that character to stdout
            if (type == 'q') {
            break;
            }
            scanf("%f",&pounds);
            switch (type) {
            //other cases
            default:
            ordercom = 'N'; //Is this really what you want? this will close the program if they make the wrong choice
            break;
            }
            }
            }

            M Offline
            M Offline
            Mike Certini
            wrote on last edited by
            #8

            Andrew, Thanks. I am not familiar with putc() and _getch(). I will take a look into this. Mike

            1 Reply Last reply
            0
            • M Mike Certini

              Andrew, Excellent! Thank you for your reply. Can you answer another question? Why is my "q" selection or default not working?

              M Offline
              M Offline
              Malli_S
              wrote on last edited by
              #9

              How did u confirm they are not working ? Try to put printf() in those breaks and check/debug.

              [Delegates]      [Virtual Desktop]      [Tray Me !]
              -Malli...! :rose:****

              M A 2 Replies Last reply
              0
              • M Malli_S

                How did u confirm they are not working ? Try to put printf() in those breaks and check/debug.

                [Delegates]      [Virtual Desktop]      [Tray Me !]
                -Malli...! :rose:****

                M Offline
                M Offline
                Mike Certini
                wrote on last edited by
                #10

                Malli, Thank you for your input. I found a number of corrections on other boards. As a result here is the corrected version:

                #include <stdio.h>
                #include <conio.h>

                #define Artichoke 1.25
                #define Beets .65
                #define Carrots .89
                #define Discount .05
                #define Shipping1 10.00
                #define Shipping2 8.00

                int main(void)
                {
                char ordercom;
                char type;
                float pounds;
                float gross;
                float poundTot = 0;
                ordercom = 'Y';
                while(ordercom == 'Y')
                {
                printf("*******************************************************************************************************************\n");
                printf("Determine What You Want To Buy\n");
                printf("1) (a)\t Artichokes\t 3) (c)\t Carrots\n");
                printf("2) (b)\t Beets\t 4) (q)\t Quit\n");
                printf("*******************************************************************************************************************\n");
                printf("\n");
                printf("Select The Type Of Vegetable You Want:\n");
                scanf("%c",&type);
                fflush(stdin);
                printf("Select The Amount Of Pounds You Want To Purchase:\n");
                scanf("%f",&pounds);
                fflush(stdin);
                switch(type)
                {
                case 'a' :
                {
                gross = pounds * Artichoke;
                ordercom = 'Y';
                break;
                }
                case 'b' :
                {
                gross = pounds * Beets;
                ordercom = 'Y';
                break;
                }
                case 'c' :
                {
                gross = pounds * Carrots;
                ordercom = 'Y';
                break;
                }
                case 'q' :
                {
                ordercom = 'N';
                break;
                }
                default :
                {
                ordercom = 'N';
                break;
                }
                }
                }

                }
                
                A 2 Replies Last reply
                0
                • M Malli_S

                  How did u confirm they are not working ? Try to put printf() in those breaks and check/debug.

                  [Delegates]      [Virtual Desktop]      [Tray Me !]
                  -Malli...! :rose:****

                  A Offline
                  A Offline
                  Andrew Brock
                  wrote on last edited by
                  #11

                  for the breaks in the switch statement, I could just see what was hapening for the rest I placed a breakpoint on the line switch(type) so when it hit that I could see what the value of the variables were. for the 2nd time in the loop type = '\n' (0x0A) when you hover over the variable name. For the 'q' case for quitting I just know how scanf works, that it wont return until it gets a new line character, so another solution was needed

                  1 Reply Last reply
                  0
                  • M Mike Certini

                    Malli, Thank you for your input. I found a number of corrections on other boards. As a result here is the corrected version:

                    #include <stdio.h>
                    #include <conio.h>

                    #define Artichoke 1.25
                    #define Beets .65
                    #define Carrots .89
                    #define Discount .05
                    #define Shipping1 10.00
                    #define Shipping2 8.00

                    int main(void)
                    {
                    char ordercom;
                    char type;
                    float pounds;
                    float gross;
                    float poundTot = 0;
                    ordercom = 'Y';
                    while(ordercom == 'Y')
                    {
                    printf("*******************************************************************************************************************\n");
                    printf("Determine What You Want To Buy\n");
                    printf("1) (a)\t Artichokes\t 3) (c)\t Carrots\n");
                    printf("2) (b)\t Beets\t 4) (q)\t Quit\n");
                    printf("*******************************************************************************************************************\n");
                    printf("\n");
                    printf("Select The Type Of Vegetable You Want:\n");
                    scanf("%c",&type);
                    fflush(stdin);
                    printf("Select The Amount Of Pounds You Want To Purchase:\n");
                    scanf("%f",&pounds);
                    fflush(stdin);
                    switch(type)
                    {
                    case 'a' :
                    {
                    gross = pounds * Artichoke;
                    ordercom = 'Y';
                    break;
                    }
                    case 'b' :
                    {
                    gross = pounds * Beets;
                    ordercom = 'Y';
                    break;
                    }
                    case 'c' :
                    {
                    gross = pounds * Carrots;
                    ordercom = 'Y';
                    break;
                    }
                    case 'q' :
                    {
                    ordercom = 'N';
                    break;
                    }
                    default :
                    {
                    ordercom = 'N';
                    break;
                    }
                    }
                    }

                    }
                    
                    A Offline
                    A Offline
                    Andrew Brock
                    wrote on last edited by
                    #12

                    There is no harm in it, but in the cases for 'a', 'b' and 'c' you dont need to set ordercom = 'Y'; because it already has that value. You should no longer need #include <conio.h> now that you got rid of _getch()

                    1 Reply Last reply
                    0
                    • M Mike Certini

                      Malli, Thank you for your input. I found a number of corrections on other boards. As a result here is the corrected version:

                      #include <stdio.h>
                      #include <conio.h>

                      #define Artichoke 1.25
                      #define Beets .65
                      #define Carrots .89
                      #define Discount .05
                      #define Shipping1 10.00
                      #define Shipping2 8.00

                      int main(void)
                      {
                      char ordercom;
                      char type;
                      float pounds;
                      float gross;
                      float poundTot = 0;
                      ordercom = 'Y';
                      while(ordercom == 'Y')
                      {
                      printf("*******************************************************************************************************************\n");
                      printf("Determine What You Want To Buy\n");
                      printf("1) (a)\t Artichokes\t 3) (c)\t Carrots\n");
                      printf("2) (b)\t Beets\t 4) (q)\t Quit\n");
                      printf("*******************************************************************************************************************\n");
                      printf("\n");
                      printf("Select The Type Of Vegetable You Want:\n");
                      scanf("%c",&type);
                      fflush(stdin);
                      printf("Select The Amount Of Pounds You Want To Purchase:\n");
                      scanf("%f",&pounds);
                      fflush(stdin);
                      switch(type)
                      {
                      case 'a' :
                      {
                      gross = pounds * Artichoke;
                      ordercom = 'Y';
                      break;
                      }
                      case 'b' :
                      {
                      gross = pounds * Beets;
                      ordercom = 'Y';
                      break;
                      }
                      case 'c' :
                      {
                      gross = pounds * Carrots;
                      ordercom = 'Y';
                      break;
                      }
                      case 'q' :
                      {
                      ordercom = 'N';
                      break;
                      }
                      default :
                      {
                      ordercom = 'N';
                      break;
                      }
                      }
                      }

                      }
                      
                      A Offline
                      A Offline
                      Andrew Brock
                      wrote on last edited by
                      #13

                      1 more thing I missed at a first glance. For the case when the user presses q to quit, they still need to enter a float for the weight. you should have the 2nd scanf inside an if to check for that

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups