Need help

Why does this not display 2% when it outputs all the answers. It does the calculation of 2% fine but doesn't display "interest rate: 2%"

#include <iostream>
using namespace std;

int main(){
int accBalance = 0;//change keypoint name , initialize value are important
float interestPaid = 0;
float interestRate = 0;
float totalBalance = 0;

cout << "Enter account balance : ";
cin >> accBalance;

if( accBalance <= 1000 || accBalance > 0 ){
interestRate = 0.02;
interestPaid = interestRate * accBalance;
totalBalance = accBalance + interestPaid ;
}
else if( accBalance > 1000 ){
interestRate = 0.05;
interestPaid = interestRate * accBalance;
totalBalance = accBalance + interestPaid ;
}

cout << "Total Balance : " << totalBalance << endl;
cout << "Interest Rate : ";
if( interestRate == 0.02 )
cout << "2%" << endl;
else if
cout << "5%" << endl;
cout <<"Interest Paid : " << interestPaid << endl;


return 0;
}
Theres a couple of things here. One is that you will want to read the sticky topic in the beginners forum called "Console closing down."

The second id that if..else if statement. there is no condition for the second if. All you need is the else.
if( accBalance <= 1000 || accBalance > 0 ){

If accBalance is -100, interest is stil added so fix it to
if( accBalance <= 1000 && accBalance > 0 ){

1
2
3
4
5
if( interestRate == 0.02 )
cout << "2%" << endl;
else if
cout << "5%" << endl;
cout <<"Interest Paid : " << interestPaid << endl;


not sure what you wanted to achieve here;
Possibly
1
2
3
4
5
6
if( interestRate == 0.02 )
cout << "2%" << endl;
else if (interestRate == 0.05)
cout << "5%" << endl;
else
cout <<"Interest Paid : " << interestPaid << endl;


which could be written in one line actually...
cout << "Interest Rate: " << interestRate * 100 << "%" << endl;
Last edited on
You can't use the == operator on floats. How floats are handled and truncated vary with computer and compiler. It is highly unlikely that the value of interestRate will ever equal exactly 0.02, because floating point variables are always calculated to seven significant digits It will actually be something like "0.0200001." You can never know exactly what the margin of error will be on these calculations, so a rule of thumb is to never use the equality operator on floats/doubles.

A way around this would be to output the variable directly.
 
cout << "Interest Rate: " << interestRate*100 << "%" << endl;


EDIT:
Beat by Manix, :P
Last edited on
Okay thanks helped a lot. I need to amend it now so that tax can be deducted from interest. the programs needs to give an option at the start if to tax the interest. Tax rate should be %20. the output needs to be changed so that it shows tax deducted. this is what i have done so far.

#include <iostream>
using namespace std;

int main(){
int accBalance = 0;//change keypoint name , initialize value are important
float interestPaid = 0;
float interestRate = 0;
float totalBalance = 0;
int yestax
int notax

cout << "Enter account balance : ";
cin >> accBalance;

cout << "Should interest be taxed?";


if( accBalance <= 1000 || accBalance > 0 ){
interestRate = 0.02;
interestPaid = interestRate * accBalance;
totalBalance = accBalance + interestPaid ;
}
else if( accBalance > 1000 ){
interestRate = 0.05;
interestPaid = interestRate * accBalance;
totalBalance = accBalance + interestPaid ;
}

cout << "Total Balance : " << totalBalance << endl;
cout << "Interest Rate : ";
if( accBalance <= 1000 )
cout << "2%" << endl;
if (accBalance > 1000)
cout << "5%" << endl;
cout <<"Interest Paid : " << interestPaid << endl;


return 0;
}


Use code tags.
Can't see any changes from the first post...

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
#include <iostream>
using namespace std;

int main(){
int accBalance = 0;//change keypoint name , initialise value are important
float interestPaid = 0;
float interestRate = 0;
float totalBalance = 0;
bool tax;

cout << "Enter account balance : ";
cin >> accBalance;

cout << "Should interest be taxed?(1 is yes, 0 is no)";
cin >> tax;

if( accBalance <= 1000 && accBalance > 0 ){
interestRate = 0.02;
interestPaid = interestRate * accBalance;
if (tax)
interestPaid *= 0.80;
totalBalance = accBalance + interestPaid ;
}
else if( accBalance > 1000 ){
interestRate = 0.05;
interestPaid = interestRate * accBalance;
if (tax)
interestPaid *= 0.80;
totalBalance = accBalance + interestPaid ;
}

cout << "Total Balance : " << totalBalance << endl;
cout << "Interest Rate : " << interestRate*100 << "%" << endl;
cout <<"Interest Paid : " << interestPaid << endl;

return 0;
}
Topic archived. No new replies allowed.