Free Scan: Update Your PC's Outdated Drivers to Optimize Performance
July 28th, 2008, 02:01 PM
|
#1 (permalink)
| | Junior Member
Join Date: Jul 2008
Posts: 2
| Need Help with C - School project
Hi guys
I was given this problem for my summer assignment Write a program to simulate a cash register change calculator. Allow the user to enter a purchase price of up to 100 dollars. Then determine the best change to give back for a $100 bill. Print the results to the screen as below, with no zero
Now i am new to programming but have a couple of books on C so i thought i would give it my best shot. Here was what i have so far. If anyone could look over it and correct any mistakes or advice me on what to do that would be greatly appreciated. also if anyone could please recommend a good compiler package, that would be great also. Thank you for your time.
//================================================== ==========================
// Name : cash.cpp
// Author : Chris Thorne
// Version :1
// Copyright : Your copyright notice
// Description : Summer assignment
//================================================== ==========================
#include <stdio.h>
int main()
{
int dollar ; /* input – this will be 100*/
int change ; /* input – change that is needed*/
int one = 1 ; /* output*/
int five = 5 ; /*output */
int twenty = 20 ; /* output*/
int round1
int round2
int round3
/* asking how much money was given*/
Printf("Hello, please type in how much money was present> \n“);
Scanf("%d", &dollar)
Printf("Thank you, you entered %d dollars 0 cents. \n” , dollars) ;
Printf(" Please enter the amount of change needed > \n”);
Scanf("%d”, &change) ;
Printf("Thank you, you owe the customer %d dollars 0 cents. \n “ , change) ;
Printf("Your change is $ %d. \n")
/* now calculating the change*/
if (change < 20)
{
printf("0 $20 bill. \n");
if (change >= 15)
{
Printf("3 $5 bills. \n")
}
else if ((change >= 10) && (<15))
{
printf("2 $5 bills. \n")
}
else if ((change >= 5)&& (<10))
{
printf ("1 $5 bill.")
}
else if (change < 5)
{
printf ("0 $5 bill.")
}
}
else if ((change >= 20) && (<40))
{
printf('1 $20 bill');
round1 = change - twenty ;
if (round1 >= 15)
{
Printf("3 $5 bills. \n")
}
else if ((round1 >= 10) && (<15))
{
printf("2 $5 bills. \n")
}
else if ((round1 >= 5)&& (<10))
{
printf ("1 $5 bill. \n")
}
else if (round1 < 5)
{
printf ("o $5 bill. \n")
}
}
Last edited by chrispringle : July 28th, 2008 at 02:08 PM.
Reason: make it easier to read
|
| |
July 28th, 2008, 03:22 PM
|
#2 (permalink)
| | Real gangstas sip on Yacc
Join Date: Oct 2001 Location: Suckas-ville
Posts: 4,540
|
Well you are supposed to simulate a cash register, so I would imagine it would go more like this.
"Please enter the cost of the items"
"Please enter how much money they gave you"
Calculate how much change to give back
Calculate what bills/coins to use.
Also it appears you are not doing coins which I imagine would be a requirement.
For calculating how many of each bill start at the biggest and work down.
If the total is greater than the bill, use integer division to see how many of that bill to give them. Then set the remaining total to be total modulus bill and continue on with smaller bills.
__________________
Signatures blow hard
If your signature contains an ad of any kind, congratulations, you're on my ignore list.
|
| |
July 30th, 2008, 06:31 PM
|
#3 (permalink)
| | Ultimate Member
Join Date: Mar 2005 Location: Out of my mind
Posts: 2,742
|
I like to know what the teacher is covering at the point of the assignment.
Are you going over loops (for, while, if/else)?
Are you going over recursion?
Did he introduce you to the mod operator?
Not sure what your code is doing...what if my purchase was a penny? You owe me $99.99 
Last edited by Rootstonian : July 30th, 2008 at 06:33 PM.
|
| |
July 31st, 2008, 01:36 AM
|
#4 (permalink)
| | Junior Member
Join Date: Jul 2008
Posts: 2
|
no its the summer assignment, so i have never met the teacher.
this is my first time as well, so i probably did it a long and hard way.
The sheet said just to use dollar intervals. and to only use 1,5,20
I have the completed code now anyways. And it works great :] Code: #include <stdio.h>
int
main(void)
{
int dollar; /* input – this will be 100*/
int change; /* input – change that is needed*/
int circle1;/* 3 $5 bills*/
int circle5; /* 2 $5 bills*/
int circle3;/* 1 $5 bills*/
int circle4;/* 0 $5 bills*/
/* asking how much money was given*/
printf("Hello, please type in how much money was present> \n");
scanf("%d", &dollar);
printf("Thank you, you entered %d dollars 0 cents. \n", dollar);
printf(" Please enter the amount of change needed > \n");
scanf("%d", &change);
printf("Thank you, you owe the customer %d dollars 0 cents. \n", change) ;
printf("Your change is $ %d. \n", change) ;
/* now calculating the change*/
if (change < 20)
{
printf("0 $20 bill. \n");
if (change >= 15)
{
printf("3 $5 bills. \n");
circle1 = change - 15;
printf("%d $1 bill(s). \n", circle1 );
}
else if ((change >= 10) && ( change <15))
{
printf("2 $5 bills. \n");
circle5 = change - 10;
printf("%d $1 bill(s). \n", circle5 );
}
else if ((change >= 5)&& ( change <10))
{
printf ("1 $5 bill. \n");
circle3 = change - 5;
printf("%d $1 bill(s). \n", circle3 );
}
else if (change < 5)
{
printf ("0 $5 bill. \n");
circle4 = change;
printf("%d $1 bill(s). \n", circle4);
}
}
else if ((change >= 20) && ( change<40))
{
printf("1 $20 bill. \n");
change = change ;
if (change >= 35)
{
printf("3 $5 bills. \n");
circle1 = change -35 ;
printf("%d $1 bill(s). \n", circle1 );
}
else if ((change >= 30) && (change<35))
{
printf("2 $5 bills. \n");
circle5 = change - 30;
printf("%d $1 bill(s). \n", circle5 );
}
else if ((change >= 25)&& (change<30))
{
printf ("1 $5 bill. \n");
circle3 = change - 25 ;
printf("%d $1 bill(s). \n", circle3);
}
else if (change < 25)
{
printf ("0 $5 bill. \n");
circle4 = change - 20;
printf( "%d $1 bill(s). \n", circle4);
}
}
else if ((change >= 40) && ( change <60))
{
printf("2 $20 bills. \n");
if (change >= 55)
{
printf("3 $5 bills. \n");
circle1 = change - 55;
printf("%d $1 bill(s). \n", circle1 );
}
else if ((change >= 50) && ( change <55))
{
printf("2 $5 bills. \n");
circle5 = change - 50;
printf("%d $1 bill(s). \n", circle5 );
}
else if ((change >= 45)&& (change <50))
{
printf ("1 $5 bill. \n");
circle3 = change - 45;
printf("%d $1 bill(s). \n",circle3);
}
else if (change < 45)
{
printf ("0 $5 bill. \n");
circle4 = change - 40;
printf("%d $1 bill(s). \n",circle4);
}
}
else if ((change >= 60) && (change <80))
{
printf("3 $20 bills. \n");
if (change >= 75)
{
printf("3 $5 bills. \n");
circle1 = change - 75;
printf("%d $1 bills. \n", circle1 );
}
else if ((change >= 70) && (change <75))
{
printf("2 $5 bills. \n");
circle5 = change - 70;
printf("%d $1 bill(s). \n", circle5 );
}
else if ((change >= 65)&& (change <70))
{
printf ("1 $5 bill. \n");
circle3 = change - 65;
printf("%d $1 bill(s). \n",circle3);
}
else if (change < 65)
{
printf ("0 $5 bill. \n");
circle4 = change - 60;
printf( "%d $1 bill(s). \n", circle4);
}
}
else if ((change >= 80) && ( change <100))
{
printf("4 $20 bills. \n");
if (change >= 95)
{
printf("3 $5 bills. \n");
circle1 = change - 95;
printf("%d $1 bill(s). \n",circle1);
}
else if ((change >= 90) && (change <95))
{
printf("2 $5 bills. \n");
circle5 = change - 90;
printf("%d $1 bill(s). \n", circle5 );
}
else if ((change >= 85) && (change <90))
{
printf ("1 $5 bill. \n");
circle3 = change - 85;
printf("%d $1 bill(s). \n",circle3);
}
else if (change < 85)
{
printf ("0 $5 bill. \n");
circle4 = change - 80;
printf( "%d $1 bill(s). \n", circle4);
}
}
return(0);
}
#include <stdio.h>
int
main(void)
{
int dollar; /* input – this will be 100*/
int change; /* input – change that is needed*/
int circle1;/* 3 $5 bills*/
int circle5; /* 2 $5 bills*/
int circle3;/* 1 $5 bills*/
int circle4;/* 0 $5 bills*/
/* asking how much money was given*/
printf("Hello, please type in how much money was present> \n");
scanf("%d", &dollar);
printf("Thank you, you entered %d dollars 0 cents. \n", dollar);
printf(" Please enter the amount of change needed > \n");
scanf("%d", &change);
printf("Thank you, you owe the customer %d dollars 0 cents. \n", change) ;
printf("Your change is $ %d. \n", change) ;
/* now calculating the change*/
if (change < 20)
{
printf("0 $20 bill. \n");
if (change >= 15)
{
printf("3 $5 bills. \n");
circle1 = change - 15;
printf("%d $1 bill(s). \n", circle1 );
}
else if ((change >= 10) && ( change <15))
{
printf("2 $5 bills. \n");
circle5 = change - 10;
printf("%d $1 bill(s). \n", circle5 );
}
else if ((change >= 5)&& ( change <10))
{
printf ("1 $5 bill. \n");
circle3 = change - 5;
printf("%d $1 bill(s). \n", circle3 );
}
else if (change < 5)
{
printf ("0 $5 bill. \n");
circle4 = change;
printf("%d $1 bill(s). \n", circle4);
}
}
else if ((change >= 20) && ( change<40))
{
printf("1 $20 bill. \n");
change = change ;
if (change >= 35)
{
printf("3 $5 bills. \n");
circle1 = change -35 ;
printf("%d $1 bill(s). \n", circle1 );
}
else if ((change >= 30) && (change<35))
{
printf("2 $5 bills. \n");
circle5 = change - 30;
printf("%d $1 bill(s). \n", circle5 );
}
else if ((change >= 25)&& (change<30))
{
printf ("1 $5 bill. \n");
circle3 = change - 25 ;
printf("%d $1 bill(s). \n", circle3);
}
else if (change < 25)
{
printf ("0 $5 bill. \n");
circle4 = change - 20;
printf( "%d $1 bill(s). \n", circle4);
}
}
else if ((change >= 40) && ( change <60))
{
printf("2 $20 bills. \n");
if (change >= 55)
{
printf("3 $5 bills. \n");
circle1 = change - 55;
printf("%d $1 bill(s). \n", circle1 );
}
else if ((change >= 50) && ( change <55))
{
printf("2 $5 bills. \n");
circle5 = change - 50;
printf("%d $1 bill(s). \n", circle5 );
}
else if ((change >= 45)&& (change <50))
{
printf ("1 $5 bill. \n");
circle3 = change - 45;
printf("%d $1 bill(s). \n",circle3);
}
else if (change < 45)
{
printf ("0 $5 bill. \n");
circle4 = change - 40;
printf("%d $1 bill(s). \n",circle4);
}
}
else if ((change >= 60) && (change <80))
{
printf("3 $20 bills. \n");
if (change >= 75)
{
printf("3 $5 bills. \n");
circle1 = change - 75;
printf("%d $1 bills. \n", circle1 );
}
else if ((change >= 70) && (change <75))
{
printf("2 $5 bills. \n");
circle5 = change - 70;
printf("%d $1 bill(s). \n", circle5 );
}
else if ((change >= 65)&& (change <70))
{
printf ("1 $5 bill. \n");
circle3 = change - 65;
printf("%d $1 bill(s). \n",circle3);
}
else if (change < 65)
{
printf ("0 $5 bill. \n");
circle4 = change - 60;
printf( "%d $1 bill(s). \n", circle4);
}
}
else if ((change >= 80) && ( change <100))
{
printf("4 $20 bills. \n");
if (change >= 95)
{
printf("3 $5 bills. \n");
circle1 = change - 95;
printf("%d $1 bill(s). \n",circle1);
}
else if ((change >= 90) && (change <95))
{
printf("2 $5 bills. \n");
circle5 = change - 90;
printf("%d $1 bill(s). \n", circle5 );
}
else if ((change >= 85) && (change <90))
{
printf ("1 $5 bill. \n");
circle3 = change - 85;
printf("%d $1 bill(s). \n",circle3);
}
else if (change < 85)
{
printf ("0 $5 bill. \n");
circle4 = change - 80;
printf( "%d $1 bill(s). \n", circle4);
}
}
return(0);
}
Last edited by chrispringle : July 31st, 2008 at 01:40 AM.
|
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |