Thread: HOw can I shorten my code?

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    28

    HOw can I shorten my code?

    Code:
    //Programming in C
    
    
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    int temp;
    float amount = 0,tax = 0.12;
    float resolver_Func(int temp)
    {
        switch(temp)
        {
        case 1:
            return 40;
            break;
        case 2:
            return 40;
            break;
        case 3:
            return 40;
            break;
        case 4:
            return 80;
            break;
        case 5:
            return 30;
            break;
        case 6:
            return 25;
            break;
        case 7:
            return 380;
            break;
        default:
            return 0;
            break;
        }
    
    
        exit(0);
    }
    void take_order(int opt)
    {
        printf("\n\n");
        int i;
        for (i = 0; i < opt; i++)
        {
            printf("What's the %d dish number? ", (i+1));
            scanf("%d", &temp);
            amount += resolver_Func(temp);
        }
        amount += (amount*tax);
        printf("Final amount = %f", amount);
    }
    int main(void)
    {
        float opt;
        system("cls");
        printf("FOOD DISH \t PRICE(Rs.)");
    
    
        printf("\n1.)Rajma Chawal \t 40");
        printf("\n2.) Kadhi Chawal \t 40");
        printf("\n3.) Dal Chawal \t 40");
        printf("\n4.) Roti & 2 sabjis \t 80");
        printf("\n5.) Omlette \t 30");
        printf("\n6.) Egg Roll \t 25");
        printf("\n7.) Maharaja Thali \t 380");
    
    
    
    
        printf("\n\n User please enter how many items you want to place order of? ");
        scanf("%f", &opt);
    
    
        if(opt <2)
        {
            printf("Ok1 Processing your request...");
            take_order(opt);
        } else
        {
            printf("Ok1 Processing your request...");
    
    
            take_order(opt);
        }
    
    
        getch();
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Separate code from data.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct dish {
      const char *description;
      float       price;
    } dishes[] = {
      { "Rajma Chawal", 40 },
      { "Kadhi Chawal", 40 },
      { "Dal Chawal", 40 },
      { "Roti & 2 sabjis", 80 },
      { "Omlette", 30 },
      { "Egg Roll", 25 },
      { "Maharaja Thali", 380 },
    };
    
    void printMenu ( struct dish *d, size_t n ) {
      for ( unsigned int i = 0 ; i < n ; i++ ) {
        printf("%u.) %s\t%f\n",
          i+1, d[i].description, d[i].price );
      }
    }
    
    void takeOrder ( struct dish *d, size_t n ) {
      unsigned int what;
      printf("What do you want? ");
      scanf("%u",&what);
      if ( what > 0 && what < n ) {
        printf("%s costs %f\n",
          d[what-1].description, d[what-1].price);
      }
    }
    
    int main ( ) {
      printMenu(dishes,sizeof(dishes)/sizeof(dishes[0]));
      takeOrder(dishes,sizeof(dishes)/sizeof(dishes[0]));
    }
    Extending this to read the menu from a file becomes trivial.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making a function to shorten my code?
    By evilcubed in forum C Programming
    Replies: 8
    Last Post: 12-08-2012, 11:46 AM
  2. Anyway to shorten this if statement?
    By dayalsoap in forum C Programming
    Replies: 3
    Last Post: 08-08-2011, 11:19 AM
  3. help to shorten my code.
    By hugoguan in forum C Programming
    Replies: 7
    Last Post: 12-01-2010, 02:19 AM
  4. [C] How to shorten this program?
    By milky in forum C Programming
    Replies: 13
    Last Post: 11-07-2009, 10:44 AM
  5. Need to simplify/shorten this code. Help.
    By Lonck in forum C++ Programming
    Replies: 5
    Last Post: 11-08-2007, 04:23 AM

Tags for this Thread