1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
using namespace std;
char menu();
void interactive();
void enter(char* num, char* date, char* to, double amount);
void list();
void balance();
enum sizes { NUM_SZ=15, DATE_SZ=15, TO_SZ=30, AMOUNT_SZ=10 }; // data sizes
int main(int argc, char* argv[])
{
if (argc == 1) // no arguments ==> enter interactive mode
interactive();
else if (argc == 2) // allow either -l (lower case L) or -b
{
if (!strcmp(argv[1], "-l")) // -l
list();
else if(!strcmp(argv[1], "-b")) // -b
balance();
else
interactive(); // unrecognized flag: go in to interactive mode
}
else if (argc == 5) // 4 args ==> make new entry and quit
enter(argv[1], argv[2], argv[3], atof(argv[4]));
else
cerr << "USAGE: checkbook [ -l | -b ] | checkbook <num> <date> <to> <amount>\n";
char* temp = "Version 2008/11/18";
return 0;
}
char menu()
{
cout << "E\tEnter A Check or Deposit\n";
cout << "L\tList All Entries\n";
cout << "B\tBalance Checkbook\n";
cout << "Q\tQuit\n";
char chice;
cin >> chice;
cin.ignore(); // discard the new line
return chice;
}
void interactive()
{
while (true)
{
char operation = menu();
switch(operation)
{
case 'b' : // balance
case 'B' :
balance();
break;
case 'e' : // enter
case 'E' :
{
char num[NUM_SZ];
char date[DATE_SZ];
char to[TO_SZ];
char amount[AMOUNT_SZ];
cout << "Please enter the check number: ";
cin.getline(num, NUM_SZ);
cout << "Please enter the check date: ";
cin.getline(date, DATE_SZ);
cout << "Please enter the check reipient: ";
cin.getline(to, TO_SZ);
cout << "Please enter the check amount: ";
cin.getline(amount, AMOUNT_SZ);
enter(num, date, to, atof(amount));
break;
}
case 'l' : // list
case 'L' :
list();
break;
case 'q' : // quit
case 'Q' :
exit(0);
default:
cerr << "Unknown option: " << operation << endl;
break;
}
}
}
void balance()
{
double newBalance = tempBalance - checkAmount;
return newBalance;
}
void enter(char* num, char* date, char* to, double amount)
{
input.clear();
input.seekp(0, ios::end);
istrstream n1(amount);
double temp;
n1 >> temp;
input << num << " : " << date << " : " << to << " : " << "$" << amount << endl;
input.flush();
}
void list()
{
ifstream input("checkbook.txt");
if (!input.good())
{
cerr << "ERROR: unable to open \"checkbook.txt\"\n";
exit(1);
}
char line[100];
cout << "-----------------------------------------------------------------------\n";
while (input.getline(line, 100))
{
cout << left << setw(NUM_SZ) << strtok(line, ":");
cout << left << setw(DATE_SZ) << strtok(0, ":");
cout << left << setw(TO_SZ) << strtok(0, ":");
cout << "$" << right << setw(AMOUNT_SZ) << atof(strtok(0, ":")) << endl;
}
cout << "-----------------------------------------------------------------------\n";
balance();
input.close();
}
|