Basic Interest Program with wrong math??

Hi. I have only been programming for a couple of weeks now, and I am running into some snags with my programs

For instance, the current program I am working on is a simple interest calculator.


My program doesn't calculate either equation correctly and shows results in scientific notation.

I must have a bad math equation, or I have my variables mixed up. Maybe I am using the wrong manipulator.

I'd greatly appreciate and comments.

I am using Code Blocks.

Thank you in advance.



These are the instructions from the teacher:

/*Write a program which calculates interest for 1 year.
Prompt the user for a dollar amount and an interest rate
then output the interest earned and new principal.
Format both these numbers to two decimal places. */

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main ()
{

double amount, rate, interest_rate, earned, total;

cout << "== One Year Interest Calculator ==" << endl;
cout << "Enter principal followed by interest rate." << endl;
cout << "Omit the dollar sign and percent symbol." << endl;
cout << "e.g. 13500.23 5.3" << endl;

interest_rate= rate * 0.01; //calculates percentge into a decimal point

cin >> amount, rate; //user input

earned = interest_rate * amount;//calculated interest
cout << "Interest earned: $ "<< showpoint << setprecision(2)<<earned<< endl;

total = earned + amount;//calculates amount plus interest
cout << "Ending balance: $ "<< showpoint << setprecision(2)<<total<< endl;
cout << "Big money!" <<endl;

return 0;
}

Thanks
Compile with warnings
Edit: Read the warnings
Last edited on
Thanks ne555.

I don't get any warnings, and it compiles just not with my desired results.

I'm sure I'm over looking something small. I've been working on programs for 3 days straight now.
$ g++ -Wall foo.cpp
foo.cpp: In function ‘int main()’:
foo.cpp:23:21: warning: right operand of comma operator has no effect [-Wunused-value]
foo.cpp:21:28: warning: ‘rate’ may be used uninitialized in this function [-Wuninitialized]
¿do you understand what they are saying?

Edit: as you didn't use code tags to post
1
2
3
interest_rate= rate * 0.01; //line 21

cin >> amount, rate; //line 23 
Last edited on
I haven't seen this before, but what I understand from it is 'rate' isn't initializing, and the comma between amount and rate is pointless?

When the user is prompted to enter two sets of numbers separated by a space, I don't need to use a comma?

Also in line 1 - I thought it necessary to multiply the rate by 0.01 to get the proper decimal places for percent. Is that correct?

I'll rework some more.

Thanks

















To input in several variables you use cin >> amount >> rate;

About the other error, you are trying to operate before you've got the input.
It is not an equation, it is assignment.


PS: ¿why there is so much white space in your reply?
My toddler must have contributed to the white space you see.

Thanks for your help ne55. I made more progress.

My interest converts correctly. Now the Ending Balance is askew. It is displaying in scientific notation.

Here is my more improved program if you care to look at it.

Thank you for telling me about code tags, I didn't know.

Thanks again.
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
/*Write a program which calculates interest for 1 year.
Prompt the user for a dollar amount and an interest rate
then output the interest earned and new principal.
Format both these numbers to two decimal places. */

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main ()
{

    double amount, rate, rate1, earned, total;

    cout << "== One Year Interest Calculator ==" << endl;
    cout << "Enter principal followed by interest rate." << endl;
    cout << "Omit the dollar sign and percent symbol." << endl;
    cout << "e.g. 13500.23 5.3" << endl;// prompt user


    cin >> amount>> rate;//user input
    rate1 = rate * 0.01;
    earned = rate1* amount;//calculated interest
    total = earned + amount;//calculates amount plus interest

    cout << "Interest earned: $ "<< earned<< endl;
    cout << showpoint<< setprecision (2);

    cout << "Ending balance: $"<<total<<endl;
    cout << showpoint<< setprecision (2);
    cout << "Big money!" <<endl;

    return 0;
}
WOW! I figured it out. Whew!

I figured out I needed to used fixed & set precision, and I only needed one line stating so directly under my declared variables.

Thanks for taking the time for me.

What a rush! This may be difficult at first but it is rewarding when you actually write a good / working program!
Topic archived. No new replies allowed.