/*******************************************************************
Checkbook version 1
Katie Rivard
Simple user input/output practice, file pointers, commands etc
*******************************************************************/

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

#define __NO_WIDE_CHAR

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

main()
{
long double balance;
int commandnum; /*number of the command*/
int i=1;
FILE *iofile;
char filename[10];
strcpy(filename, /**/);

iofile=fopen(filename, "r"); /**/
balance=getbalance(); /*gets beginning balance*/

sortnums: /*goto label*/
while(i)
{
commandnum=getcommand(); /*gets command (withdrawl/deposit/etc)*/
if (commandnum==99) goto sortnums; /*allow for invalid commands*/
if (commandnum<5) /*withdrawl or deposit*/
{
balance+=alterbalance(commandnum);
printf("\nBalance:\t%.2lf\n", balance);
}
else if(commandnum<10)
{
commandnum-=5;
if(commandnum) /*quit*/
{
printf("This ends your checkbook session.\n");
i=0;
}
else /*help*/
{
printf("\nCheckbook Help: Commands\ndeposit (value)\t\td (value)\tDeposits the value into your balance.\n");
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");
}
}

}
return 0;
}


char getfile() /**/
{
char filename[10];
char a;

printf("Do you have a personal checkbook file(y/n)?\n");
scanf("%c", &a);
if (a=='y')
{
printf("Please enter the filename:\n");
scanf("%s", &filename);
}
return *filename; /**/
}


int getbalance() /*getbalance(): gets user's current 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/true for deposit, 0/false for withdrawl, 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/true*/
return 1;
else if(command[0]=='w' || command[0]=='c') /*Withdrawl or Check/false*/
return 0;
else if(command[0]=='q') /*Quit/5+true*/
return 6;
else if(command[0]=='h') /*Help/5+false*/
return 5;
else if(command[0]=='n') /*New balance/10+true*/
return 11;
else
{
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; /*command 1 (deposit)*/
else
return -amount; /*(withdrawl)*/
}