C++ Tutorial #6 — Nested if statements

Homework: C++ Homework 3: Using if statements to control program flow


Topics Discussed: If statements, using bool(ean) operators as custom if-conditionals.


#include <iostream>

using namespace std;

int main() {
    double accountBalance = 5000.0, withdrawAmount = 0;

    cout << "How much money would you like to withdraw? ";
    cin >> withdrawAmount;

    if (withdrawAmount > accountBalance) {
        cout << "Error: The amount you tried to take out is greater than your current balance.";
    } else if (withdrawAmount > 1000) {
        cout << "It looks like you're trying to take out a lot of money, Press 1 to confirm, "
                "press 2 to decline this transaction to withdraw of:  " << withdrawAmount << " --";
        int confirm = 0;
        cin >> confirm;
        if (confirm == 1) {
            cout << "Confirmation accepted You withdrew: " << withdrawAmount <<
                    " -- Your remaining account balance is:  " << accountBalance - withdrawAmount;
        } else {
            cout << "Transaction declined by user.";
        }
    } else {
        cout << "You withdrew: " << withdrawAmount << " -- Your remaining account balance is:  " << accountBalance - withdrawAmount;
    }

    return 0;
}

C++ Tutorial #5 — Simple if conditional, if else structure, variable output
C++ Tutorial #7 — In depth: If statements, nested if commission problem