Thread: void

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    29

    void

    Code:
     
    #include<stdio.h>
    int fiction();
    int art();
    int engin();
    int child();
    int study();
    int main()
    {
        char option,ans;
        printf("WELCOME TO ONLINEBUKU.COM\n");
        printf("WHICH CATEGORY YOU WANT TO CHOOSE?\n");
        printf("   A: FICTION BOOKS\n");
        printf("   B: ART AND CRAFT BOOKS\n");
        printf("   C: ENGINEERING & SCIENCE BOOKS\n");
        printf("   D: CHILDREN BOOKS\n");
        printf("   E: ISLAMIC STUDIES BOOKS\n");
        do
        {
           printf("\n");
           printf("YOUR OPTION:");
           scanf("%c",&option);
           printf("\n");
        
          switch(option)
         {
           case 'A': fiction();break;
           case 'B': art();break;
           case 'C': engin();break;
           case 'D': child();break;
           case 'E':  study();break;
         }
          printf("Press y to continue:");
          scanf("%c",&ans);
        }while(ans=='y');
        
        system("pause");
        return 0;
    }
     int fiction()
     {
         
          int books,amount;
          float pre=0.0;
          printf("\tOPTION A: FICTION BOOKS  \t\t PRICE (UNIT)\n");
          printf("\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
          printf("\t1.Zeus King Of The Gods                  RM  58.20\n");
          printf("\t2.Penny Century: A Love and Rockets Book RM  78.90\n");
          printf("\t3.Map of the Invisible World             RM  34.60\n");
          printf("\t4.Jodi Picoult: House Rules              RM  32.90\n");
          printf("\tPlease key in your choice:");
          scanf("%d",&books);
          printf("\tPlease enter the amount:");
          scanf("%d",&amount);
          switch(books)
           {
               case 1: pre=amount*58.2;break;
               case 2: pre=amount*78.9;break;
               case 3: pre=amount*34.6;break;
               case 4: pre=amount*32.9;break;
           }
          printf("\tThe total price is %0.2f\n",pre);
    }  
     int art()
     {
          int books,amount;
          float pre=0.0;
          printf("\tOPTION B: ART AND CRAFT BOOKS  \t\tPRICE (UNIT)\n");
          printf("\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
          printf("\t1.Connoisseur'S Guide To Antique Silver  RM  58.20\n");
          printf("\t2.Best Ever Craft Project Book           RM  38.50\n");
          printf("\t3.Encyclopedia Of Acrylic Techni Ques    RM  89.60\n");
          printf("\t4.Teach Yourself Calligraphy             RM 100.50\n");
           printf("\tPlease key in your choice:");
          scanf("%d",&books);
          printf("\tPlease enter the amount:");
          scanf("%d",&amount);
          switch(books)
          {
               case 1: pre=amount*58.2;break;
               case 2: pre=amount*38.5;break;
               case 3: pre=amount*89.6;break;
               case 4: pre=amount*100.5;break;
          }
          printf("\tThe total price is %0.2f\n",pre);
     }
     int engin()
     {
         
          int books,amount;
          float pre=0.0;
          printf("\tOPTION C: ENGINEERING & SCIENCE BOOKS  PRICE (UNIT)\n");
          printf("\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
          printf("\t1.Solar Power For Your Home             RM  71.80\n");
          printf("\t2.Chemical Engineering Thermodynamic    RM  69.90\n");
          printf("\t3.Marine Electrical Technology          RM 128.60\n");
          printf("\t4.Engineering Design Reliability        RM 112.90\n");
           printf("\tPlease key in your choice:");
          scanf("%d",&books);
          printf("\tPlease enter the amount:");
          scanf("%d",&amount);
          switch(books)
          {
               case 1: pre=amount*71.8;break;
               case 2: pre=amount*69.9;break;
               case 3: pre=amount*128.6;break;
               case 4: pre=amount*112.9;break;
          }
          printf("\tThe total price is %0.2f\n",pre);
     }
     int child()
     {
          int books,amount;
          float pre=0.0;
          printf("\tOPTION D: CHILDREN BOOKS  \t    PRICE (UNIT)\n");
          printf("\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
          printf("\t1. Kasih Sahabat                      RM  11.90\n");
          printf("\t2. Adikku Istimewa                    RM  12.00\n");
          printf("\t3. 101 Akhlak Rasulullah              RM  32.00\n");
          printf("\t4. Legasi Blok E: Kebangkitan Kuasa   RM  14.50\n");
          printf("\tPlease key in your choice:");
          scanf("%d",&books);
          printf("\tPlease enter the amount:");
          scanf("%d",&amount);
          switch(books)
          {
               case 1: pre=amount*11.9;break;
               case 2: pre=amount*12.0;break;
               case 3: pre=amount*32.0;break;
               case 4: pre=amount*14.5;break;
          }
          printf("\tThe total price is %0.2f\n",pre);
     }
     int study()
     {
          int books,amount;
          float pre=0.0;
          printf("\tOPTION E: ISLAMIC STUDIES BOOKS \tPRICE (UNIT)\n");
          printf("\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
          printf("\t1. Mencari Islam di China               RM  23.30\n");
          printf("\t2. From Cat Steven to Yusuf Islam       RM  20.00\n");
          printf("\t3. Ketika Hatiku Ingin Berhijrah        RM  25.50\n");
          printf("\t4. Taatilah Allah, Rasul & Ulil Amri    RM  12.50\n");
          printf("\tPlease key in your choice:");
          scanf("%d",&books);
          printf("\tPlease enter the amount:");
          scanf("%d",&amount);
          switch(books)
          {
               case 1: pre=amount*23.3;break;
               case 2: pre=amount*20.0;break;
               case 3: pre=amount*25.5;break;
               case 4: pre=amount*12.5;break;
          }
          printf("\tThe total price is %0.2f\n",pre);
     }
    why can't the program be repeated when i used the loop? can't a loop have a switch in it?
    Last edited by amoeba532; 01-16-2011 at 05:04 AM. Reason: changing the program

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    yeah, something like :

    Code:
    while ( option != 'q' || option != 'Q')
    {
       switch...
    }
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by amoeba532 View Post
    why can't the program be repeated when i used the loop? can't a loop have a switch in it?
    Why can't it? What is it doing specifically behavior-wise that is different from what you expect?

    You do have a problem with how you are handling your input with the scanf calls. Input is processed once the enter key is pressed which stuffs a newline character in the input buffer. When you use %c to read a character, it will read and process any character that happens to be in the input buffer which could lead to unexpected behavior if, for example, the buffer contains a solitary newline character that is left over from a previous input attempt.

    Let's say you select your menu option of 'A' for example. The %c works grabbing the 'A' character and stuffing it into the option variable and leaving the newline in the input buffer. In your fiction function you have two scanf's that process integer values (%d) these both ignore leading newlines before reading/processing the integer values that are present. The last scanf (the one that reads the amount) will leave it's own newline in the input buffer. When you exit the fiction function and then return to the main function, that newline character will still be in the input buffer. Now, you ask the user if they want to continue or not and try to get input. This is where the problem now manifests itself. That leftover newline character in the input buffer is still there and the scanf("%c",&ans); statment will happily read that newline character behaving as if you - the user - pressed the enter key without typing anything else at the prompt. The comparison will check '/n' against 'y' - which will never be true - and you will therefore exit the do-while loop. What you see when this happens is that the code appears to skip past the part where it asks you if you wish to continue or not and simply exits the program.

    This same scenario will occur regardless of what option you choose. All you current options will result in a trailing newline character being left in the input buffer when you arrive at the "Press y to continue" code.

    So... now that we have that out of the way, what do we do about it? You need to be aware of how scanf behaves when using the %c format specifier and try to anticipate the flow of I/O throughout your program. As far as solutions go, there is a trick to scanf that you can use to have it ignore leading newline characters such that the expected behavior will be present and that is to put a space in front of the %c format specifier as such:
    Code:
        do
        {
           printf("\n");
           printf("YOUR OPTION:");
           scanf(" %c",&option);  /* Notice space in front of %c here */
           printf("\n");
        
          switch(option)
         {
           case 'A': fiction();break;
           case 'B': art();break;
           case 'C': engin();break;
           case 'D': child();break;
           case 'E':  study();break;
         }
          printf("Press y to continue:");
          scanf(" %c",&ans);  /* Notice space in front of %c here */
        }while(ans=='y');
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrix operations using objects
    By circuitbreaker in forum C++ Programming
    Replies: 7
    Last Post: 05-04-2010, 08:53 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. msvc just ate one of my source files
    By Eber Kain in forum C++ Programming
    Replies: 6
    Last Post: 07-01-2004, 05:40 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM