Thread: Simple Restaurant Point-of-Sale Application: Help?!

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    54

    Simple Restaurant Point-of-Sale Application: Help?!

    I've been having a lot of trouble with this one, any help would be great.

    I basically have to create a point of sale application for tabulating a bill. The waiter/waitress has menu/drink/desert options that they can enter, as well as the ability to apply a discount and tip percentage, and then have the final bill tabulated.....

    For the life of me I can't get it to work properly, I've been on this for nearly two days straight and I can't figure this out! :S

    PS: I'm new to this language, please be gentle :P lol

    EDIT: One of the big problems I'm having is in regards to a counter. Beside the items on the menu is a counter that shows how many of each item has been ordered, and must be updated in 'real time' as the order changes (same with subtotal, total)....I've gone through the notes I was provided and have followed it through, but it doesn't seem to make much sense..........my attempt is below lol

    Code:
    
    #include <stdio.h>  	// def's 4 printf,scanf,gets,puts,getchar,fflush
    #include <conio.h>      // def's 4 getch
    #include <stdlib.h>     // def's 4 system
    
    #define GST 0.05
    #define PST 0.08
    #define i1 "ROAST PRIME RIB "
    #define i1_p 12.45
    #define i2 "HOT BBQ BEEF Sandwich"
    #define i2_p 9.99
    #define i3 "GRILLED CHEESE/FRIES "
    #define i3_p 5.99
    #define i4 "FILET MIGNON "
    #define i4_p 21.99
    #define i5 "CLUB HOUSE SANDWICH "
    #define i5_p 10.99
    #define i6 "B.L.T. SANDWICH "
    #define i6_p 6.99
    #define i7 "VEGETARIAN CHOW MEIN "
    #define i7_p 8.95
    #define i8 "ATLANTIC SALMON "
    #define i8_p 16.45
    #define i9 "CHEDDAR CHEESE BURGER"
    #define i9_p 9.57
    #define i10 "GRAIN FED VEAL "
    #define i10_p 25.00
    #define i11 "POP "
    #define i11_p 1.65
    #define i12 "Juice "
    #define i12_p 2.49
    #define i13 "Coffe "
    #define i13_p 1.99
    #define i14 "Shakes "
    #define i14_p 2.85
    #define i15 "APPLE PIE"
    #define i15_p 3.55
    #define i16 "Banana Sp"
    #define i16_p 4.99
    
    int main()
    {
    	
    	do
    	{
    	int a_counter, b_counter, c_counter, d_counter, e_counter, f_counter;
    	int g_counter, h_counter, i_counter, j_counter, k_counter, l_counter, m_counter;
    	int n_counter, o_counter, p_counter;
    	double total;
    	double discount, subtotal, tip, pst, gst;
    	double tippercent;
    	char choice;
    	a_counter=0;
    	b_counter=0;
    	c_counter=0;
    	d_counter=0;
    	e_counter=0;
    	f_counter=0;
    	g_counter=0;
    	h_counter=0;
    	i_counter=0;
    	j_counter=0;
    	k_counter=0;
    	l_counter=0;
    	m_counter=0;
    	n_counter=0;
    	o_counter=0;
    	p_counter=0;
    	subtotal=0;
    	discount=0;
    	gst=0;
    	tip=0;
    	pst=0;
    	total=0;
    	system ("cls");
    	printf("\n\t\t\t\tDAVE'S RESTAURANT");
    	printf("\n\nItem   ENTREES\t\t\tPrices\t#     Item\tDrinks\t   Prices    #");
    	printf("\n--------------------------------------------------------------------------------");
    	puts("");
    	printf(" A     %s \t%.2f\t%i\tK\t%s\t    %.2f     %i",i1, i1_p, a_counter, i11, i11_p, k_counter);
    	printf("\n B     %s \t%.2f\t%i\tL\t%s\t    %.2f     %i",i2, i2_p, b_counter, i12, i12_p, l_counter);
    	printf("\n C     %s \t%.2f\t%i\tM\t%s\t    %.2f     %i",i3, i3_p, c_counter, i13, i13_p, m_counter);
    	printf("\n D     %s \t\t%.2f\t%i",i4, i4_p, d_counter);
    	printf("\n E     %s \t%.2f\t%i",i5, i5_p, e_counter);
    	printf("\n F     %s \t%.2f\t%i\t\tDESSERTS",i6, i6_p, f_counter);
    	printf("\n G     %s \t%.2f\t%i",i7, i7_p, g_counter);
    	printf("\n H     %s \t%.2f\t%i\tN\t%s\t    %.2f     %i",i8, i8_p, h_counter, i14, i14_p, n_counter);
    	printf("\n I     %s \t%.2f\t%i\tO\t%s   %.2f     %i",i9, i9_p, i_counter, i15, i15_p, o_counter);
    	printf("\n J     %s \t\t%.2f\t%i\tP\t%s   %.2f     %i",i10, i10_p, j_counter, i16, i16_p, p_counter);
    	printf("\n");
    	printf("\n================================================================================");
    	printf("\n");
    
    	pst = subtotal * PST;
    	gst = subtotal * GST;
    	subtotal = i1_p+i2_p+i3_p+i4_p+i5_p+i6_p+i7_p+i8_p+i9_p+i10_p+i11_p+i12_p+i13_p+i14_p+i15_p+i16_p;
    	total = i1_p+i2_p+i3_p+i4_p+i5_p+i6_p+i7_p+i8_p+i9_p+i10_p+i11_p+i12_p+i13_p+i14_p+i15_p+i16_p+gst+pst+tip-discount;
    
    	printf("\n\t\t\t\t\t\tSubtotal = %.2f",subtotal);
    	printf("\n\t\t\t\t\t\tPST = %.2f",pst);
    	printf("\n\t\tSPECIAL DISCOUNT\t\t\tGST = ",gst);
    	printf("\n\t\t%.2f\t\t\t\tTIP = ",discount, tip);
    	printf("\n\t\t\t\t\t\tTOTAL = ",total);
    	
    	
    	printf("\n================================================================================");
    	printf("\nSELECT (A-P), R-RESET, - CORRECT, T-TIP, S-DISCOUNT, X-EXIT");
    	scanf("%c", &choice);
    	
    	switch (choice)
    	{
    	case 'a':
    	case 'A':
    		a_counter++;
    		total = total + i1_p;
    		break;
    
    		case 'b':
    		case 'B':
    		b_counter++;
    		total = total + i2_p;
    		break;
    
    		case 'c':
    		case 'C':
    		c_counter++;
    		total = total + i3_p;
    		break;
    
    		case 'd':
    		case 'D':
    		d_counter++;
    		total = total + i4_p;
    		break;
    
    		case 'e':
    		case 'E':
    		e_counter++;
    		total = total + i5_p;
    		break;
    
    		case 'f':
    		case 'F':
    		f_counter++;
    		total = total + i6_p;
    		break;
    
    		case 'g':
    		case 'G':
    		g_counter++;
    		total = total + i7_p;
    		break;
    
    		case 'h':
    		case 'H':
    		h_counter++;
    		total = total + i8_p;
    		break;
    
    		case 'i':
    		case 'I':
    		i_counter++;
    		total = total + i9_p;
    		break;
    
    		case 'j':
    		case 'J':
    		j_counter++;
    		total = total + i10_p;
    		break;
    
    		case 'k':
    		case 'K':
    		k_counter++;
    		total = total + i11_p;
    		break;
    
    		case 'l':
    		case 'L':
    		l_counter++;
    		total = total + i12_p;
    		break;
    
    		case 'm':
    		case 'M':
    		m_counter++;
    		total = total + i13_p;
    		break;
    
    		case 'n':
    		case 'N':
    		n_counter++;
    		total = total + i14_p;
    		break;
    
    		case 'o':
    		case 'O':
    		o_counter++;
    		total = total + i15_p;
    		break;
    
    		case 'p':
    		case 'P':
    		p_counter++;
    		total = total + i16_p;
    		break;
    
    		case 't':
    		case 'T':
    		p_counter++;
    		printf ("ENTER TIP PERCENTAGE ");
    		scanf ("%f", &tippercent);
    		tip = ((subtotal)*(tippercent))/100;
    		break;
    
    		case 'x':
    		case 'X':
    		printf ("Press any Key to Exit");
    		getch();
    		break;
    
    	default:
    		printf("INVALID CHOICE!\a\a");
    	}
    	}
    while (1);
    
    return 0;
    }
    Last edited by matt.s; 04-15-2010 at 12:50 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You've done a lot of work, and it's hard to intuitively deal with it, because the item has no association with it's price: i.e.:

    a_counter price is i1_p Oh clam!

    I don't want to appear unkind but

    Is there a match in the house?


    When you see a lot of stuff like this, you need to STOP, and think about it. Almost always the program needs to be redesigned - different data types, structures, or algorithm. Sometimes a little abstraction of something in the program, helps a lot.

    So, how about we start with the match, and make up:

    1) a 2D string to hold the description of the items being charged, and
    2) a price array to hold the prices (inspired name, no?)

    3) A cust(omer) struct to hold the essentials:

    Code:
    struct customer {
      char name[25];
      long food charges;
      long drink charges;
      long tip;
      long tax;
    }
    We'll define it outside main, so all functions can access the definition of the array, and then create the actual array, inside main(), and pass it around to other functions, as needed. An array of 50 struct customers cust[50], should be plenty.

    Right off, you want to multiply all floats *immediately* by 100 and make them all pennies. That eliminates float problems and we'll use type long int, just in case, in all array and nearly all variables. We'll print them out correctly, of course.

    Now we put the descriptions in sorted order alphabetically, and put their associated prices, in the same index number, over in the prices[]. So if Atlantic Salmon is #8 in the descriptions array, it will also be #8 in the prices array.


    I would save the menu part, and ditch the rest. The var's would be replaced, of course. One of the big tasks programmers have, is to make their code, simple enough that others can study it, and know WTF the program is doing in each function. Clear concise code, is very important. Not all code can be that way, of course. Yours can, and should be.

    it's too late to do much with it right now, but tomorrow, if you agree, we can go further.
    Last edited by Adak; 04-15-2010 at 03:55 AM.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    ^^

    Thanks for the reply, I have another day or so to get this done so I'd absolutely like to give it another go...

    I follow most of your theory but I'm still confused a bit, in regards to the functions. Outside of main, what other functions would you need? ones for menu, tax, subtotal, total?

    and in regards to the pricing, lets say for example i use #define i1_p 12.99 and implement it in the menu selection, can I not get the totals by simply adding them in the totals sections?

    thanks!

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It's your program, and nothing says you can't add defines together - they're just literal replacements done before the program is compiled.

    This is an idea of how I'd move your program over to another design:

    Code:
    #include <stdio.h>  	// def's 4 printf,scanf,gets,puts,getchar,fflush
    #include <conio.h>      // def's 4 getch
    #include <stdlib.h>     // def's 4 system
    
    #define GST 5
    #define PST 8
    
    int main()
    {
    	
      int counter[18]=0;
      long total, discount, subtotal, tip, pst, gst, tippercent;
      char choice[50]={'\0'};
      item[25][35] = {
    {"APPLE PIE"},
    {"ATLANTIC SALMON"},
    {"Banana Sp"},
    {"B.L.T. SANDWICH"},
    {"CHEDDAR CHEESE BURGER"},
    {"CLUB HOUSE SANDWICH"},
    {"Coffe"},
    {"FILET MIGNON"},
    {"GRILLED CHEESE/FRIES"},
    {"GRAIN FED VEAL"},
    {"HOT BBQ BEEF Sandwich"},
    {"Juice "},
    {"POP"},
    {"ROAST PRIME RIB"},
    {"Shakes"},
    {"VEGETARIAN CHOW MEIN"}
    }
    
      int price[25]={355,1645,499,957,699,1099,199,2199,599,2500,999,249,165,1245,285,895);
      subtotal=discount=gst=tip=pst=total=0;
      
      do
      {
    	system ("cls");
    	printf("\n\t\t\t\tDAVE'S RESTAURANT");
    	printf("\n\nItem   ENTREES\t\t\tPrices\t#     Item\tDrinks\t   Prices    #");
    	printf("\n--------------------------------------------------------------------------------");
    
    	printf("\n A     %s \t%.2f\t%i\tK\t%s\t    %.2f     %i",i1, i1_p, a_counter, i11, i11_p, k_counter);
    	printf("\n B     %s \t%.2f\t%i\tL\t%s\t    %.2f     %i",i2, i2_p, b_counter, i12, i12_p, l_counter);
    	printf("\n C     %s \t%.2f\t%i\tM\t%s\t    %.2f     %i",i3, i3_p, c_counter, i13, i13_p, m_counter);
    	printf("\n D     %s \t\t%.2f\t%i",i4, i4_p, d_counter);
    	printf("\n E     %s \t%.2f\t%i",i5, i5_p, e_counter);
    	printf("\n F     %s \t%.2f\t%i\t\tDESSERTS",i6, i6_p, f_counter);
    	printf("\n G     %s \t%.2f\t%i",i7, i7_p, g_counter);
    	printf("\n H     %s \t%.2f\t%i\tN\t%s\t    %.2f     %i",i8, i8_p, h_counter, i14, i14_p, n_counter);
    	printf("\n I     %s \t%.2f\t%i\tO\t%s   %.2f     %i",i9, i9_p, i_counter, i15, i15_p, o_counter);
    	printf("\n J     %s \t\t%.2f\t%i\tP\t%s   %.2f     %i",i10, i10_p, j_counter, i16, i16_p, p_counter);
    	printf("\n");
    	printf("\n================================================================================");
    	printf("\n");
    
    	pst = subtotal * PST;
    	gst = subtotal * GST;
    	subtotal = ;
    	total = +gst+pst+tip-discount;
    
    	printf("\n\t\t\t\t\t\tSubtotal = %.2f",subtotal);
    	printf("\n\t\t\t\t\t\tPST = %.2f",pst);
    	printf("\n\t\tSPECIAL DISCOUNT\t\t\tGST = ",gst);
    	printf("\n\t\t%.2f\t\t\t\tTIP = ",discount, tip);
    	printf("\n\t\t\t\t\t\tTOTAL = ",total);
    	
    	
    	printf("\n================================================================================");
    	printf("\nSELECT (A-P), R-RESET, - CORRECT, T-TIP, S-DISCOUNT, X-EXIT");
    	scanf("%c", &choice);
    	
            choice = toupper(choice);  
            i = choice - 'A';   
            total += price[i];
    
    
    	switch(choice) {
            case 'T': printf ("ENTER TIP PERCENTAGE ");
    	   scanf ("%d", &tippercent);
    	   tip = (subtotal*tippercent);
               break;   
    
             case 'X':
    	   printf ("Press any Key to Exit");
               getchar();
               break;
    
    	default:
    		printf("INVALID CHOICE!\a\a");
    	}
    
         }while (1);
    
      return 0;
    }
    Since the menu lists items in columns, grouped by type, the display would need another
    Code:
    char array "Type[25] = {'\0'};
    which would have values of 'F' (for food), 'L' for liquids (drinks), and 'D' for desserts

    I encourage every forum member to step in and help if matt wants to use his original program. I can't work with it, and keep my sanity.

    Let me know if you want to work with finishing the re-design, like above. I don't see any problems finishing it in time, if you can be here, rather frequently.
    Last edited by Adak; 04-15-2010 at 04:55 PM.

  5. #5
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    I guess I really don't understand the problem. It would have been kind of nice if the OP would have stated the exact problem instead of giving the problem in his own words. But whatever... Here is what I have so far

    Code:
    #include <stdio.h>
    
    #define GST 0.05
    #define PST 0.08
    
    struct food {
      char *item;
      double price;
    }menu[] = {
      "ROAST PRIME RIB", 12.45,
      "HOT BBQ BEEF SANDWICH", 9.99,
      "GRILLED CHEESE/FRIES", 5.99,
      "FILET MIGNON", 21.99,
      "CLUB HOUSE SANDWICH", 10.99,
      "B.L.T. SANDWICH", 6.99,
      "VEGETARIAN CHOW MEIN", 8.95,
      "ATLANTIC SALMON", 16.45,
      "CHEDDAR CHEESE BURGER", 9.57,
      "GRAIN FED VEAL", 25.00,
      "POP", 1.65, 
      "Juice", 2.49,
      "Coffe", 1.99, 
      "Shakes", 2.85, 
      "APPLE PIE", 3.55,
      "Banana Sp", 4.99
    };
    
    double total(int max)
    {
      int i;
      double sum = 0.00;
      double price;
      for (i = 0; i < max; i++) {
        price += 100 * menu[i].price;
      }
    
      return price/100;
    }
    
    void print_menu(int max)
    {
      int i;
      int c = 'A';
    
      printf("\n\t\t\t\tDAVE'S RESTAURANT\n\n");
      fprintf(stdout, "%-5s%-24s%-14s\n", "Item", "ENTREES","Price");
    
      for (i = 0; i < max; i++) {
        fprintf(stdout, "%-5c%-24s%-.2f\n", c+i, menu[i].item, 
                menu[i].price);
      }
    }
    
    int main(void)
    {
      int max = sizeof(menu)/sizeof(struct food);
      print_menu(max);
      double sum;
      sum = total(max);
      /*printf("%.2f", sum);*/
    
      return 0;
    }
    I figured the A-whatever represented both the item description and the price. That way, when you enter in a letter, you can get the corresponding item and price.

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    Hey!

    thanks for getting back to me! Wow I love what you did with it so far, MUCH more simplified and coherent! Yeah the #defines were definatly a bad way to go and created such a headache when trying to tabulate everything!

    I definatly have a much more usable template to build off of, thank you so much, I will report back with my progress!

    cheers,
    matt

  7. #7
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    I'm not that sure if this is right, but what I do is have the program display the menu. The person then enters in the letter that corresponds to both the menu item and the price. Based on this, I can calculate the total. This is only a partial solution...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define GST 0.05
    #define PST 0.08
    
    struct food {
      char *item;
      double price;
    }menu[] = {
      "ROAST PRIME RIB", 12.45,
      "HOT BBQ BEEF SANDWICH", 9.99,
      "GRILLED CHEESE/FRIES", 5.99,
      "FILET MIGNON", 21.99,
      "CLUB HOUSE SANDWICH", 10.99,
      "B.L.T. SANDWICH", 6.99,
      "VEGETARIAN CHOW MEIN", 8.95,
      "ATLANTIC SALMON", 16.45,
      "CHEDDAR CHEESE BURGER", 9.57,
      "GRAIN FED VEAL", 25.00,
      "POP", 1.65, 
      "Juice", 2.49,
      "Coffe", 1.99, 
      "Shakes", 2.85, 
      "APPLE PIE", 3.55,
      "Banana Sp", 4.99
    };
    
    void print_menu(int max)
    {
      int i;
      int c = 'A';
    
      printf("\n\t\t\t\tDAVE'S RESTAURANT\n\n");
      fprintf(stdout, "%-5s%-24s%-14s\n", "Item", "ENTREES","Price");
    
      for (i = 0; i < max; i++) {
        fprintf(stdout, "%-5c%-24s%-.2f\n", c+i, menu[i].item, 
                menu[i].price);
      }
    
      printf("\nSELECT (A-P), R-RESET, - CORRECT, T-TIP, S-DISCOUNT, X-EXIT: ");
      fflush(stdout);
    }
    
    int main(void)
    {
      int max = sizeof(menu)/sizeof(struct food);
      double sum = 0.00;
      int c;
      int choice = 'A';
      int index = 0;
    
      print_menu(max);
     
      while ((c = getchar()) != EOF) {
        switch(c)
          {
          case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
          case 'G': case 'H': case 'I': case 'J': case 'K': 
          case 'L': case 'M': case 'N': case 'O': case 'P':
            index = c - 'A';
            sum += menu[index].price; 
            printf("**The current total is:%.2f\n", sum);
            break;
          case 'X': case 'x': 
            printf("**The final total is:%.2f\n", sum);
            exit(1);
          default:
            print_menu(max);
            break;
          }
      }
      
      return 0;
    }

  8. #8
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Ack, I screwed up the sum in that code.

  9. #9
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    I built the program around the following description

    I basically have to create a point of sale application for tabulating a bill. The waiter/waitress has menu/drink/desert options that they can enter, as well as the ability to apply a discount and tip percentage, and then have the final bill tabulated.....
    I didn't bother to implement the '-' option because I wasn't sure if that option was supposed to correct the last entry or some other previous entry. On top of that, I have no idea what PST and GST are supposed to be. The floating point math really slowed me down. Did anyone else that attempted this solution have rounding issues? I interpreted 'RESET' as to reset the sum and not reset the sum, discount, and tip percentage. This interpretation may or may not be correct. On top of that, I have the input for 'percent' to mean some number that is a multiple of 100. For example, the number 20 means 20 percent or .20.

    Finally, I have the program print the final total when the user chooses the 'X' option and I didn't account for case sensitivity expect on the exit option. Also, I didn't do any error checking on scanf()!.

    Anyways, below is my non-googleable solution. Yes, this all came out of my brain, and not a search engine.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define GST 0.05
    #define PST 0.08
    
    struct food {
      char *item;
      double price;
    } menu[] = {
      "ROAST PRIME RIB", 12.45,
      "HOT BBQ BEEF SANDWICH", 9.99,
      "GRILLED CHEESE/FRIES", 5.99,
      "FILET MIGNON", 21.99,
      "CLUB HOUSE SANDWICH", 10.99,
      "B.L.T. SANDWICH", 6.99,
      "VEGETARIAN CHOW MEIN", 8.95,
      "ATLANTIC SALMON", 16.45,
      "CHEDDAR CHEESE BURGER", 9.57,
      "GRAIN FED VEAL", 25.00,
      "POP", 1.65, 
      "Juice", 2.49,
      "Coffe", 1.99, 
      "Shakes", 2.85, 
      "APPLE PIE", 3.55,
      "Banana Sp", 4.99
    };
    
    double total(double index)
    {
      double price = 0.00;
     
      price += 100 * index;
      return price;
    }
    
    void print_menu(int max)
    {
      int i;
      int c = 'A';
    
      printf("\n\t\t\t\tDAVE'S RESTAURANT\n\n");
      fprintf(stdout, "%-5s%-24s%-14s\n", "Item", "ENTREES","Price");
    
      for (i = 0; i < max; i++) {
        fprintf(stdout, "%-5c%-24s%-.2f\n", c + i, menu[i].item, 
                menu[i].price);
      }
    
      printf("\nSELECT (A-P), R-RESET, - CORRECT, T-TIP, S-DISCOUNT, X-EXIT: ");
      fflush(stdout);
    }
    
    int main(void)
    {
      int max = sizeof(menu)/sizeof(struct food);
      double sum = 0.00;
      float tippercent = 0.00;
      float discount = 0.00;
      int c;
    
      print_menu(max);
     
      while ((c = getchar()) != EOF) {
        switch(c)
          {
          case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
          case 'G': case 'H': case 'I': case 'J': case 'K': 
          case 'L': case 'M': case 'N': case 'O': case 'P':
            sum += total(menu[c-'A'].price); 
            break;
          case 'R':
            sum = 0.00;
            break;
          case 'S':
            printf ("ENTER DISCOUNT ");
            fflush(stdout);
            scanf ("%f", &discount);
            break;
          case 'T':
            printf ("ENTER TIP PERCENTAGE ");
            fflush(stdout);
            scanf ("%f", &tippercent);
            break;
          case 'X': case 'x': 
            sum = sum + (((sum) * tippercent)/100);
            printf("**The final total is: %.2f\n", (sum/100) - discount);
            exit(1);
          default:
            print_menu(max);
            break;
          }
      }
      
      return 0;
    }
    Last edited by Overworked_PhD; 04-15-2010 at 06:32 PM. Reason: Yes my grammar blows.

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    Quote Originally Posted by Overworked_PhD View Post
    I built the program around the following description



    I didn't bother to implement the '-' option because I wasn't sure if that option was supposed to correct the last entry or some other previous entry. On top of that, I have no idea what PST and GST are supposed to be. The floating point math really slowed me down. Did anyone else that attempted this solution have rounding issues? I interpreted 'RESET' as to reset the sum and not reset the sum, discount, and tip percentage. This interpretation may or may not be correct. On top of that, I have the input for 'percent' to mean some number that is a multiple of 100. For example, the number 20 means 20 percent or .20.

    Finally, I have the program print the final total when the user chooses the 'X' option and I didn't account for case sensitivity expect on the exit option. Also, I didn't do any error checking on scanf()!.

    Anyways, below is my non-googleable solution. Yes, this all came out of my brain, and not a search engine.
    Hey!

    Sorry for taking so long to check back, I've been going through my code and should have something up soon hopefully....

    Yeah I apologize for being vague in my original post, in regards to the specifics of the program. In Canada GST and PST are two forms of tax that are applied to bills, to get a total. Reset should reset who entire order, including tax, tip, totals and entered orders. In regards to the discount percent, it should be a whole number that the user enters, for example the waiter/waiteress enters 20 or 80 or 100 and that will repressent a discount of 20%, 80% or 100%.

    One of the big problems I'm still having is that beside each item on the menu, I need to have a value of how many of the item has been ordered, and it must update constantly as orders are placed.....if that makes sense. So for the first item, it would have a "0" beside it, but as soon as someone pushes "A", the counter updates to a "1".............this is the biggest headache right now...

    Hope that helped!

    thx!
    Last edited by matt.s; 04-15-2010 at 07:43 PM.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Did anyone else that attempted this solution have rounding issues? I interpreted 'RESET' as to reset the sum and not reset the sum, discount, and tip percentage. This interpretation may or may not be correct. On top of that, I have the input for 'percent' to mean some number that is a multiple of 100. For example, the number 20 means 20 percent or .20.
    @Overworked: That rounding problem is exactly why you can't deal well with money, by using floats/doubles.

    Easiest way is just to avoid it all, by multiplying all money by 100, immediately. Rounding problems are now history.

    Matt: Yeah, I didn't want to work on a new idea too much, without knowing your preferences on the matter.

    Is a struct OK to use, for the order? I like them because they keep bits of data together, and would work well for an order. That's what I would use for the current order.

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    Quote Originally Posted by Adak View Post

    Is a struct OK to use, for the order? I like them because they keep bits of data together, and would work well for an order. That's what I would use for the current order.

    Yeah after looking into it a struct definatly makes the most amount of sense from what I can see, I'm working on getting it all in order now lol :P

  13. #13
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    What really sucked was that I would be off by like 2 or 3 cents. I believe I've addressed this issue, but don't quote me on it. Anyways, I managed to get everything working expect for the pst and gst thing. This is because I don't know they get calculated exactly. A sample formula would be nice.

    With that, I would like to point out that I used an array of structures, instead of a regular structure, in my original code. It's the following lines

    Code:
    struct food {
      char *item;
      double price;
      int count; /*<---I just added a count field*/
    } menu[] = {
      "ROAST PRIME RIB", 12.45, 0,
      "HOT BBQ BEEF SANDWICH", 9.99, 0,
      "GRILLED CHEESE/FRIES", 5.99, 0,
      "FILET MIGNON", 21.99, 0,
      "CLUB HOUSE SANDWICH", 10.99, 0,
      "B.L.T. SANDWICH", 6.99, 0,
      "VEGETARIAN CHOW MEIN", 8.95, 0,
      "ATLANTIC SALMON", 16.45, 0,
      "CHEDDAR CHEESE BURGER", 9.57, 0,
      "GRAIN FED VEAL", 25.00, 0,
      "POP", 1.65, 0,
      "Juice", 2.49, 0,
      "Coffe", 1.99, 0,
      "Shakes", 2.85, 0,
      "APPLE PIE", 3.55, 0,
      "Banana Sp", 4.99, 0
    };
    I went this route so that I could access the data in constant time. I don't think I could have gotten the same kind of performance using a combination of structures and string compares.

    With that, here is my updated solution..

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define GST 0.05
    #define PST 0.08
    
    struct food {
      char *item;
      double price;
      int count; /*<---I just added a count field*/
    } menu[] = {
      "ROAST PRIME RIB", 12.45, 0,
      "HOT BBQ BEEF SANDWICH", 9.99, 0,
      "GRILLED CHEESE/FRIES", 5.99, 0,
      "FILET MIGNON", 21.99, 0,
      "CLUB HOUSE SANDWICH", 10.99, 0,
      "B.L.T. SANDWICH", 6.99, 0,
      "VEGETARIAN CHOW MEIN", 8.95, 0,
      "ATLANTIC SALMON", 16.45, 0,
      "CHEDDAR CHEESE BURGER", 9.57, 0,
      "GRAIN FED VEAL", 25.00, 0,
      "POP", 1.65, 0,
      "Juice", 2.49, 0,
      "Coffe", 1.99, 0,
      "Shakes", 2.85, 0,
      "APPLE PIE", 3.55, 0,
      "Banana Sp", 4.99, 0
    };
    
    double total(double index)
    {
      double price = 0.00;
     
      price += 100 * index;
      return price;
    }
    
    void print_menu(int max)
    {
      int i;
      int c = 'A';
    
      printf("\n\t\t\t\tDAVE'S RESTAURANT\n\n");
      fprintf(stdout, "%-10s%-10s%-24s%-14s\n", 
              "Item", "Count", "ENTREES", "Price");
    
      for (i = 0; i < max; i++) {
        fprintf(stdout, "%-10c%-10d%-24s%-.2f\n",c + i,
                menu[i].count, menu[i].item, menu[i].price);
      }
    
      printf("\nSELECT (A-P), R-RESET, - CORRECT, T-TIP, S-DISCOUNT, X-EXIT: ");
      fflush(stdout);
    }
    
    void clear_data(int max)
    {
      int i;
    
      for (i = 0; i < max; i++) {
        menu[i].count = 0;
      }
    }
    
    int main(void)
    {
      /* I have no idea why the computers in the Particle Physics Lab 
         at school complain about these initializers.
       */
      int max = sizeof(menu)/sizeof(struct food);
      double sum = 0.00;
      float tippercent = 0.00;
      float discount = 0.00;
      int c;
    
      print_menu(max);
     
      while ((c = getchar()) != EOF) {
        switch(c)
          {
          case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
          case 'G': case 'H': case 'I': case 'J': case 'K': 
          case 'L': case 'M': case 'N': case 'O': case 'P':
            menu[c - 'A'].count++;
            sum += total(menu[c - 'A'].price); /*Do I apply PST and GST here? */
            break;
          case 'R':
            sum = 0.00;
            tippercent = 0.00;
            discount = 0.00;
            clear_data(max);
            break;
          case 'S':
            printf ("ENTER DISCOUNT ");
            fflush(stdout);
            scanf ("%f", &discount);
            break;
          case 'T':
            printf ("ENTER TIP PERCENTAGE ");
            fflush(stdout);
            scanf ("%f", &tippercent);
            break;
          case 'X': case 'x': 
            sum = sum + (sum * (tippercent/100));
            printf("**The final total is: %.2f\n", (sum/100) - (discount/100));
            exit(1);
          default:
            print_menu(max);
            break;
          }
      }
      
      exit(0);
    }

  14. #14
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    ^^^

    Hey,
    Great solution, kinda similar to mine in regards to the array of structures, I'm at school now but i'll post what I have so far when I get home! From what I can tell you definatly figured out the totals correctly (which is something I still havn't got working properly on mine), and they don't appear to be off at all as far as i can tell...

    In regards to the GST and PST, i think it can just simply be entered like this for the calculation to be correct...

    Code:
     case 'X': case 'x': 
            sum = sum + (sum * (tippercent/100));
            printf("**The final total is: %.2f\n", ((sum/100)*1.12) - (discount/100));
    		printf("PRESS ANY KEY TO EXIT");
    		getch();
    The one major difference with my code is that i'm trying to have the menu stay constant the entire time, for example, if someone enters "A", it doesn't jump to a new screen before another order can be entered, the counter just updates....im doing it with a while loop right now, but its extremely buggy :S
    Last edited by matt.s; 04-16-2010 at 01:06 PM.

  15. #15
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    I'm not that sure if the following piece of code falls within the realm of "C" or "Making your terminal perform stupid sheep tricks".

    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int c;
    
      /*Just enter one letter and then press the return key*/
      while ( (c = getchar()) != EOF) {
        printf("%c[%d;%dH",27,2,300); 
      }
      return 0; 
    }
    You should just see the cursor positioned near the upper right hand corner of the terminal if it works. Each subsequent key press will keep the cursor in the same position.
    Last edited by Overworked_PhD; 04-16-2010 at 05:39 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fixed point / floating point
    By confuted in forum Game Programming
    Replies: 4
    Last Post: 08-13-2002, 01:25 PM
  2. Please point to some really simple 3D tutorials
    By Tazar in forum Game Programming
    Replies: 6
    Last Post: 02-08-2002, 12:34 AM
  3. point lies in circle?
    By cozman in forum Game Programming
    Replies: 3
    Last Post: 12-20-2001, 04:39 PM
  4. more with my 1st structure program
    By sballew in forum C Programming
    Replies: 42
    Last Post: 10-22-2001, 08:03 PM