Hi,
I'm trying to write a program that computes interest and check fees and then prints a new calculated balance. So far it works okay, but seems like I have a lot of unnecessary work. Can someone help me simplify it down plz?

ps: The premium acct has no check fees, but the standard one does. premium=p, standard=s;
Premium acct gives .05 interest if the account is above 5k or .03 if it is under.

#include <iostream>
#include <conio.h>

using namespace std;


int main()
    {
        
        
        
        double FIVE = 5000.00;
        const double LOW_INTEREST = 0.03;
        const double HIGH_INTEREST = 0.05;
        const double CHECK_FEE = 0.10;
        char acct_type;
        bool okay = true, notOkay = false;
        
    
        do 
        {
        cout << "What type of account is this: " << endl;
        cout << "Type 's' for a standard account: " << endl;
        cout << "Type 'p' for a premium account: " << endl;
        cin >> acct_type;
        
        cin.ignore(80, '\n');
        okay = (acct_type == 's' || acct_type == 'p');
        if (!okay)
            cout << "\nYou entered an invalid account type!" << endl;
        }while(!okay);
        
        
        
        double earned_interest;
        double fees;
        double newbalance;
        double balance;
        int checks;
        
        if (acct_type == 'p') {
            
        cout << "Enter in the ending balance for this month: " << endl;
        cin >> balance;
        if (balance >= FIVE)
        earned_interest = balance * HIGH_INTEREST;
        else earned_interest = balance * LOW_INTEREST;
        
        
        if (balance < FIVE) {
        cout << "Enter in the number of checks written this month: " << endl;
        cin >> checks;
        fees = checks * CHECK_FEE;
        
        
        newbalance = balance - fees + earned_interest;
            cout << "\nThe new ending balance is: " << "$" << newbalance << endl;
        }else
        newbalance = balance + earned_interest;
            cout << "\nThe new ending balance is: " << "$" << newbalance << endl;
}         
        if (acct_type == 's')
        cout << "Enter in the ending balance for this month: " << endl;
        cin >> balance;
        cout << "Enter in the number of checks written this month: " << endl;
        cin >> checks;
            earned_interest = balance * LOW_INTEREST;
            fees = checks * CHECK_FEE;
            newbalance = balance - fees + earned_interest;
            cout << "\nThe new ending balance is: " << "$" << newbalance << endl;
        
        //getch();
        return 0;
}

Dani AI

Generated

— the fastest way to simplify and fix this program is to (1) centralize the math so you compute interest and fees exactly once, (2) only ask for the number of checks when the account actually pays fees, and (3) avoid the control-flow bugs caused by missing braces. asked about what kind of optimization — here the goal is clarity and correctness rather than performance (the math is trivial).

Simplified flow to follow:

  • validate account type ('s' or 'p');
  • read the ending balance;
  • pick the interest rate with a single conditional (premium gets 5% only if balance >= 5000, otherwise 3%; standard is 3%);
  • ask for number of checks only for standard accounts and compute fees;
  • compute newBalance = balance + interest - fees and print once.

Example implementation (short, clear, and fixes the brace/input bugs):

#include <iostream>
#include <iomanip>
#include <cctype>

double interestRate(char acct, double bal) {
    const double LOW = 0.03, HIGH = 0.05;
    return (acct == 'p' && bal >= 5000.0) ? HIGH : LOW;
}

int main() {
    char acct;
    do {
        std::cout << "Account type ('s' or 'p'): ";
        if (!(std::cin >> acct)) return 1;
        acct = std::tolower(acct);
    } while (acct != 's' && acct != 'p');

    double balance;
    std::cout << "Ending balance: ";
    if (!(std::cin >> balance)) return 1;

    double earned = balance * interestRate(acct, balance);
    double fees = 0.0;
    if (acct == 's') {
        int checks;
        std::cout << "Number of checks: ";
        std::cin >> checks;
        fees = checks * 0.10;
    }

    double newBalance = balance + earned - fees;
    std::cout << std::fixed << std::setprecision(2)
              << "New ending balance: $" << newBalance << '\n';
    return 0;
}

Notes and quick tips:

  • Always use braces for multi-statement if/else blocks to avoid subtle bugs (the original code printed/asked twice due to missing braces).
  • Remove nonstandard conio.h/getch() — they’re not portable.
  • Validate inputs (no negative balances/checks) and format money with std::fixed + std::setprecision(2).

What kind of optimization do you want to perform on this code, performance wise or lengthwise ? Please be more specific.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.