Help with if else statement

closed account (z8q4izwU)
Hello, im having with my if else statements it wont compile and im probably doing something wrong. But here is my code......

#include <iostream>
#include <iomanip>

using namespace std;

//Savings Account
const double SAVING_SERV_CHARGES = 10.00;
const double SAVING_INTEREST_RATE = 0.04;

//Checking Account
const double CHECKING_SERV_CHARGES = 25.00;
const double CHECKING_INT_RATE_REG = 0.03;
const double CHECKING_INT_RATE_OVER_5000 = 0.05;

int main()
{
//variable declaration

int accountNumber;
char customerType;
double minimumBalance, currentBalance, savingsAccount, checkingAccount, newBalance;

cout << fixed << showpoint;
cout << setprecision (2);

cout << "Enter account number: ";
cin >> accountNumber;
cout << endl;

cout << "Enter a service code for the type of service owned: "
<< " S or s (Savings), "
<< " C or c (Checking): ";
cin >> customerType;
cout << endl;

cout << "Enter the minimum balance that the account should maintain: ";
cin >> minimumBalance;
cout << endl;

cout << "Enter the current balance of the account: ";
cin >> currentBalance;
cout << endl;

cout << "Account number: "
<< accountNumber
<< endl;
cout << "Account Type: "
<< customerType
<< endl;
cout << "Minimum Balance: $"
<< minimumBalance
<< endl;
cout << "Current Balance: $"
<< currentBalance
<< endl;
switch(customerType)
{
case 'S':
case 's':
//savings
if (currentBalance >= minimumBalance)
newBalance = currentBalance * SAVING_INTEREST_RATE;

else
newBalance = currentBalance - SAVING_SERV_CHARGES;
cout << "New Balance: $"
<< newBalance << endl;

//checking
case 'C':
case 'c':
if (currentBalance >= minimumBalance)
newBalance = currentBalance * CHECKING_INT_RATE_REG;

if (currentBalance <= minimumBalance)

else
newBalance = currentBalance * CHECKING_INT_RATE_OVER_5000;

else
newBalance = currentBalance - CHECKING_SERV_CHARGES;

cout << "New Balance: $"
<< newBalance << endl;

else
cout << "Invalid account type." << endl;
}
system("pause");
return 0;
}


This is the section that wont compile and i cant figure out why.It gives me the error expected primary-expression before "else" and expected ';' before "else" if anyone can help me id appreciate it!

if (currentBalance >= minimumBalance)
newBalance = currentBalance * CHECKING_INT_RATE_REG;

if (currentBalance <= minimumBalance)

else
newBalance = currentBalance * CHECKING_INT_RATE_OVER_5000;

else
newBalance = currentBalance - CHECKING_SERV_CHARGES;

cout << "New Balance: $"
<< newBalance << endl;

else
cout << "Invalid account type." << endl;
closed account (z8q4izwU)
This is the pseusdocode my teacher gave us to use for it...

If savings account (allow for upper and lower case data being entered)
If balance greater or equal to minimum balance
compute new balance using the interest rate
else
compute new balance deducting service charge
output the new balance
else
if checking account (allow for upper and lower case data being entered)
If balance greater or equal to minimum balance
if balance is less than or equal than (minimum balance +
checking high balance)
compute new balance using the interest rate
else
compute new balance using the interest rate over 5000
else
compute new balance deducting service charge
output the new balance
else
output a message for invalid account type entered
First of all, every else has to be paired with a corresponding if, and there can only be one else per if. You really need to indent that pseudocode so it makes more sense. Where ever the indentions are, is where your brackets {} should go.
If savings account (allow for upper and lower case data being entered)
     If balance greater or equal to minimum balance
           compute new balance using the interest rate
     else
          compute new balance deducting service charge
          output the new balance
else if checking account (allow for upper and lower case data being entered)
     If balance greater or equal to minimum balance
          if balance is less than or equal than (minimum balance + checking high balance)
               compute new balance using the interest rate
          else
               compute new balance using the interest rate over 5000
     else
          compute new balance deducting service charge
          output the new balance
else
     output a message for invalid account type entered


Any if statement or else statement that has more than one semicolon needs curly brackets around it. So...
1
2
3
4
5
6
7
8
9
10
if(x >10)
     cout << "hi" << endl;
     cout << "there" << endl;
//this ALWAYS couts "there" because that statement isn't part of the if statement. so if x>10 it couts "hi there", if x <=10 it couts "there"

if(x>10)
{
     cout << "hi" << endl;
     cout << "there" << endl;
} //this is the correct way. Same goes with else statements. 


The "invalid account type entered" will actually be your default statement in the switch cases.
For what I can see as I didn't run the program.

1
2
3
4
5
6
7
8
9
if (currentBalance >= minimumBalance)
newBalance = currentBalance * CHECKING_INT_RATE_REG;

if (currentBalance <= minimumBalance)
{
//missing a statement here, need to do something here
}
else
newBalance = currentBalance * CHECKING_INT_RATE_OVER_5000; 
Topic archived. No new replies allowed.