DESPERATELY confused...

Feb 3, 2012 at 12:49am
Hi, I was assigned this homework but I'm just a beginner and have absolutely no idea at all how to work on this. =( I do need your help!!

Here's the assignment question:



You have been asked to write a program to help the cashiers make change. This program will begin by introducing the user to the program by printing your name and a short description of the program. It will then prompt the user to enter the cost of the purchased item between $0.05 and $1.00. The program then calculate the change. The output will consist of a print out that tells the user what the item cost, the amount of change, and how many quarters, dimes, nickels, and pennies this change will be.



I just don't know how to start this.. Please help =(
THANKS!!
Feb 3, 2012 at 1:17am
Where are you lost? Have you even tried?
Feb 3, 2012 at 1:24am
Just start the first part of the problem. Show the message, prompt the user for input (the numbers 0.05 and 1.0 tell you what type of variable you need for this).
Feb 3, 2012 at 1:35am
Show us what you've written so far
Feb 3, 2012 at 4:04am
This is quite embarassing but this is the only thing I have done so far....

# include <iostream>
using namespace std;

int main(void) {

// Start of program
double price = 0.00;// initialized to zero
cout<< "Please enter the price between 0.05 and 1.00" << endl;
cin>> price;
cout<< "The price is $" << price << endl;
double change = 0.00;
change = 1.00 - price;

return 0;
}
Feb 3, 2012 at 10:17am
ok so your about half way there. next you need to print out how much change there is. you already have that calculated just print it out like you did the price. then you will need a few more variables to hold what you calculate the quarters, dimes, nickles and pennies you should give to the customer. hint: use the modulus operator to get the remainders on your calculations. ex: 5%2 = 1, 5/2 = 2
Feb 3, 2012 at 10:26am
Go through it step by step, as per the description.

The first part asks to display your name and a description. Although it's trivial, lecturers will often put instructions like this in to ensure you're meeting requirements properly. They'll be easy marks there that you want to pick up and you'd kick yourself for missing out on.

You seem to have the calculations in place for working out the change and, as the user above said, you can use the modulus operator to help out on working out the coin denominations.

Just take it step by step and certainly don't be embarrassed about your code; everyone starts somewhere.
Feb 3, 2012 at 3:03pm
Im not quite surr how modulus operators work. How can i apply that to get change in coins??
Feb 3, 2012 at 4:42pm
modulo returns the remainder. Used with division it can help break down your change.

BUT, what change??? Your problem definition seems to be missing something. You need an items cost AND the amount the customer gives the cashier to know how much change will be returned. Is that really the whole problem that your teacher gave you?
Feb 3, 2012 at 4:45pm
We should be able to get the amount of change whenever we randomly choose the price of the product between $0.05 and $1.00.

This is the source code I have created so far, but it's not running.. It seems there's something wrong with "%."





// Project 4
// February 3rd, 2012

# include <iostream>
using namespace std;

int main(void) {

// Start of program
double price = 0.00; // initiazlied to zero
cout << "Please enter the price between 0.50 and 1.00" << endl;
cin >> price;
cout << "The price is $" << price << endl;
double change = 0.00;
change = 1.00 - price;
cout<<"The change will be $" << change<< endl;

// Variables - quarters, dimes, nickels, and pennies
cin >> change;
double quarters = change%0.25;
cin >> quarters;
double dimes = quarters%0.10;
cin >> dimes;
double nickels = dimes%0.05;
cin >> nickels;
double pennies = pennies%0.01;
cin >> pennies;
cout << "You will have" << quarters << " quarters," << dimes << " dimes," << nickles << " nickles, and" << pennies << "pennies." << endl;



return 0;
}



Last edited on Feb 3, 2012 at 4:46pm
Feb 3, 2012 at 4:48pm
You appear to be getting the user to enter the change
cin >> change;
and the quarters
cin >> quarters;
and the dimes
cin >> dimes;
and the nickels
cin >> nickels;
and the pennies
cin >> pennies;

Why? You're calculating these values. Why get the user to enter them after you've calculated them?

The modulus operator, %, works on integer values. It does not work on doubles or with 0.25.
Last edited on Feb 3, 2012 at 4:48pm
Feb 3, 2012 at 5:02pm
I have recreated it, but there're still some problems.. Can anybody make some corrections please?







// Project 4
// February 3rd, 2012

# include <iostream>
using namespace std;

int main(void) {

// Start of program
int price = 00; // initiazlied to zero
cout << "Please enter the price between 50 and 100" << endl;
cin >> price;
cout << "The price is ¢ " << price << endl;
int change = 00;
change = 100 - price;
cout<<"The change will be ¢ " << change<< endl;
cin >> change;
int quarters = change%25;
cin >> quarters;
int dimes = quarters%10;
cin >> dimes;
int nickels = dimes%5;
cin >> nickels;

if (nickels>=5) {
}
else {
int pennies =nickels%1;
}
cin >> pennies;
cout << "You will have" << quarters << " quarters," << dimes << " dimes," << nickles << " nickles, and" << pennies << "pennies." << endl;

return 0;
}
Feb 3, 2012 at 5:24pm
See this line here, where you get the user to enter the change?
cin >> change;
Don't.

You are calculating the change. Getting the user to type it in is wrong.

Likewise for cin >> quarters;, cin >> dimes;, cin >> nickels; and cin >> pennies;
Last edited on Feb 3, 2012 at 5:25pm
Feb 4, 2012 at 2:24am
Well, I tried to do the assignment myself but I got caught in a bug I can't solve.

The code I wrote is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Project 4
// February 3rd, 2012

#include "stdafx.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main(void)

	{

		//Starts the loop so the program can be used more than once without having the need to open it again.
		char indicator = 'y';
		while (indicator == 'y' || indicator == 'Y')
		{
			indicator = 'n'; //Set's the indicator to no so it can't get caught in an infinite loop by mistake.
			double price = 0.00; //Initializes variable price to zero.
			cout << "Please enter the price between 0.05 and 1.00" << endl; //Ask for cashier to input price.
			cin >> price; //Get price
			while (price > 1.0 || price < 0.05) // Conditional to check if the value entered is valid.
			{
				cout << "Please enter the price between 0.05 and 1.00" << endl; //If it's not valid it will prompt the user again to make an input.
				cin >> price; //The new input.
			}
				cout << endl;
				cout << "The price is $" << price << endl; //Tell the cashier the product's price
				double change = 0.00; //Initializes variable change to zero
				change = 1.00 - price; //Calculates de change
				cout<<"The change will be $" << change<< endl; //Inform the change to the cashier
				
				int quarters(0), dimes(0), nickles(0), pennies(0);//Initializes the variables for - quarters, dimes, nickels, and pennies
				change *= 100; //Removing decimals from variable change because double cannot be used to do the remaining necessaries operations.
				quarters = static_cast<int>(change)/25; //Used static_cast to explicit the type change to int to perform the operation, and calculated the number of quarters .
				dimes = (static_cast<int>(change) - (quarters * 25))/10; //Calculated the number of dimes.
				nickles = (static_cast<int>(change) - ((quarters * 25) + (dimes * 10)))/5;  //Calculated the number of nickles.
				pennies = (static_cast<int>(change) - ((quarters * 25) + (dimes * 10) + (nickles * 5))); //Calculated the number of pennies
				//Inform the cashier the amount of each coins (s)he will need.
				cout << "You will have " << quarters << " quarters, " << dimes << " dimes, " << nickles << " nickles, and " << pennies << " pennies." << endl;
				//Asks if the user want to make another operation.
				cout << "Please type 'y' for yes and 'n' for no:" << endl;
				cin	>> indicator; //Gets the answer from the user.
			
		}
		
		//Thanks for using your software.
		cout << "Thanks for using this program." << endl;
		
		return 0;
	}


I'm not able to understand why, because by hand all calculations are correct. When I input the values:
0.93
0.92
0.91
0.9
0.89
0.81
0.8
0.79
0.78
0.68
0.67
0.56
0.55
0.34

The answer is always missing 1 cent. I would really appreciate if someone could answer me why my code bugs on these values.

Sorry for the long post.

This is my first post, hope I didn't do anything wrong. Also hope my code is well commented and readable. As English is not my first language I apologize for any spelling/grammar mistakes.

edit reason: Corrected typos.
Last edited on Feb 4, 2012 at 2:27am
Topic archived. No new replies allowed.