Ok guys i am suppose to simulate an atm machine
declare a variable to store the acct balance
there shud be three options
1. if the user choses option 1, print balance
2. If the user choses option 2, ask the user to enter amt they want to withdraw. accept the amt, subtract from balance
3. if the user choses option 3, ask the user to enter amt they want to deposit. accept the amt, add to balance
at the end of the program output balance and thank you msg


here's what i did
START
DECLARE balance, amt, deposit, result
DECLARE option as integer
DOCASE
CASE OPTION = 1
RESULT = balance
CASE OPTION = 2
WRITE “Enter the amount you wish to withdraw”
READ AMT
RESULT = balance-amt
CASE OPTION = 3
WRITE “Enter the amount you wish to deposit”
READ deposit
RESULT = balance+deposit
WRITE” Balance is currently” RESULT “ Thank you for using the ATM”
ENDCASE
STOP

should there be a loop? i nt sure..

Dani AI

Generated

Quick review for : the idea is fine, but a few issues will make the program behave incorrectly. The stored balance must be initialized and updated (don’t compute into a separate result and then forget to write it back). Add input validation (reject negatives and non-numeric input) and an overdraft check. As pointed out, put the menu inside a loop only if you want the user to do more than one transaction; otherwise a single-pass menu is enough.

Practical approach: ask (or set) an initial balance, present a menu with an explicit exit choice, update the balance variable directly for deposits/withdrawals, and validate amounts before changing the balance. Represent money in cents (integer) to avoid floating-point rounding issues. Check scanf return values and clear the input buffer on invalid reads.

Example C structure (concise, ready to adapt):

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    long long balance_cents = 0;
    int choice;
    double amt;

    printf("Enter initial balance (e.g. 100.50): ");
    if (scanf("%lf", &amt) != 1) return 1;
    balance_cents = (long long)(amt * 100.0 + 0.5);

    while (1) {
        printf("\n1) Show balance  2) Withdraw  3) Deposit  4) Exit\nChoose: ");
        if (scanf("%d", &choice) != 1) { int ch; while ((ch=getchar())!='\n' && ch!=EOF); continue; }
        if (choice == 1) {
            printf("Current balance: %lld.%02d\n", balance_cents/100, (int)llabs(balance_cents%100));
        } else if (choice == 2) {
            printf("Amount to withdraw: "); if (scanf("%lf",&amt)!=1) break;
            long long w = (long long)(amt*100.0 + 0.5);
            if (w<=0) printf("Enter a positive amount.\n"); else if (w>balance_cents) printf("Insufficient funds.\n"); else balance_cents -= w;
        } else if (choice == 3) {
            printf("Amount to deposit: "); if (scanf("%lf",&amt)!=1) break;
            long long d = (long long)(amt*100.0 + 0.5);
            if (d<=0) printf("Enter a positive amount.\n"); else balance_cents += d;
        } else if (choice == 4) break; else printf("Invalid option.\n");
    }

    printf("Final balance: %lld.%02d\nThank you for using the ATM.\n", balance_cents/100, (int)llabs(balance_cents%100));
    return 0;
}

Quick test checklist: try non-numeric input, negative amounts, withdrawing exactly the balance, and repeated transactions. Fixes above will make the flow clear and keep the stored balance accurate.

Recommended Answers

All 2 Replies

Do you want to do something more than once? If so, yes. If not, no.

Do you want to do something more than once? If so, yes. If not, no.

is what i did correct?

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.