-
If else /else if problem
Ok so, I feared this would happen when I compiled and ran, and surely enough it did.
I'm getting a logical error within this program and i cant figure out how to fix it. I want to make it so that when I enter any of the numbers between 0 - 19, 20 - 39, 40 - 59, or 60+, it will only do the calculation of the entered number by the specific decimal value assigned to that group of numbers. Take a look at my code and you'll understand what im talking about
Code:
#include <iostream>
using namespace std;
int main()
{
double checkAmount;
int monthBankCharge = 10;
cout << "Enter the amount of checks you've written in the past month\n";
cin >> checkAmount;
if (checkAmount < 20)
{ cout << checkAmount * 0.10 << endl;}
else if (checkAmount >= 20)
{ cout << checkAmount * 0.08 << endl;}
else if (checkAmount >= 40)
{ cout << checkAmount * 0.06 << endl;}
else if (checkAmount >= 60)
{ cout << checkAmount * 0.04 << endl;}
system("pause");
return 0;
}
-
If checkAmount == 43 then the first if(checkAmount < 20) will not execute but since 43 > 20 the next if statement will execute and the rest of the statements will be skipped. You may want to rearrange your else if clauses. Maybe switch the >= 20 with >= 60.
Jim
-
Consider input such that checkAmount = 70. Then, trace the flow of control of the program, line by line. What do you observe?
-
The question says:
A bank charges $10 per month plus the following check fees for a commercial checking account:
$0.10 each for fewer than 20 checks
$0.08 each for 20-39 checks
$0.06 each for 40-59 checks
$0.04 each for 60 or more checks
write a program that asks for the number of checks written during the past month, then computes and displays the bank's fees for the month.
-
never mind I finally figured it out. Thanks to all that helped (: