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));
}