I am writing this simple program of depositting and withdrawing an amount a printing the balance.
The program is not working correctly.....Any suggestions
I am not too sure whether I should use double , int or float
and also whether I should use gets fgets, scanf, or sscanf...I think this is where my problem is......
Thanks
Code:
#include <stdio.h>
#include<stdlib.h>
int main(void)
{
double old_balance, balance;
double deposit, withdraw;
int choice;
char line[15];
balance=0;
printf("Enter old balance: ");
gets(line);
sscanf(line,"%ld",&old_balance);
for(;;)
{
printf("\n MENU\n");
printf(" 1. deposit\n");
printf(" 2. withdrawal\n");
printf(" 3. quit\n\n");
printf(" choice: ");
if (scanf("%d", &choice)==1)
{
printf("\nEnter deposit amount: ");
fscanf(stdin,"%ld",&deposit);
balance= old_balance + deposit;
printf("\nNew balance: %ld\n", balance);
}
if (scanf("%d", &choice)==2)
{
printf("\nEnter deposit amount: ");
fscanf(stdin,"%ld",&deposit);
balance= old_balance + deposit;
printf("\nNew balance: %ld\n", balance);
}
if (scanf("%d", &choice)==3)
exit(1);
}
return 0;
}