Trouble calculating correct decimal digits - c++

I am trying to create a program that will do some simple calculations, but am having trouble with the program not doing the correct math, or placing the decimal correctly, or something. Some other people I asked cannot figure it out either.
Here is the code: http://pastie.org/887352
When you enter the following data:
Weekly Wage: 500
Raise: 3
Years Employed: 8
It outputs the following data:
Year Annual Salary
1 $26000.00
2 $26780.00
3 $27560.00
4 $28340.00
5 $29120.00
6 $29900.00
7 $30680.00
8 $31460.00
And it should be outputting:
Year Annual Salary
1 $26000.00
2 $26780.00
3 $27583.40
4 $28410.90
5 $29263.23
6 $30141.13
7 $31045.36
8 $31976.72
Here is the full description of the task:
8.17 ( Pay Raise Calculator Application) Develop an application that computes the amount of money an employee makes each year over a user- specified number of years. Assume the employee receives a pay raise once every year. The user specifies in the application the initial weekly salary, the amount of the raise (in percent per year) and the number of years for which the amounts earned will be calculated. The application should run as shown in Fig. 8.22. in your text. (fig 8.22 is the output i posted above as what my program should be posting)
Opening the template source code file. Open the PayRaise.cpp file in your text editor or IDE.
Defining variables and prompting the user for input. To store the raise percentage and years of employment that the user inputs, define int variables rate and years, in main after line 12. Also define double variable wage to store the user’s annual wage. Then, insert statements that prompt the user for the raise percentage, years of employment and starting weekly wage. Store the values typed at the keyboard in the rate, years and wage variables, respectively. To find the annual wage, multiply the new wage by 52 (the number of weeks per year) and store the result in wage.
Displaying a table header and formatting output. Use the left and setw stream manipulators to display a table header as shown in Fig. 8.22 in your text. The first column should be six characters wide. Then use the fixed and setprecision stream manipulators to format floating- point values with two positions to the left of the decimal point.
Writing a for statement header. Insert a for statement. Before the first semicolon in the for statement header, define and initialize the variable counter to 1. Before the second semicolon, enter a loop- continuation condition that will cause the for statement to loop until counter has reached the number of years entered. After the second semicolon, enter the increment of counter so that the for statement executes once for each number of years.
Calculating the pay raise. In the body of the for statement, display the value of counter in the first column and the value of wage in the second column. Then calculate the new weekly wage for the following year, and store the resulting value in the wage variable. To do this, add 1 to the percentage increase (be sure to divide the percentage by 100.0 ) and multiply the result by the current value in wage.
Save, compile and run the application. Input a raise percentage and a number of years for the wage increase. View the results to ensure that the correct years are displayed and that the future wage results are correct.
Close the Command Prompt window.
We can not figure it out! Any help would be greatly appreciated, thanks!

Do not store money as floating point. This will end only in tears. Store money as an integral number of cents.
The reason for this is that floating point math on a computer is necessarily inexact. You know that 0.40 / 2 = 0.20, but it's entirely possible that the computer will say it is 0.19999999999999, and that is not an error. The internal representation of floating point numbers makes it impossible for a computer to exactly represent some fractions, much like you cannot write out an exact decimal representation of 1/3 (without an infinite amount of paper).
When you are dealing with numbers that have fractional parts and for which inexactness is not acceptable (e.g. money), you must compute using fixed-point math. In general, you might use a fixed point library, but for an assignment like this, if you're not allowed to do so, an int that stores a number of pennies will do just fine, so long as you understand how integer division works. You will have to write more math code and account for the rounding yourself, though. But that's what you want. You want absolute control over rounding.

I changed your for loop to this:
cout << (i+1) << " $" << wage*52 << "\n";
wage = wage * (1+(raise/100.0));
And it did worked!. I see you didn't understand the language of the problem.

I think that the intention is to receive a 3% raise each year, but you are actually only adding 3% of the starting salary ($780 in this case) each year. You may want to explore modifying the wage value on each pass in the loop (I won't present a solution as I suspect that this is a homework problem, yes?).
The best way to catch this sort of problem is to run it in a debugger and step through each line looking for when the results don't match your expectations. It's usually pretty easy at that point to figure out where your logic went astray.

Your problem is that your program ignores compounding. You are calculating the dollar value of the raise once, and using that for each increase. Once you get your first raise, the value of your second raise needs to be calculated based on your new wage, not your original wage.

Related

An "If" question for a budget that refers back to two different cells -

I am trying to create a formula to use in a budget spreadsheet. This particular section I am struggling with is in the savings section. I want to create a formula that will pull money from my paycheck balance (reflected in one cell) if it reflects a negative number (meaning I am taking money from my savings), however if it is a positive number (meaning I am adding money to my savings) it will just add to the current balance of the savings (another cell). I hope this makes sense and someone can help.
I can't think of the right formula to even begin.

How can I create a loop that terminates at any desired year (C++)?

I was faced with creating a program that can tell a user how much interest will be added to a product after any arbitrary number of years. I had to use a loop to estimate the new price, among other things.
The problem I ran into was in the creation of the loop. I didn't know how to make it stop at any specific year that a user chose. What I did is made a for loop that outputted the loop for the next 99 years. This was my loop:
for (time>0; time<=99; time++) where time was the number of years from today.
This is obviously not ideal. I wrote this in frustration from not knowing how to create a loop that can end wherever the user wants. How can I create a loop that terminates at any desired year?
for (time>0; time<=99; time++)
This will stop at a predefined number, i.e. 99. To make it stop at a number given by user you need to:
Get the number from the user and save it in a variable
Replace 99 with the variable in your loop

Stuck on a program creation assignment utilising Visual C++,

I am Brand New to programming, Ienter code here have an assignment to do, based on this image and question here, " A local electronics store allows items to be purchased on hire purchase. The hire purchase option
requires a deposit of one fifth of the cash price. There is an interest charge of 10% on the
balance.
Consider the following example: A customer wishes to purchase a Dell Inspiron 15-inch laptop,
with an i5 processor and 8 GB RAM, for $3500. The item code is 12345 and the customer
chooses to pay via two years of hire purchase.
The program operates as follows:
Write a program, HirePurchase, which prints the invoice for an item on hire purchase as
detailed above. The program prompts for the item code, cash price and the number of years on hire
purchase. enter image description here
I have begun but every time I try to run the program only a piece of it presents itself on screen. I'm very sorry if this seems basic but I'm interested in programming.
#include <iostream>
using namespace std;
int main () {
int NumberMonthHirePurchase,ItemCode,NumberYearHirePurchase;
double CashPrice,Deposit,MonthlyInstall,NewbalanceOne, NewBalanceTwo;
cout<<"Please enter the Item Code:2768N3496YHG";
cin>>ItemCode;
cout<<"Please enter the cash price of the item:6,264";
cin>>CashPrice;
cout<<"Please enter the Number of Years Hire Purchase:1";
cin>>NumberYearHirePurchase;
cout<<endl;
cout<<"item details"<<endl;
cout<<"Item code:2768N3496YHG"<<ItemCode<<endl;
return 0;
}
this is what I have so far but only "please enter the item code" is appearing and nothing else. I don't know if it's because I am doing something wrong, can someone please assist, would be greatly appreciated.
The variable ItemCode is an int. That means cin>>ItemCode will read only 2768 from the input. The rest of the input, N3496YHG, will be left in the input buffer for the remaining input to read.
However, since the next input is to read a floating point value, and N3496YHG is not a valid floating point value, nothing will be read and an error flag will be set on the input stream.
Since the error flag is now set on the stream, the third attempt to read something (the integer for NumberYearHirePurchase) will not do anything at all.
To solve this, the ItemCode should be a string, using std::string.
Also note that input of floating-point values doesn't have to use the comma , as decimal separator. It depends on the settings of your computer.
Of course, the above is only valid if you actually try to input something. If you don't input anything, the program will appear to stand still as it patiently waits for your input.

What's the best way to round monetary amounts to the nearest nickel, quarter, $1, $5, etc. denominations?

Canada announced that they will no longer mint pennies and the U.S. Treasury is strongly contemplating following suit! That implies that monetary amounts will have to be rounded to the nearest nickel, thus requiring a lot of programming modifications!.. In the pawnshop business, we've been rounding loan amounts to the nearest $5 denominations, e.g. 50, 55, 60.. when the calculated loan falls between $50 and $100, to the nearest $10 denomination above $100, etc. Its common practice in order to minimize the use of smaller denomination bills. So what is a good algorithm for rounding to the nearest desired denomination?
In SQL, could I create a user-defined datatype to round to n-denomination, or is it better to leave the decimal(8,2) datatype alone and round with a function?
Assuming that you have your numbers as something like: 44.32, then all you need to do (this is pseudocode, the implementation in c++ should be trivial):
findmod = (num_to_be_changed*100)%5
if findmod <= 2
new_num = num_to_be_changed - findmod
else
new_num = num_to_be_changed + (5 - findmod)
end
new_num = new_num/100
I'd be tempted to use in SQL:
new_val = ROUND(old_value * 20) / 20;
And when nickels go the way of cents, then:
new_val = ROUND(old_value * 10) / 10;
This works fine in SQL with DECIMAL or NUMERIC values; it also works well with SQL FLOAT and a little less reliably with SQL SMALLFLOAT (simply because SMALLFLOAT may only have about 6 decimal digits of accuracy). The same basic technique can be used in C++ if the old_value is a double (more dubiously if it is a float, for the same reason that SQL SMALLFLOAT is problematic). In C++ or C, you'd use the <cmath> or <math.h> header to obtain a declaration for the function round() or roundf(). If you're using ESQL/C and dec_t, then you need decround(&dec_value, 0);.
In ESQL/C, you have provide the number of places to round to, and you'd have to code the multiply and divide, too. Assuming you have a decimal value dec_20 somewhere containing the value 20, you'd write:
dec_t new_val;
decmul(&old_val, &dec_20, &new_val);
decround(&new_val, 0);
decdiv(&new_val, &dec_20, &new_val);
You might error check decmul() and decdiv(); there is no return value from decround() to do an error check on. It is safe to use one of the inputs as the output in the last line.
Not a technical answer per se to the question, but a point to be considered. You may find that your "programmatic modifications" are illusory.
When Australia phased out 1 and 2 cent coins in the 1990s, rounding of amounts was only performed on cash transactions, and this is still the case. Card payments are charged to the cent. Likewise, telephone and other utility bills are charged to the cent, but rounded to the nearest 5c when (and only when) paid in cash over the counter. And of course, share trading and currency exchange continue to be dealt with in fractions of cents or pennies.
So if you can assume that the number of cash transactions rounded down (those ending in 1, 2, 6 or 7c) is roughly the same as the number rounded up (3, 4, 8 or 9c), then the only effect is that the cash register may be out by a few cents at the end of the day. In fact, it is more likely to be up a few cents, because all single item transactions will be rounded up (e.g. every $3.99 purchase is $4.00 in the till.)
There do not have to be any programming changes to support the phase-out of pennies. You can choose to show the rounded amount on printed dockets, but it's not correct to presume anyone is lumbered with a mini Y2K or Euro implementation problem.

How can I round money values to the nearest $5.00 interval?

I have an Informix-SQL based Pawnshop app which calculates an estimate of how much money should be loaned to a customer, based on the weight and purity of gold. The minimum the pawnshop lends is $5.00. The pawnshop employee will typically lend amounts which either ends with a 5 or 0. examples: 10, 15, 20, 100, 110, 125, etc. They do this so as to not run into shortage problems with $1.00 bills. So, if for example my system calculates the loan should be: $12.49, then round it to $10, $12.50 to $15.00, $13.00 to $15.00, $17.50 to $20.00, and so on!..The employee can always override the rounded amount if necessary. Is it possible to accomplish this within the instructions section of a perform screen or would I have to write a cfunc and call it from within perform?.. Are there any C library functions which perform interval rounding of money values?.. On another note, I think the U.S. Government should discontinue the use of pennies so that businesses can round amounts to the nearest nickel, it would save so much time and weight in our pockets!
I'd just divide by 5, round to the appropriate integer, and multiply by 5.
There might be an easier way but that would work.
The C function round and relatives would do what you ask.
float in_fives = roundf( exact_change / 5. ) * 5.;
There is also rint, which you might avoid because it is not guaranteed to round $12.50 properly.