C++ coins conversion hw [duplicate] - c++

This question already has answers here:
c++ integer division
(3 answers)
Closed 3 years ago.
The last part of my code doesn't seem to run
#include <iostream>
#include <cmath>
int main() {
int dollars;
int cents;
std::cout << "Please enter your amount in the format of dollars and cents separated by a space: ";
std::cin >> dollars >> cents;
double quarters = dollars/0.25;
int whole_quarters = (int) quarters;
double dimes = cents/10;
int whole_dimes = (int) dimes;
double nickels = (dimes - whole_dimes)/0.5;
int whole_nickels = (int) nickels;
int pennies = std::fmod((dimes - whole_dimes),0.5);
std::cout << dollars << " dollars and " << cents << " cents are:\n";
std::cout << whole_quarters << " quarter " << whole_dimes << " dimes " << whole_nickels << " nickels " << pennies << " pennies ";
return 0;
}
I typed in 2 58 but the output was 8 quarters 5 dimes 0 nickels 0 pennies
It should be 1 nickels and 3 pennies
Can someone tell me what am i missing?

A better answer would be 10 quarters, 1 nickle, 3 pennies. Be that as it may.
I wouldn't have used this particular algorithm, although scanning it isn't telling me the obvious problem. I would debug by dumping out dimes (the double) and verify it's what you think it is, and then nickles (the double) and make sure that value makes sense, too.
I would do something like this:
int dimes = cents / 10;
int nickles = (cents % 10) / 5;
int pennies = (cents % 5);
If cents is 58, then dimes = 5, cents % 10 is 8, divided by 5 is 1, and cents % 5 is 3.
But it's worthwhile, if you're serious about programming, to put a lot of cout statements into your code and make sure values are becoming what you think they are.
Also, this will be faster if instead of asking for input, you comment out that code and hardcode your test data. Once you're getting proper test results that way, then switch back to asking for input.

Related

Calculate amount of coins

I have the assignment from ZyLab
Given six values representing counts of silver dollars, half dollars, quarters, dimes, nickels, and pennies, output the total amount as dollars and cents. The variable totalAmount is used to represent the total amount of money.
Output each floating-point value with two digits after the decimal point, which can be achieved by executing results once before all other cout statements.
Ex: If the input is: 5 3 4 3 2 1
where 5 is the number of silver dollars, 3 is the number of half-dollars, 4 is the number of quarters, 3 is the number of dimes, 2 is the number of nickels, and 1 is the number of pennies, the output is: Amount: $5.66
For simplicity, assume input is non-negative.
Here is my code:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double totalAmount;
int dollars;
int halfDollars;
int quarters;
int dimes;
int nickels;
int pennies;
/* Type your code here. */
/* get input */
cin >> dollars;
cin >> halfDollars;
cin >> quarters;
cin >> dimes;
cin >> nickels;
cin >> pennies;
/* calculate totalAmount */
cout << fixed << setprecision(2);
halfDollars = halfDollars * 0.5;
quarters = quarters * 0.25;
dimes = dimes * 0.10;
nickels = nickels * 0.05;
pennies = pennies * 0.01;
totalAmount = dollars + halfDollars + quarters + dimes + nickels + pennies;
/* output results */
cout << "Amount: " << totalAmount << endl;
return 0;
}
What is wrong?
Your variables should be of type float.
Rewrite your code as following:
float dollars;
float halfDollars;
float quarters;
float dimes;
float nickels;
float pennies;
When you are converting for example half dollars, 3 half dollars * 0.5 is 1.5, but it can't be stored inside integer variable, so you need the variable to be of type float.
So when compiler is executing this piece of code;
/* calculate totalAmount */
cout << fixed << setprecision(2);
halfDollars = halfDollars * 0.5;
quarters = quarters * 0.25;
dimes = dimes * 0.10;
nickels = nickels * 0.05;
pennies = pennies * 0.01;
it stores floating point value inside an integer declared variable, so compiler naturally casts it to integer type, producing incorrect output.

What's wrong with this simple piece of code? [duplicate]

This question already has answers here:
Why not use Double or Float to represent currency?
(16 answers)
Closed 3 years ago.
The code seems to work fine when it performs the first step of multiplying the number of quarters entered by 0.25, but then it just doesn't work with the next two steps.
#include <iostream>
using namespace std;
int main()
{
int quarter, dime, nickle;
int result;
quarter = 25;
dime = 10;
nickle = 5;
int numQuarters, numDimes, numNickles;
cout << "Please enter the number of quarters and press Enter: ";
cin >> numQuarters;
cout << "Please enter the number of dimes and press Enter: ";
cin >> numDimes;
cout << "Please enter the number of nickles and press Enter: ";
cin >> numNickles;
result = (numQuarters * quarter) + (numNickles * nickle) + (numDimes * dime);
cout << "The total amount of pennies is: " << result;
return 0;
}
I expect the output of 4 quarters, 10 dimes & 20 nickels to be 300 pennies
The output is 102
Edit: Code Fixed and working now!
Hmm... your code seems fine, but I think something was wrong with the Order of Operations in your Math. I just changed the value of nickel to 0.05 and the value of dime to 0.10 (I think that was a mistake in your code). I aslo moved the *100 down to the cout statement, and that helped clear things up...
#include <iostream>
using namespace std;
int main()
{
float quarter, dime, nickle, penny;
float result;
quarter = 0.25;
dime = 0.10;
nickle = 0.05;
float numQuarters, numDimes, numNickles;
cout << "Please enter the number of quarters and press Enter: ";
cin >> numQuarters;
cout << "Please enter the number of nickles and press Enter: ";
cin >> numNickles;
cout << "Please enter the number of dimes and press Enter: ";
cin >> numDimes;
result = (numQuarters * quarter) + (numNickles * nickle)+ (numDimes * dime);
cout << "The total amount of pennies is: " << result * 100;
return 0;
}
Oh, and just like what #Pete Becker said, do your calculations in pennies (whole numbers). Not only does it fix some minor errors with floats as money, it also makes your code easier to read and manipulate.

Calculating time for any sum to double, at a given rate of interest compounded annually

I was trying to write a program that calculates the time it takes for any sum of money to double at a given rate of interest compounded annually.
When I run this program, I have found that
the loop is not exiting
the counter is incremented endlessly
with the sum stuck at 100
What am I doing wrong?
int main(){
cout << "Please enter the interest rate in % per annum:";
int counter = 0;
int sum=100;
int interest = 0;
cin >> interest;
while(sum<200){
counter++;
sum += sum*(interest / 100);
}
cout << "\n It would take about " << counter << " years to double";
}
interest is an int so this line
interest / 100
is doing integer division, and will always be 0. The quick fix would be to change the literal so you are doing floating point math
sum += sum*(interest / 100.0);

Not counting all the coins

I'm trying to create a simple change sorter program, everything is functioning properly expect for the part that's suppose to detect quarters.
Example of current output:
The amount you entered is: 52.50
You have this many Fifty dollars: 1
You have this many Ten dollars: 0
You have this many One: 2
You have this many Quarters: 0
Desired output:
The amount you entered is: 52.50
You have this many Fifty dollars: 1
You have this many Ten dollars: 0
You have this many One: 2
You have this many Quarters: 2
Code:
#include <iostream>
using namespace std;
const int FIFTY = 50;
const int TEN = 10;
const int ONE = 1;
const double QUARTER = 0.25;
int _tmain(int argc, _TCHAR* argv[])
{
int change;
cout << "Enter the amount of money in your wallet: ";
cin >> change;
cout << endl;
cout << "The amount you entered is: " << change << endl;
cout << "The number of Fifty dollars to be returned is: " << change / FIFTY << endl;
change = change % FIFTY;
//
cout << "The number of Ten dollars to be returned is: " << change / TEN << endl;
change = change % TEN;
//
cout << "The number of One dollars to be returned is: " << change / ONE << endl;
change = change % ONE;
//
cout << "The number of Quarters to be returned is: " << change / QUARTER << endl;
change = change % QUARTER;
return 0;
}
The 2 errors I'm getting are:
Error 1 error C2297: '%' : illegal, right operand has type 'double'
Error2 IntelliSense: expression must have integral or unscoped enum type
Your change variable is an int type, so it won't store 52.50 at all.
It will read the 52 then stop.
On top of that, you can't use floating point values in the % operator.
I would suggest reading the value as a double, multiplying it by a hundred, perhaps adding a small delta (like 0.001) so as to avoid potential floating point precision problems, then placing that into an int. In other words, make it an integral number of cents.
Then use the int for doing the calculations.

C++ cashier code

The question:
Giving change. Implement a program that directs a cashier how to give
change. The program has two inputs: the amount due and the amount
received from the customer. Display the dollars, quarters, dimes,
nickels, and pennies that the customer should receive in return.
What i have so far:
#include <iostream>
using namespace std;
int main()
{
double amount_due;
double amount_recieved;
cout << "Enter amount due: " << endl;
cin >> amount_due;
cout << "Enter amount received: ";
cin >> amount_recieved;
int change = amount_recieved - amount_due;
int dollar = 100;
int quarters = 25;
int dimes = 10;
int nickels = 5;
int pennies = 1;
//Return change in full dollars
cout << "dollars: " << change % 100 << endl;
//Return change in quarters
cout << "quarters: " << (change % 100) % 25 << endl;
//Return change in dimes
cout << "dimes: " << ((change % 100) % 25) % 10 << endl;
// Return change in nickels
cout << "nickels: " << (((change % 100) % 25) % 10) % 5 << endl;
//Return change in pennies
cout << "pennies: " << ((((change % 100) % 25) % 10) % 5) % 1 << endl;
system("pause");
return 0;
}
I realize there are some other one of these answered but they may be to advanced to use in my code, what am i doing wrong?
What you want to do is the same as a cashier would do.
First ensure the change is represented as whole pennies.
Then provide enough dollars until the change remaining is less than a dollar. Then move on to quarters, then dimes, nickels and pennies.
So for the dollar case, pseudo-code would be:
dollars = change / 100 # get integral number of dollars
change = change % 100 # and reduce change-left-to-go accordingly
print dollars, " dollars"
It should then be a simple matter to apply that logic to the other coin types in order of reducing value.
There are several problems. The first is that you ask the user to input values, but don't specify what they are. If the user enters a number that's not in pennies, you're not going to get the value you expect. I expect this input should be in dollars. So, first, change change into:
change = int((amount_recieved - amount_due) * 100.0)
Next:
cout << "dollars: " << change % 100 << end;
Will return the remainder of dividing change by 100, which is not what you want. You simply want to divide dollars by 100. You also want to likely modify change to stop you having to repeat this math.
dollars = change / 100;
cout << "dollars: " << dollars << endl;
change -= dollars*100;
From there, the rest of the code should work as expected minus the % 100 parts. As others have mentioned in the comments to the question, your problem is arising from not thinking the math through first, not from doing anything inherently wrong with C++. This would have produced the wrong result in any language, including writing it down as math on a blackboard.
As others have mentioned, you are not doing the step required to get this working, and that is repeated subtraction. Yes, you subtracted the price from the amount given, and determined the dollars to give, but you failed to subtract out those dollars, giving you a new total to determine how many quarters.
Something like this (assuming you're working in pennies):
change_left = customer_payment - original_cost;
//...
number of dollars = change_left / 100;
change_left = change_left - (100 * number of dollars); <-- where is this?
Now how do you determine the number of quarters? You have a "running total" called change_left that is being reduced by the change you currently have given the customer. You repeat similar steps to get the number of quarters, then number of dimes, nickels, etc., i.e. divide by 25, then subtract out the number of quarters giving a new "change_left". Then repeat for 10 to get the dimes, 5 to get the number of nickels, etc.
So again, the problem isn't C++ -- the issue is that you are not thinking this out as a discrete series of steps that lead to a final goal.
#include <iostream>
using namespace std;
int main() {
double amount_due;
double amount_recieved;
cout << "Enter amount due: " << endl;
cin >> amount_due;
cout << "Enter amount received: ";
cin >> amount_recieved;
int change = amount_recieved - amount_due;
int dollar = 100;
int quarters = 25;
int dimes = 10;
int nickels = 5;
int pennies = 1;
//Return change in full dollars
cout << "dollars: " << change / 100 << endl;
//Return change in quarters
cout << "quarters: " << (change % 100) / 25 << endl;
//Return change in dimes
cout << "dimes: " << ((change % 100) % 25) / 10 << endl;
// Return change in nickels
cout << "nickels: " << (((change % 100) % 25) % 10) / 5 << endl;
//Return change in pennies
cout << "pennies: " << ((((change % 100) % 25) % 10) % 5) / 1 << endl;
return 0;
}