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;
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
$ 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]
/*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>
usingnamespace 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;
}