error check...loop

This is a discussion on error check...loop within the C Programming forums, part of the General Programming Boards category; I have produced a menu operated bank operating system, it compiles and runs fine however if for example you try ...

  1. #1
    Unregistered
    Guest

    error check...loop

    I have produced a menu operated bank operating system, it compiles and runs
    fine however if for example you try to withdraw money before you depositied
    it gives you a error message and asks you to put in another vaule, how can i
    make this loop end and return to the main menu? as if there is no money
    available to take out the same error message comes up over and over again.

    #include <stdio.h>
    #include <time.h>

    int get_option;
    time_t oclock;
    struct tm *thetime;

    void check_bal (double balance);
    void deposit (double *balance);
    void withdraw (double *balance);
    void showtime ();

    int main()
    {
    double balance = 0;

    while (1)
    {
    printf("select choice\n");
    printf("1.Check Balance\n");
    printf("2. Deposit amount\n");
    printf("3. Withdraw amount\n");
    printf("4. Quit menu\n");
    scanf("%d", &get_option);


    switch (get_option)
    {
    case 1:
    check_bal (balance);
    break;
    case 2:
    deposit (&balance);
    break;
    case 3:
    withdraw (&balance);
    break;
    default: // Do this instead of 4 to prevent invlaid option from casing
    spining
    return 0;
    }
    }
    }

    void check_bal (double balance)
    {
    printf("your blance is %.2f pounds\n", balance);
    }

    void deposit (double *balance)
    {
    float amount = 0; // set default

    printf ("Enter amount to deposit: ");
    scanf("%f.2", &amount);

    while (0.0 >= amount)
    {
    printf ("Invalid amount, re-enter: ");
    scanf("%f.2", &amount);
    }

    *balance += amount;
    printf("your balance is now %.2f\n", *balance);
    showtime();
    }

    void withdraw (double *balance)
    {
    float amount = 0; // set default

    printf ("Enter amount to withdraw: ");
    scanf("%f.2", &amount);

    while ((0.0 >= amount) || (amount > *balance))
    {
    printf ("Invalid amount, re-enter: ");
    scanf("%f.2", &amount);
    }

    *balance -= amount;
    printf("your balance is now %.2f\n", *balance);
    showtime();
    }

    void showtime () // Reusable
    {
    time(&oclock);
    thetime = localtime(&oclock);
    printf("%s\n", asctime(thetime));
    }

  2. #2
    Registered User Mace's Avatar
    Join Date
    Nov 2001
    Posts
    7
    I would create a global integer and set the value to zero. If you then deposit money, change the value to 1. Then use an if statement to do withdrawl. If a=0 then return else withdrawl. I don't use switches so I'm exactly sure how you would code it, but that's the plan I'd use to prevent an error happening.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-19-2005, 11:30 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21