| admccann (11) | |
|
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; } | |
|
|
|
| pogrady (410) | |
|
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. | |
|
|
|
| Maniax (36) | |||||
if( accBalance <= 1000 || accBalance > 0 ){If accBalance is -100, interest is stil added so fix it to if( accBalance <= 1000 && accBalance > 0 ){
not sure what you wanted to achieve here; Possibly
which could be written in one line actually... cout << "Interest Rate: " << interestRate * 100 << "%" << endl;
| |||||
|
Last edited on
|
|||||
| Thumper (310) | |||
|
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.
EDIT: Beat by Manix, :P | |||
|
Last edited on
|
|||
| admccann (11) | |
|
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; } | |
|
|
|
| Maniax (36) | |||
|
Use code tags. Can't see any changes from the first post...
| |||
|
|
|||