Grocery Store Program Help

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 4244 Views - Last Post: 18 November 2010 - 06:35 PM Rate Topic: -----

#1 Sushii  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 11-December 08

Grocery Store Program Help

Posted 17 November 2010 - 09:26 PM

Hi,

I have almost everything done except a few things.

1. I can't seem to figure out how to RESET the program so that all values go back to default
2. Program always makes a beep noise after entering number (pounds) even though i'm telling it to break;
3. How do i stop a value from dividing by 0 at the start of the program (float avg)

I AM NOT ASKING FOR THE CODE, I want to try this on my own. So just tips.

Here is my code

/*		PROGRAMMERS BLOCK

Author: XXXXXXXX
Student ID: 1234567890
Date: November 16th 2010
Description: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX




TO DO's

- STOP THE BEEPING NOISE AFTER EACH INPUT
- FIX AVG (Beginning = UNDEFINED)
- MAKE A RESET LOOP IF R/r is ENTER








*/

// INCLUDE HEADERS
#include <stdio.h>      // used for printf, scanf, getchar, puts
#include <stdlib.h>     // used for system

/ DEFINE SECTION
#define		A			"Smarties " //Item A
#define		AP			"4.55"		//Price of Item A per pound
#define		B			"Flour "	//Item B
#define		BP			"1.5"		//Price of Item B per pound
#define		C			"Soup "		//Item C
#define		CP			"2.88"		//Price of Item C per pound
#define		D			"Walnuts "	//Item D
#define		DP			"5.25"		//Price of Item D per pound
#define		E			"Rice "		//Item E
#define		EP			"1.05"		//Price of Item E per pound
#define		F			"Noodles "	//Item F
#define		FP			"0.35"		//Price of Item F per pound
#define		G			"Sugar "	//Item G
#define		GP			"0.92"		//Price of Item G per pound
#define		H			"CakeMix "	//Item H
#define		HP			"1.45"		//Price of Item H per pound

#define		A_P			4.55		//Price of Item A per pound constants
#define		B_P			1.5			//Price of Item B per pound constants
#define		C_P			2.88		//Price of Item C per pound constants
#define		D_P			5.25		//Price of Item D per pound constants
#define		E_P			1.05		//Price of Item E per pound constants
#define		F_P			0.35		//Price of Item F per pound constants
#define		G_P			0.92		//Price of Item G per pound constants
#define		H_P			1.45		//Price of Item H per pound constants

#define		HST			0.13

// FUNCTION PROTOTYPES
void main_menu();					//Displays the menu and does all the calculations and then displays results

int main()

	{
		system("cls");

		main_menu();				//Calls the function main_menu

		// Wait for any key to be pressed
		puts("");
		puts("");
		system("pause");

		return 0;

	}

void main_menu()
{
		float pa=0.00,pb=0.00,pc=0.00,pd=0.00,pe=0.00,pf=0.00,pg=0.00,ph=0.00;								// pa,b,c,d,e,f,g,h pounds of item at the beginning of the program
		char choice;																						// Variable to store the choice entered by the user
		do{																									// do while loop
		float sta=A_P*pa,stb=B_P*pb,stc=C_P*pc,std=D_P*pd,ste=E_P*pe,stf=F_P*pf,stg=G_P*pg,sth=H_P*ph;		// sta,b,c,d,e,f,g,h = Subtotal of a,b,c,d,e,f,g,h
		float stt=sta+stb+stc+std+ste+stf+stg+sth;															// stt = Total of all subtotals
		float totalpounds = pa+pb+pc+pd+pe+pf+pg+ph;														// Total pounds
		float avg=sta/totalpounds;																			// Average cost/lb
		float hst= stt*HST;																					// HST Tax 13%
		float totalcost=stt+hst;																			// Total Amount
		puts("\t\tBULK FOOD STORE PROGRAM");
		puts("");
		puts("Item\tDescription\tCost/lb\t#Pounds\tSubtotal");
		puts("");
		printf("\nA\t"A"\t"AP"\t%.2f\t$ %.2f", pa, sta);
		printf("\nB\t"B"\t\t"BP"\t%.2f\t$ %.2f", pb, stb);
		printf("\nC\t"C"\t\t"CP"\t%.2f\t$ %.2f", pc, stc);
		printf("\nD\t"D"\t"DP"\t%.2f\t$ %.2f", pd, std);
		printf("\nE\t"E"\t\t"EP"\t%.2f\t$ %.2f", pe, ste);
		printf("\nF\t"F"\t"FP"\t%.2f\t$ %.2f", pf, stf);
		printf("\nG\t"G"\t\t"GP"\t%.2f\t$ %.2f", pg, stg);
		printf("\nH\t"H"\t"HP"\t%.2f\t$ %.2f", ph, sth);
		puts("");
		printf("\nTotal Pounds = %.2f", totalpounds);
		printf("\t  SUBTOTAL      $ %.2f", stt);
		printf("\nAvg Cost/lb  = %.2f", avg); 
		printf("\t  HST Amount    $ %.2f", hst);
		puts("");
		printf("\n\t\t\t  TOTAL Amount  $ %.2f", totalcost);
		printf("\n\n     Your choice (R to RESET, Q to quit) ==> ");
		scanf("%c", &choice);

		if (choice == 'q' || choice == 'Q')																	// If q/Q is entered as the choice, the program will quit
		{
			break;
		}
		else if (choice == 'r' || choice == 'R')															// If r/R is entered as the choice, the program will reset all values
		{
			//RESET THE CODE
		}
		else
		{
		switch (choice)																						// switch for choices a-h and A-H
		{
			case 'a':
			case 'A':
				printf("\nHow many pounds of "A"would you like? ");
				scanf("%f", &pa);
				break;
			case 'b':
			case 'B':
				printf("\nHow many pounds of "B"would you like? ");
				scanf("%f", &pb);
				break;
			case 'c':
			case 'C':
				printf("\nHow many pounds of "C"would you like? ");
				scanf("%f", &pc);
				break;
			case 'd':
			case 'D':
				printf("\nHow many pounds of "D"would you like? ");
				scanf("%f", &pd);
				break;
			case 'e':
			case 'E':
				printf("\nHow many pounds of "E"would you like? ");
				scanf("%f", &pe);
				break;
			case 'f':
			case 'F':
				printf("\nHow many pounds of "F"would you like? ");
				scanf("%f", &pf);
				break;
			case 'g':
			case 'G':
				printf("\nHow many pounds of "G"would you like? ");
				scanf("%f", &pg);
				break;
			case 'h':
			case 'H':
				printf("\nHow many pounds of "H"would you like? ");
				scanf("%f", &ph);
				break;
			default:
				puts("\a");
				break;
		}
		}
		}
		 while (1);																								// Infinite loop until program quits (choice == 'q' || choice == 'Q')

}



Thanks!

This post has been edited by Sushii: 17 November 2010 - 09:30 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Grocery Store Program Help

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Grocery Store Program Help

Posted 17 November 2010 - 09:37 PM

Kudos for a good description of what you want help with. Better than most people posting here.

Can we just expand a little more on this one
"1. I can't seem to figure out how to RESET the program so that all values go back to default"

Do you want the values reset to default at the top of the do-while() loop in the main_menu() function?
It seems, at a quick glance, you have code there to do that. Is it not working as you want? Or am I mis-reading your code?

Or are you looking to reset the defaults somewhere else in the program?

Just want to focus the energy in the right problem and not start telling you things you don't care about.

EDIT
Fat fingers of the worst kind.

This post has been edited by janotte: 17 November 2010 - 09:38 PM

Was This Post Helpful? 0
  • +
  • -

#3 Sushii  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 11-December 08

Re: Grocery Store Program Help

Posted 17 November 2010 - 10:02 PM

View Postjanotte, on 17 November 2010 - 08:37 PM, said:

Can we just expand a little more on this one
"1. I can't seem to figure out how to RESET the program so that all values go back to default"

Do you want the values reset to default at the top of the do-while() loop in the main_menu() function?
It seems, at a quick glance, you have code there to do that. Is it not working as you want? Or am I mis-reading your code?

Or are you looking to reset the defaults somewhere else in the program?


For example if you entered 'A' as the choice and '5' as the pounds. pa would now be pa=5; When i try to reset by entering 'R' the program would still keep pa as 5 rather than resetting back to default (0.00).

The reset basically changes ALL the values of pounds back to the default which is 0.00.

I was thinking of using another do-while loop outside the first do-while loop and putting the while condition to choice == r but i keep getting an error (i'm assuming its because it has to be a value rather than a character)

P.S
Don't know if i was clear enough but another way of putting it is basically quitting the program and re-running it. (Thus resetting all values back to default)



Problem 2 resides on line 121-166
Problem 3 resides on line 86 and 104

This post has been edited by Sushii: 17 November 2010 - 10:07 PM

Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Grocery Store Program Help

Posted 17 November 2010 - 10:11 PM

Ah ha - not what I thought you were asking. Glad I asked.

If I have it right now then all you would need to do is copy the relevant bits of line 81 to line 117.
Give that a try. Does it achieve what you were trying to do?
Was This Post Helpful? 0
  • +
  • -

#5 Sushii  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 11-December 08

Re: Grocery Store Program Help

Posted 17 November 2010 - 10:28 PM

View Postjanotte, on 17 November 2010 - 09:11 PM, said:

Ah ha - not what I thought you were asking. Glad I asked.

If I have it right now then all you would need to do is copy the relevant bits of line 81 to line 117.
Give that a try. Does it achieve what you were trying to do?


EDIT: Found the problem. removed float and everything works.

Thanks! Now im still trying to figure out problem's 2 and 3.

This post has been edited by Sushii: 17 November 2010 - 10:35 PM

Was This Post Helpful? 0
  • +
  • -

#6 Sushii  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 11-December 08

Re: Grocery Store Program Help

Posted 17 November 2010 - 10:38 PM

Is there a value for undefined?

e.g

if (avg == undefined)
{
avg = 0;
}
REST OF PROGRAM

Found out undefined = NULL (I think?) tried adding it but it still doesn't work.

I can't seem to understand why it beeps. I have checked the code over and over again. Whenever a choice is selected e.g B, it should break out of the switch but it doesn't. Odd...

This post has been edited by Sushii: 17 November 2010 - 10:55 PM

Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: Grocery Store Program Help

Posted 18 November 2010 - 04:44 AM

You have leftover newline(s) in the input buffer. After the input and the loop starts again, the switch getting that newline from choice, which is going through the default branch of the switch/case statement.

See my blog entry for an explanation and fix.

This post has been edited by JackOfAllTrades: 18 November 2010 - 04:45 AM

Was This Post Helpful? 0
  • +
  • -

#8 Sushii  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 11-December 08

Re: Grocery Store Program Help

Posted 18 November 2010 - 06:12 AM

View PostJackOfAllTrades, on 18 November 2010 - 03:44 AM, said:

You have leftover newline(s) in the input buffer. After the input and the loop starts again, the switch getting that newline from choice, which is going through the default branch of the switch/case statement.

See my blog entry for an explanation and fix.



Thanks for the hint! I fixed it by putting fflush(stdin); after the switch case which flushes the keyboard buffer.

Now one more problem left.

If anyone has any tips on how to make a undefined value = zero please feel free to suggest.

This post has been edited by Sushii: 18 November 2010 - 06:15 AM

Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: Grocery Store Program Help

Posted 18 November 2010 - 06:35 AM

No. No, no, NO! That is NOT the right solution. fflush is ONLY DEFINED FOR OUTPUT STREAMS. Using fflush(stdin) invokes undefined behavior.

Link
Link
Link

The blog entry to which I linked gave you the RIGHT answer to flushing the stdin buffer! So glad I bothered :rolleyes:

// sta,b,c,d,e,f,g,h = Subtotal of a,b,c,d,e,f,g,h
float sta=A_P*pa,stb=B_P*pb,stc=C_P*pc,std=D_P*pd,ste=E_P*pe,stf=F_P*pf,stg=G_P*pg,sth=H_P*ph;

// stt = Total of all subtotals		
float stt=sta+stb+stc+std+ste+stf+stg+sth;					 
float totalpounds = pa+pb+pc+pd+pe+pf+pg+ph;							
		
float avg=sta/totalpounds;																		// Average cost/lb
float hst= stt*HST;																			// HST Tax 13%
float totalcost=stt+hst;			



That's just ALL WRONG. You seem to expect the definition of these variables like this to just automatically do your calculations for the rest of your program. That's NOT how it works.
Was This Post Helpful? 0
  • +
  • -

#10 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Grocery Store Program Help

Posted 18 November 2010 - 06:36 AM

View PostSushii, on 18 November 2010 - 10:12 PM, said:

I fixed it by putting fflush(stdin); after the switch case which flushes the keyboard buffer.


If it is working then it is okay for now but put this in your reading list for later
http://www.gidnetwork.com/b-57.html
fflush() is not everything you might think it is.
You should not use it as you have here going forward.


View PostSushii, on 18 November 2010 - 10:12 PM, said:

If anyone has any tips on how to make a undefined value = zero please feel free to suggest.

I don't understand what you are asking there.

Earlier you were asking about a float value. It is not possible for that to be 'undefined'. As soon as you declare the variable and that means some memory is allocated and the variable has whatever is in that location. It might be zero, it might be any garbage value.

Please explain at greater length what the problem is here so we can help you better.

EDIT
Ninja'D in comprehensive fashion :)

This post has been edited by janotte: 18 November 2010 - 06:38 AM

Was This Post Helpful? 0
  • +
  • -

#11 Sushii  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 11-December 08

Re: Grocery Store Program Help

Posted 18 November 2010 - 06:42 AM

View Postjanotte, on 18 November 2010 - 05:36 AM, said:

View PostSushii, on 18 November 2010 - 10:12 PM, said:

I fixed it by putting fflush(stdin); after the switch case which flushes the keyboard buffer.


If it is working then it is okay for now but put this in your reading list for later
http://www.gidnetwork.com/b-57.html
fflush() is not everything you might think it is.
You should not use it as you have here going forward.


View PostSushii, on 18 November 2010 - 10:12 PM, said:

If anyone has any tips on how to make a undefined value = zero please feel free to suggest.

I don't understand what you are asking there.

Earlier you were asking about a float value. It is not possible for that to be 'undefined'. As soon as you declare the variable and that means some memory is allocated and the variable has whatever is in that location. It might be zero, it might be any garbage value.

Please explain at greater length what the problem is here so we can help you better.

EDIT
Ninja'D in comprehensive fashion :)


Thanks for another wonderful tip. I'm in first year so the only thing i know is scanf(); but i'll keep it in mind and see if my profs teach me some other methods.

Basically on line 87 you see
float avg=stt/totalpounds;		



Sine the program is running for the first time, STT = 0 and totalpounds = 0

then avg = 0/0 which is undefined.

Here is the result i get on screen

Posted Image


Here is what i've tried so far.

if (avg != 0 && avg <= 0)
{
avg = 0.00;
}



and some other variations of if statements

This post has been edited by Sushii: 18 November 2010 - 06:46 AM

Was This Post Helpful? 0
  • +
  • -

#12 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Grocery Store Program Help

Posted 18 November 2010 - 06:58 AM

As JackOfAllTrades mentioned above there is a fundamental mistake in how you are doing this.

Your program always runs from top to bottom.
So there is no logical reason for you to be doing the calculation at line 87.

You shouldn't be doing that calculation until you have values to do the maths on.

So step one.
Line 87 and probably a lot of other stuff up there at the top of the loop should be moved so it is after you get input from the user.

Step two.
There is no built in language feature to protect against divide by zero problems.
You have to write your own code to shield against that problem.
If you think there is a chance that you could have a divide by zero occur then you need to wrap the dangerous calculation in an if() test and if the risky value is, in fact, zero then don't do the calculation.

Anyway, the important problem is the step one stuff.
I think you have fundamentally mis-designed your program and there is stuff that is trying to happen before it should happen.
You need to wait to do calculations until you have the data to fill the calculation.
Scroll back up and read the post from JackOfAllTrades before starting to work on your code.
Was This Post Helpful? 0
  • +
  • -

#13 Sushii  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 11-December 08

Re: Grocery Store Program Help

Posted 18 November 2010 - 07:13 AM

View Postjanotte, on 18 November 2010 - 05:58 AM, said:

As JackOfAllTrades mentioned above there is a fundamental mistake in how you are doing this.

Your program always runs from top to bottom.
So there is no logical reason for you to be doing the calculation at line 87.

You shouldn't be doing that calculation until you have values to do the maths on.

So step one.
Line 87 and probably a lot of other stuff up there at the top of the loop should be moved so it is after you get input from the user.

Step two.
There is no built in language feature to protect against divide by zero problems.
You have to write your own code to shield against that problem.
If you think there is a chance that you could have a divide by zero occur then you need to wrap the dangerous calculation in an if() test and if the risky value is, in fact, zero then don't do the calculation.

Anyway, the important problem is the step one stuff.
I think you have fundamentally mis-designed your program and there is stuff that is trying to happen before it should happen.
You need to wait to do calculations until you have the data to fill the calculation.
Scroll back up and read the post from JackOfAllTrades before starting to work on your code.


Fixed it by using a if statement

float totalpounds = pa+pb+pc+pd+pe+pf+pg+ph;
float avg;
								
			float avg;
			if (totalpounds == 0)
			{
				avg = 0;
			}
			else
			{
				avg=stt/totalpounds;																			// Average cost/lb
			}



I don't know if this was a correct way to do it but it fixes the problem.

This post has been edited by Sushii: 18 November 2010 - 07:14 AM

Was This Post Helpful? 0
  • +
  • -

#14 Sushii  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 11-December 08

Re: Grocery Store Program Help

Posted 18 November 2010 - 06:07 PM

After fixing all the problems i noticed one more problem arised.


Problem: I just noticed that pa,b,c,d,e,f,g,h (number of pounds user wants) can be a negative value. What is the best way to go about making it positive values only?

I'm guessing i'll have to use a if and else statement but i don't know where to put it. I guess you can also use a while loop until the entered value is positive.

Thanks
Was This Post Helpful? 0
  • +
  • -

#15 Sushii  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 11-December 08

Re: Grocery Store Program Help

Posted 18 November 2010 - 06:21 PM

Okay here is what i found out so far


If i use a do while loop around the switch (choice) it will repeat it until a positive answer is entered


e.g

do
				{
				switch (choice)																						// switch for choices a-h and A-H
				{
					case 'a':
					case 'A':
						printf("\nHow many pounds of "A"would you like? ");
						scanf("%f", &pa);
						break;
					case 'b':
					case 'B':
						printf("\nHow many pounds of "B"would you like? ");
						scanf("%f", &pb);
						break;
					case 'c':
					case 'C':
						printf("\nHow many pounds of "C"would you like? ");
						scanf("%f", &pc);
						break;
					case 'd':
					case 'D':
						printf("\nHow many pounds of "D"would you like? ");
						scanf("%f", &pd);
						break;
					case 'e':
					case 'E':
						printf("\nHow many pounds of "E"would you like? ");
						scanf("%f", &pe);
						break;
					case 'f':
					case 'F':
						printf("\nHow many pounds of "F"would you like? ");
						scanf("%f", &pf);
						break;
					case 'g':
					case 'G':
						printf("\nHow many pounds of "G"would you like? ");
						scanf("%f", &pg);
						break;
					case 'h':
					case 'H':
						printf("\nHow many pounds of "H"would you like? ");
						scanf("%f", &ph);
						break;
					default:
						puts("\a");
						break;
				}
				}while (pa < 0 || pb < 0 || pc < 0 || pd < 0 || pe < 0 || pf < 0 || pg < 0 || ph < 0);



This works but how do i make it so that it displays, "You have entered a negative value, please enter a positive value"

Okay here is the final code

do
				{
				switch (choice)																						// switch for choices a-h and A-H
				{
					case 'a':
					case 'A':
						printf("\nHow many pounds of "A"would you like? ");
						scanf("%f", &pa);
						break;
					case 'b':
					case 'B':
						printf("\nHow many pounds of "B"would you like? ");
						scanf("%f", &pb);
						break;
					case 'c':
					case 'C':
						printf("\nHow many pounds of "C"would you like? ");
						scanf("%f", &pc);
						break;
					case 'd':
					case 'D':
						printf("\nHow many pounds of "D"would you like? ");
						scanf("%f", &pd);
						break;
					case 'e':
					case 'E':
						printf("\nHow many pounds of "E"would you like? ");
						scanf("%f", &pe);
						break;
					case 'f':
					case 'F':
						printf("\nHow many pounds of "F"would you like? ");
						scanf("%f", &pf);
						break;
					case 'g':
					case 'G':
						printf("\nHow many pounds of "G"would you like? ");
						scanf("%f", &pg);
						break;
					case 'h':
					case 'H':
						printf("\nHow many pounds of "H"would you like? ");
						scanf("%f", &ph);
						break;
					default:
						puts("\a");
						break;
				}
				if (pa < 0 || pb < 0 || pc < 0 || pd < 0 || pe < 0 || pf < 0 || pg < 0 || ph < 0)
				{
					printf("\nPounds must be a POSITIVE number, Please try again\n");
					printf("\a");
				}
				}while (pa < 0 || pb < 0 || pc < 0 || pd < 0 || pe < 0 || pf < 0 || pg < 0 || ph < 0);



It works fine but is there an easier way of doing this?

Also a quick question, The program accepts -0 as pound and displays it as -0 on the menu. How do i fix this?

This post has been edited by Sushii: 18 November 2010 - 06:25 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2