/***************************************************
Simple Checkbook version 1
Katie Rivard
Simple user input/output practice
***************************************************/

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

#define __NO_WIDE_CHAR

int getcommand();
int alterbalance(int commandnum);
int getbalance();

main()
{
long double balance; int commandnum;                   /*number of the command*/
int i=1;

balance=getbalance();      /*gets beginning balance*/

sortnums:            /*goto label*/
while(i)
{
commandnum=getcommand();                        /*gets command (withdrawl/deposit/etc)*/
switch(commandnum)
{
case 1:                                         /*fallthrough*/
case 2:       balance=+alterbalance(commandnum);       /*deposit(1) or withdraw(2)*/
       printf("\nBalance:\t%.2lf\n", balance);
       break;
case 3:       printf("This ends your checkbook session.\n");  /*quit*/
       i=0;
       break;
case 4:       printf("\nCheckbook Help: Commands\ndeposit (value)\t\td (value)\tDeposits the value into your balance.\n");                /*help*/
printf("withdrawl (value)\tw (value)\tTakes value out of your balance.\n");
       printf("new (value)\tn (value)\tNew balance: Enter a new balance.\n");
       printf("help\t\th\tView this text.\nquit\t\tq\tEnds your checkbook session.\n");
break;
case 99:      goto sortnums;                           /*allow invalid commands*/
       break;
}
      
}
return 0;
}



int getbalance()           /*getbalance():      gets balance*/
{
long double balancenum;
printf("Enter your balance:\n");
scanf("%lf", &balancenum);
printf("Balance=\t%.2lf\n", balancenum);
return balancenum;
}



int getcommand()           /*getcommand():      gets a number according to what the user enters; 1 for deposit, 2 for withdrawl, 3, 4, etc.*/
{
char command[100];

printf("\nEnter a command, or h for help:\n");
scanf("%s", &command);

if (command[0]=='d') /*checks the first letter of the word the user enters-- Deposit/1*/
       return 1;
else if(command[0]=='w')                               /*Withdrawl/2*/
       return 2;
else if(command[0]=='q')                               /*Quit/3*/
       return 3;
else if(command[0]=='h')                               /*Help/4*/
       return4;
else if(command[0]=='n')                               /*New balance/5*/
       return 5;
else                                            /*invalid/99*/
       {
       printf("Invalid command.\n");
       return 99;
       }
}



int alterbalance(commandnum)      /*alterbalance():    returns a negative value for withdrawls, positive for deposits, depending on command.*/
{
double amount;
scanf("%lf", &amount);
if(commandnum)
       return amount;
else
       return -amount;
}

/**********************************************************end*/
 


back