![]() |
| | #1 |
| Registered User Join Date: Oct 2005
Posts: 53
| Change this program so it uses function?? you are to implement the cash register program using multiple functions. this is not homework, if possible ask an experienced class mate or relative to demonstrate these to you before class on tuesday. using the following functions: • Implement a main function that inputs the number of grocery items and the amount paid by the user. • Implement a function that inputs the price of each individual grocery item. This function should accept the number of grocery items as an input argument. In addition this function should return the total cost of the items. • Implement a function for computing the number of quarters that are returned. This function must accept the amount of change returned as an input argument. It must also accept the number of quarters remaining in the bank as another argument. Finally, this function should return the number of quarters that can be returned. • Implement functions for computing the number of dimes, nickels, and pennies that are similar to the previous function. By similar, I mean that they must accept similar types of arguments and return similar types of values as the above function. The exception to this is the “pennies” function which is explained in the next point. • The “pennies” function should also be able to indicate if it is possible to return the exact amount to the user (new requirement). If there are not enough pennies in the bank to cover the amount of change needed to be returned to the user, your program should display an error message. The pennies function can indicate this situation by returning a -1 back to its calling function. • Implement a function whose sole purpose is to display output to the user. It should accept four arguments which are the numbers of quarters, dimes, nickels, and pennies, respectively. It should output the information to the screen. This function should not return any value back to the user. Code: #include <stdio.h>
#include <stdlib.h>
#include <math.h> // Needed for modff
#include <conio.h> // Needed for getche
#define FALSE 0
#define TRUE !FALSE
#define DEBUG
int bDenominationCalculation(unsigned long ulCentsLeft);
int main(void)
{
char ch;
int numItems = 0;
float total = 0.0;
float price = 0.0, amtPaid = 0.0, change = 0.0;
unsigned long centsLeft = 0; // cents left to pay in change
float cents;
do
{
printf("Please enter the number of grocery items>");
scanf("%d", &numItems);
while (numItems != 0)
{
printf("\nPlease enter the price for your item>");
scanf("%f", &price);
total += price;
numItems--;
}
printf("\nPlease enter the amount paid>");
scanf("%f", &amtPaid);
change = amtPaid - total;
printf("\nchange is %0.02f\n", change);
cents = change; // Separate Dollars and cents
centsLeft = (cents * 100);
printf(" cents = %0.02f centsLeft = %d\n", cents*100, centsLeft);
if( change > 0.0 ) // optional denomination breakdown
{
if(bDenominationCalculation(centsLeft))
{
printf("program ending, change cannot be returned\n");
break;
}
}
printf("Hit the 0 key to e(X)it, any other key to repeat procedure ");
ch=toupper(getche());
printf("\n");
if (ch=='0') {
printf("Program finished");
break;
}
total = 0.0;
}
while (TRUE);
return 0;
}
int bDenominationCalculation(unsigned long ulCentsLeft)
{
unsigned long uliTempCalc = 0L;
static struct SBank
{
unsigned long uliQuarter;
unsigned long uliDime;
unsigned long uliNickel;
unsigned long uliPenny;
}SBankDenom = {10, 10, 10, 10};
if( SBankDenom.uliQuarter && (ulCentsLeft / 25))
{
if(SBankDenom.uliQuarter >= ulCentsLeft / 25)
{
printf("25%c - %u\n", 155, ulCentsLeft / 25);
SBankDenom.uliQuarter -= ulCentsLeft / 25;
ulCentsLeft -= 25 * ( ulCentsLeft / 25 );
}
else
{
ulCentsLeft -= (SBankDenom.uliQuarter * 25 );
printf("25%c - %u\n", 155,SBankDenom.uliQuarter );
SBankDenom.uliQuarter = 0;
}
}
if( SBankDenom.uliDime && (ulCentsLeft / 10) )
{
if(SBankDenom.uliDime >= ulCentsLeft / 10)
{
printf("10%c - %u\n", 155, ulCentsLeft / 10);
SBankDenom.uliDime -= ulCentsLeft/ 10;
ulCentsLeft -= 10 * ( ulCentsLeft / 10 );
}
else
{
ulCentsLeft -= (SBankDenom.uliDime * 10 );
printf("10%c - %u\n", 155,SBankDenom.uliDime );
SBankDenom.uliDime = 0;
}
}
if( SBankDenom.uliNickel && (ulCentsLeft / 5) )
{
if(SBankDenom.uliNickel >= ulCentsLeft / 5)
{
printf("5%c - %u\n", 155, ulCentsLeft / 5);
SBankDenom.uliNickel -= ulCentsLeft/ 5;
ulCentsLeft -= 5 * ( ulCentsLeft / 5 );
}
else
{
ulCentsLeft -= (SBankDenom.uliNickel * 5 );
printf("5%c - %u\n", 155, SBankDenom.uliNickel);
SBankDenom.uliNickel = 0;
}
}
if( ulCentsLeft && SBankDenom.uliPenny )
{
if(SBankDenom.uliPenny >= ulCentsLeft )
{
printf("1%c - %u\n", 155, ulCentsLeft);
SBankDenom.uliPenny -= ulCentsLeft;
ulCentsLeft -= ulCentsLeft ;
}
else
{
ulCentsLeft -= (SBankDenom.uliPenny );
printf("1%c - %u\n", 155,SBankDenom.uliPenny );
SBankDenom.uliPenny = 0;
}
}
#ifdef DEBUG
printf("Static struct variables:\n");
printf("Q %u\n",SBankDenom.uliQuarter);
printf("D %u\n",SBankDenom.uliDime);
printf("N %u\n",SBankDenom.uliNickel);
printf("P %u\n",SBankDenom.uliPenny);
#endif
if(ulCentsLeft)
return TRUE;
else
return FALSE;
}
|
| stormfront is offline | |
| | #2 | |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,629
| You need to include <ctype.h> for toupper(). What are you having problems with? Do you need to change that program above so that it uses functions? Quote:
Code: void print_results(int quarters, int dimes, int nickels, int pennies);
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, etc. New project: nort | |
| dwks is offline | |
| | #3 | |
| Registered User Join Date: Oct 2005
Posts: 53
| Quote:
yes, i need to change the above program so that it uses those function, and i really dont know how to use a function at all. | |
| stormfront is offline | |
| | #4 |
| Registered User Join Date: Mar 2005 Location: Mountaintop, Pa
Posts: 1,054
| Here's my solution to your problem based on my interpretation of your specs listed above. I haven't tested it much. So, it's your responsiblity to find the bugs and verify it meets your specs. Have fun Bob Code: #include <stdio.h>
#include <stdlib.h>
#include <math.h> // Needed for modff
#include <conio.h> // Needed for getche
#define FALSE 0
#define TRUE !FALSE
#define DEBUG
#define QUARTERS 25
#define DIMES 10
#define NICKELS 5
#define PENNIES 1
#define BADPENNY -1
#define BOOL int
typedef struct SBankReturn
{
unsigned long ulBankQuarter;
unsigned long ulBankDime;
unsigned long ulBankNickel;
unsigned long ulBankPenny;
BOOL bBankBroke;
} SBankFundsInfo;
SBankFundsInfo SBankDenom = {10,10,10, 10, FALSE };
unsigned long ulCentsLeft = 0L; // cents left to pay in change
float fGetTotalCost(int iTotalItems)
{
float fTotal = 0.0;
float fPrice = 0.0;
while (iTotalItems != 0)
{
printf("\nPlease enter the price for your item>");
scanf("%f", &fPrice);
fTotal += fPrice;
iTotalItems--;
}
return fTotal;
}
BOOL bUpdateBank(unsigned long ulDenominationCount, int iDenomination)
{
if(iDenomination == QUARTERS )
{
if(SBankDenom.ulBankQuarter >= ulDenominationCount)
{
SBankDenom.ulBankQuarter -= ulDenominationCount;
ulCentsLeft -= ulDenominationCount * QUARTERS ;
}
else
{
ulCentsLeft -= (SBankDenom.ulBankQuarter * QUARTERS );
SBankDenom.ulBankQuarter = 0L;
}
}
if(iDenomination == DIMES )
{
if(SBankDenom.ulBankDime >= ulDenominationCount)
{
SBankDenom.ulBankDime -= ulDenominationCount;
ulCentsLeft -= ulDenominationCount * DIMES ;
}
else
{
ulCentsLeft -= (SBankDenom.ulBankDime * DIMES );
SBankDenom.ulBankDime = 0L;
}
}
if(iDenomination == NICKELS )
{
if(SBankDenom.ulBankNickel >= ulDenominationCount)
{
SBankDenom.ulBankNickel -= ulDenominationCount;
ulCentsLeft -= ulDenominationCount * NICKELS;
}
else
{
ulCentsLeft -= (SBankDenom.ulBankNickel * NICKELS );
SBankDenom.ulBankNickel = 0L;
}
}
if(iDenomination == PENNIES )
{
if(SBankDenom.ulBankPenny >= ulDenominationCount)
{
SBankDenom.ulBankPenny -= ulDenominationCount;
ulCentsLeft = 0L;
}
else
{
ulCentsLeft -= SBankDenom.ulBankPenny;
SBankDenom.ulBankPenny = 0L;
}
}
#ifdef DEBUG
printf("Static struct variables:\n");
printf("Q %u\n",SBankDenom.ulBankQuarter);
printf("D %u\n",SBankDenom.ulBankDime);
printf("N %u\n",SBankDenom.ulBankNickel);
printf("P %u\n",SBankDenom.ulBankPenny);
#endif
if( !SBankDenom.ulBankQuarter && !SBankDenom.ulBankDime && !SBankDenom.ulBankNickel && !SBankDenom.ulBankPenny)
{
SBankDenom. bBankBroke = TRUE;
return TRUE;
}
else
return FALSE;
}
int CalulateQuarters(unsigned long ulCentsLeft, unsigned long ulQuartersInBank)
{
int iQuartersReturned = 0;
if(ulQuartersInBank >= (ulCentsLeft / QUARTERS))
iQuartersReturned = (ulCentsLeft / QUARTERS);
else
iQuartersReturned = ulQuartersInBank;
bUpdateBank(iQuartersReturned, QUARTERS);
return iQuartersReturned;
}
int CalulateDimes(unsigned long ulCentsLeft, unsigned long ulDimesInBank)
{
int iDimesReturned = 0;
if(ulDimesInBank >= (ulCentsLeft / DIMES))
iDimesReturned = (ulCentsLeft / DIMES);
else
iDimesReturned = ulDimesInBank;
bUpdateBank(iDimesReturned, DIMES);
return iDimesReturned;
}
int CalulateNickels(unsigned long ulCentsLeft, unsigned long ulNickelsInBank)
{
int iNickelsReturned = 0;
if(ulNickelsInBank >= (ulCentsLeft / NICKELS))
iNickelsReturned = (ulCentsLeft / NICKELS);
else
iNickelsReturned = ulNickelsInBank;
bUpdateBank(iNickelsReturned, NICKELS);
return iNickelsReturned;
}
int CalulatePennies(unsigned long ulCentsLeft, unsigned long ulPenniesInBank)
{
BOOL bNotEnoughPennies = FALSE;
int iPenniesReturned = 0;
if(ulPenniesInBank >= (ulCentsLeft / PENNIES))
iPenniesReturned = (ulCentsLeft / PENNIES);
else
{
bNotEnoughPennies = TRUE;
iPenniesReturned = ulPenniesInBank;
}
bUpdateBank(iPenniesReturned, PENNIES);
if(bNotEnoughPennies == FALSE)
return iPenniesReturned;
else
return BADPENNY;
}
void DisplayResults(unsigned long ulQuarters, unsigned long ulDimes, unsigned long ulNickles, unsigned long ulPennies)
{
printf("\nOur change is as follows:\n");
if(ulPennies != BADPENNY)
printf("%ld Quarters, %ld Dimes, %ld Nickels, %ld Pennies\n\n\n",ulQuarters, ulDimes, ulNickles, ulPennies);
else
printf("%ld Quarters, %ld Dimes, %ld Nickels, *** Pennies returned a -1 ***\n\n\n",ulQuarters, ulDimes, ulNickles);
}
int main(void)
{
unsigned long ulQuartersReturned, ulDimesReturned, ulNicklesReturned, ulPenniesReturned;
int iNumItems = 0;
float fTotal = 0.0;
float fAmtPaid = 0.0, fChange = 0.0;
float fDollars, fCents;
do
{
printf("Please enter the number of grocery items>");
scanf("%d", &iNumItems);
fTotal = fGetTotalCost(iNumItems);
printf("\nPlease enter the amount paid>");
scanf("%f", &fAmtPaid);
fChange = fAmtPaid - fTotal;
printf("\nchange is %0.02f\n", fChange);
fCents = modff( fChange, &fDollars); // Separate Dollars and cents
ulCentsLeft = (fDollars * 100) + (fCents * 100);
printf("dollars = %0.02f cents = %0.02f centsLeft = %ld\n", fDollars, fCents*100, ulCentsLeft);
if( fChange > 0.0 )
{
ulQuartersReturned = CalulateQuarters(ulCentsLeft,SBankDenom.ulBankQuarter );
ulDimesReturned = CalulateDimes(ulCentsLeft,SBankDenom.ulBankDime );
ulNicklesReturned = CalulateNickels(ulCentsLeft,SBankDenom.ulBankNickel );
ulPenniesReturned = CalulatePennies(ulCentsLeft,SBankDenom.ulBankPenny );
DisplayResults(ulQuartersReturned,ulDimesReturned, ulNicklesReturned, ulPenniesReturned );
if(SBankDenom.bBankBroke)
{
printf("Program ending, the bank is broke\n");
break;
}
}
printf("Hit the X key to e(X)it, any other key to repeat procedure ");
if(toupper(getche()) == 'X')
{
printf("\nProgram finished\n");
break;
}
else printf("\n");
fChange = fCents = fDollars = fTotal = fAmtPaid = 0.0;
}
while (TRUE);
return 0;
}
|
| BobS0327 is offline | |
| | #5 |
| Registered User Join Date: Oct 2005
Posts: 53
| hey, i worked the few bugs out of it and got it to compile, but when it shows how much change is left in the bank it shows it 4 times and the 4th time it shows the change left it is correct but in the 3 before it is wrong, i cant figure out what is happening. heres the working code: with the change left in the bank repeating. Code: #include <stdio.h>
#include <stdlib.h>
#include <math.h> // Needed for modff
#include <conio.h> // Needed for getche
#define FALSE 0
#define TRUE !FALSE
#define DEBUG
#define QUARTERS 25
#define DIMES 10
#define NICKELS 5
#define PENNIES 1
#define BADPENNY -1
#define BOOL int
typedef struct SBankReturn
{
unsigned long ulBankQuarter;
unsigned long ulBankDime;
unsigned long ulBankNickel;
unsigned long ulBankPenny;
BOOL bBankBroke;
} SBankFundsInfo;
SBankFundsInfo SBankDenom = {10,10,10, 10, FALSE };
unsigned long ulCentsLeft = 0L; // cents left to pay in change
float fGetTotalCost(int iTotalItems)
{
float fTotal = 0.0;
float fPrice = 0.0;
while (iTotalItems != 0)
{
printf("\nPlease enter the price for your item>");
scanf("%f", &fPrice);
fTotal += fPrice;
iTotalItems--;
}
return fTotal;
}
BOOL bUpdateBank(unsigned long ulDenominationCount, int iDenomination)
{
if(iDenomination == QUARTERS )
{
if(SBankDenom.ulBankQuarter >= ulDenominationCount)
{
SBankDenom.ulBankQuarter -= ulDenominationCount;
ulCentsLeft -= ulDenominationCount * QUARTERS ;
}
else
{
ulCentsLeft -= (SBankDenom.ulBankQuarter * QUARTERS );
SBankDenom.ulBankQuarter = 0L;
}
}
if(iDenomination == DIMES )
{
if(SBankDenom.ulBankDime >= ulDenominationCount)
{
SBankDenom.ulBankDime -= ulDenominationCount;
ulCentsLeft -= ulDenominationCount * DIMES ;
}
else
{
ulCentsLeft -= (SBankDenom.ulBankDime * DIMES );
SBankDenom.ulBankDime = 0L;
}
}
if(iDenomination == NICKELS )
{
if(SBankDenom.ulBankNickel >= ulDenominationCount)
{
SBankDenom.ulBankNickel -= ulDenominationCount;
ulCentsLeft -= ulDenominationCount * NICKELS;
}
else
{
ulCentsLeft -= (SBankDenom.ulBankNickel * NICKELS );
SBankDenom.ulBankNickel = 0L;
}
}
if(iDenomination == PENNIES )
{
if(SBankDenom.ulBankPenny >= ulDenominationCount)
{
SBankDenom.ulBankPenny -= ulDenominationCount;
ulCentsLeft = 0L;
}
else
{
ulCentsLeft -= SBankDenom.ulBankPenny;
SBankDenom.ulBankPenny = 0L;
}
}
#ifdef DEBUG
printf("Static struct variables:\n");
printf("Q %u\n",SBankDenom.ulBankQuarter);
printf("D %u\n",SBankDenom.ulBankDime);
printf("N %u\n",SBankDenom.ulBankNickel);
printf("P %u\n",SBankDenom.ulBankPenny);
#endif
if( !SBankDenom.ulBankQuarter && !SBankDenom.ulBankDime && !SBankDenom.ulBankNickel && !SBankDenom.ulBankPenny)
{
SBankDenom. bBankBroke = TRUE;
return TRUE;
}
else
return FALSE;
}
int CalulateQuarters(unsigned long ulCentsLeft, unsigned long ulQuartersInBank)
{
int iQuartersReturned = 0;
if(ulQuartersInBank >= (ulCentsLeft / QUARTERS))
iQuartersReturned = (ulCentsLeft / QUARTERS);
else
iQuartersReturned = ulQuartersInBank;
bUpdateBank(iQuartersReturned, QUARTERS);
return iQuartersReturned;
}
int CalulateDimes(unsigned long ulCentsLeft, unsigned long ulDimesInBank)
{
int iDimesReturned = 0;
if(ulDimesInBank >= (ulCentsLeft / DIMES))
iDimesReturned = (ulCentsLeft / DIMES);
else
iDimesReturned = ulDimesInBank;
bUpdateBank(iDimesReturned, DIMES);
return iDimesReturned;
}
int CalulateNickels(unsigned long ulCentsLeft, unsigned long ulNickelsInBank)
{
int iNickelsReturned = 0;
if(ulNickelsInBank >= (ulCentsLeft / NICKELS))
iNickelsReturned = (ulCentsLeft / NICKELS);
else
iNickelsReturned = ulNickelsInBank;
bUpdateBank(iNickelsReturned, NICKELS);
return iNickelsReturned;
}
int CalulatePennies(unsigned long ulCentsLeft, unsigned long ulPenniesInBank)
{
BOOL bNotEnoughPennies = FALSE;
int iPenniesReturned = 0;
if(ulPenniesInBank >= (ulCentsLeft / PENNIES))
iPenniesReturned = (ulCentsLeft / PENNIES);
else
{
bNotEnoughPennies = TRUE;
iPenniesReturned = ulPenniesInBank;
}
bUpdateBank(iPenniesReturned, PENNIES);
if(bNotEnoughPennies == FALSE)
return iPenniesReturned;
else
return BADPENNY;
}
void DisplayResults(unsigned long ulQuarters, unsigned long ulDimes, unsigned long ulNickles, unsigned long ulPennies)
{
printf("\nOur change is as follows:\n");
if(ulPennies != BADPENNY)
printf("%ld Quarters, %ld Dimes, %ld Nickels, %ld Pennies\n\n\n",ulQuarters, ulDimes, ulNickles, ulPennies);
else
printf("%ld Quarters, %ld Dimes, %ld Nickels, *** Pennies returned a -1 ***\n\n\n",ulQuarters, ulDimes, ulNickles);
}
int main(void)
{
unsigned long ulQuartersReturned, ulDimesReturned, ulNicklesReturned, ulPenniesReturned;
int iNumItems = 0;
float fTotal = 0.0;
float fAmtPaid = 0.0, fChange = 0.0;
float fDollars, fCents;
do
{
printf("Please enter the number of grocery items>");
scanf("%d", &iNumItems);
fTotal = fGetTotalCost(iNumItems);
printf("\nPlease enter the amount paid>");
scanf("%f", &fAmtPaid);
fChange = fAmtPaid - fTotal;
printf("\nchange is %0.02f\n", fChange);
fCents = modff( fChange, &fDollars); // Separate Dollars and cents
ulCentsLeft = (fDollars * 100) + (fCents * 100);
printf("dollars = %0.02f cents = %0.02f centsLeft = %ld\n", fDollars, fCents*100, ulCentsLeft);
if( fChange > 0.0 )
{
ulQuartersReturned = CalulateQuarters(ulCentsLeft,SBankDenom.ulBankQuarter );
ulDimesReturned = CalulateDimes(ulCentsLeft,SBankDenom.ulBankDime );
ulNicklesReturned = CalulateNickels(ulCentsLeft,SBankDenom.ulBankNickel );
ulPenniesReturned = CalulatePennies(ulCentsLeft,SBankDenom.ulBankPenny );
DisplayResults(ulQuartersReturned,ulDimesReturned, ulNicklesReturned, ulPenniesReturned );
if(SBankDenom.bBankBroke)
{
printf("Program ending, the bank is broke\n");
break;
}
}
printf("Hit the X key to e(X)it, any other key to repeat procedure ");
if(toupper(getche()) == 'X')
{
printf("\nProgram finished\n");
break;
}
else printf("\n");
fChange = fCents = fDollars = fTotal = fAmtPaid = 0.0;
}
while (TRUE);
return 0;
}
|
| stormfront is offline | |
| | #6 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| Sounds like you're printing it as it is making change. Try printing when it's done instead. Also, stuff like this isn't that hard to troubleshoot if you stop and actually think. Go over what you said: "It prints it out four times." Well gee, that sounds like it's being printed in a loop. Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #7 | |
| Registered User Join Date: Oct 2005
Posts: 53
| Quote:
i got that, ive tried a few things but i can only get it to work when it prints in a loop pattern. ill keep workin on it... Last edited by stormfront; 11-01-2005 at 08:45 AM. Reason: oops, had to reword my sentence. | |
| stormfront is offline | |
| | #8 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| I guess moving the printf statement outside the loop never occurred to you. Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #9 |
| Registered User Join Date: Oct 2005
Posts: 53
| hey bob, after i cleaned up a few errors and moved some stuff around the code was pretty accurate. My instructor emailed me with 2 more functions to add to it, but i studied what you had showed me and did them in less than 10 minutes. Thx for the help. With the new things that i havent seen before in the code you posted i think im actually a little ahead of my classmates now that i understand them. Im gonna have to keep practicing, but im getting the hang of things. THX. |
| stormfront is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| doubt in c parser coding | akshara.sinha | C Programming | 4 | 12-23-2007 01:49 PM |
| Consumer program | wise_ron | C Programming | 11 | 09-27-2006 05:21 AM |
| const at the end of a sub routine? | Kleid-0 | C++ Programming | 14 | 10-23-2005 06:44 PM |
| I need help with passing pointers in function calls | vien_mti | C Programming | 3 | 04-24-2002 10:00 AM |