Thread: help with void function

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

    help with void function

    Code:
     
    
     #include<stdio.h>
    int fiction();
    int art();
    int engin();
    int child();
    int study();
    void display();
    char menu();
    int main()
    
    {
        char option;
        printf("WELCOME TO ONLINEBUKU.COM\n");
        menu();
       
        
        system("pause");
        return 0;
    }
     char menu()
     {
        char option;
        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");
        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;
         }
    }
     int fiction()
     {
          char ans;
          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);
          printf("\tPress y-continue/n-stop:");
          scanf("%d",&ans);
          if(ans=='y')
          {
               menu();
          }
          else
          {
              display();
          }
    }  
     int art()
     {
          char ans;
          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);
          printf("\tPress y-continue/n-stop:");
          scanf("%d",&ans);
          if(ans=='y')
          {
               menu();
          }
          else
          {
              display();
          }
     }
     int engin()
     {
          char ans;
          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);
          printf("\tPress y-continue/n-stop:");
          scanf("%d",&ans);
          if(ans=='y')
          {
               menu();
          }
          else
          {
              display();
          }
     }
     int child()
     {
          char ans;
          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);
          printf("\tPress y-continue/n-stop:");
          scanf("%d",&ans);
          if(ans=='y')
          {
               menu();
          }
          else
          {
              display();
          }
     }
     int study()
     {
          char ans;
          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);
          printf("\tPress y-continue/n-stop:");
          scanf("%d",&ans);
          if(ans=='y')
          {
               menu();
          }
          else
          {
              display();
          }
     }
     void display()
     {
          printf("Good Bye!");
    }
    why cant the program return to menu function when the user entered 'y'?
    do i have to use a loop instead of if-else?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    How does this differ from your other thread?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    You are using the integer type specifier in scanf ("%d"). You correctly scanned for a character in your 'menu' function, you have to use the character type specifier ("%c").

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    29
    change %d to %c.,but the program stops before the user entered their choice

  5. #5
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    You should try and learn to debug properly. Debugging can be as simple as adding print statements before parts of the code that you want to test. After you receive the choice from the user, add a printf statement that prints the character to the screen (preferrably as an integer, in case the character is a white space character).

  6. #6
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    yeah..if he printed it out..he would know that scanf() returns because there is a newline in the buffer (this must be the fourth thread I have posted this in, scanf is a b**ch lol)


    Anyways remove the newline from the buffer
    Code:
         scanf("%d",&amount);
         getchar() ;// Here
          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;
           }
    do that for the rest...
    btw, won't it be better to use a loop (nice way of avoiding it though..i think)
    And you don't return anything, so why not use void?
    You ended that sentence with a preposition...Bastard!

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    31
    Quote Originally Posted by amoeba532 View Post
    [code]



    why cant the program return to menu function when the user entered 'y'?
    do i have to use a loop instead of if-else?

    It can return to menu! Use the return statement.
    (lots of room for other improvements in the code)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 08-30-2010, 09:26 AM
  2. Matrix operations using objects
    By circuitbreaker in forum C++ Programming
    Replies: 7
    Last Post: 05-04-2010, 08:53 AM
  3. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM