So, when I run the program and try to list all the information I get an interesting error message that pops up:
-----
Debug Assertion Failed!
Program: ...ocuments\Programs\C + +
Programs\Checkbook\Debug\Checkbook.exe
File: f:\dd\vctools\crt_bld\self_x86\crt\src\tccpy_s.inl
Line: 19
Expression: (((_Src))) != NULL
-----
Though I have an idea that it probably has something to do with the if statement
regarding the list function, but I don't really get what's wrong with it. Also, I need a little help with the balance function. its suppose to keep track of how much money is left in the balance. So depositing would add money to the current balance and writing a check would decrease from the current balance. I was hinted of using strings to integers but I have no idea where to start, Thanks for any help. Below is my code:
*Also there's suppose to be a text file which the program reads and writes to, name it Checkbook.txt
Checkbook.h
#ifndef _CHECKBOOK_H_
#define _CHECKBOOK_H_
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include "Check.h"
using namespace std;
class Checkbook
{
private:
fstream file;
public:
Checkbook() // constructor opens the file
{
// input and output stream
file.open("Checkbook.txt", ios::in | ios::out | ios::app);
if (!file.good())
{
cerr << "ERROR: unable to open \"Checkbook.txt\"\n";
exit(1);
}
}
~Checkbook() // destructor flushes and closes the file
{
file.flush();
file.close();
}
char menu();
void interactive();
void list();
int read(Check stack[]);
void newDeposit();
void newDeposit(char* date, char* amount);
void newCheck();
void newCheck(char* checkNo, char* date, char* recipient, char* amount);
//void balance();
void print(Check* entry) { entry->printCheck(); }
};
#endif
Check.h
#ifndef _CHECK_
#define _CHECK_
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
enum sizes { FIELD = 20, DATE = 20, RECIPIENT = 50, AMOUNT = 50 }; // the sizes of card fields
class Check
{
private:
char checkNo[FIELD];
char date[DATE];
char recipient[RECIPIENT];
char amount[AMOUNT];
public:
Check()
{
checkNo[0] = '\0';
date[0] = '\0';
recipient[0] = '\0';
amount[0] = '\0';
}
Check(char* c, char* d, char* r, char* a)
{
strcpy_s(checkNo, c);
strcpy_s(date, d);
strcpy_s(recipient, r);
strcpy_s(amount, a);
}
Check(char* line)
{
strcpy_s(checkNo, strtok(line, ":"));
strcpy_s(date, strtok(NULL, ":"));
strcpy_s(recipient, strtok(NULL, ":"));
strcpy_s(amount, strtok(NULL, ":"));
}
void printCheck()
{
cout << setw(FIELD) << checkNo;
cout << setw(DATE) << date;
cout << setw(RECIPIENT) << recipient;
cout << setw(AMOUNT) << amount;
}
};
#endif
Checkbook.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <cstring>
#include "Checkbook.h"
#include "Check.h"
using namespace std;
int main(int argc, char* argv[])
{
Checkbook checkbook;
if (argc == 1)
checkbook.interactive();
else if (argc == 2 && !strcmp(argv[1], "-l"))
checkbook.list();
else if (argc == 2 && strcmp(argv[1], "deposit"))
checkbook.newDeposit(argv[2], argv[3]);
else if (argc == 5)
checkbook.newCheck(argv[1], argv[2], argv[3], argv[4]);
else
cerr << "USAGE: Checkbook | Checkbook -1 | Checkbook deposit <date> <amount> | Checkbook <checkNo> <date> <recipient> <amount> \n";
return 0;
}
char Checkbook::menu()
{
system("cls");
cout << "C\t Create a new check \n";
cout << "D\t Enter a new deposit \n";
cout << "L\t List all current entries \n";
cout << "B\t Balance checkbook \n";
cout << "Q\t Quit \n";
cout << "\n\nCommand: ";
char command;
cin >> command;
cin.ignore(); // discard the new line
return command;
}
void Checkbook::interactive()
{
while (true)
{
char operation = menu();
switch (operation)
{
case 'c' : // search
case 'C' :
newCheck();
system("pause");
break;
case 'd' : // search
case 'D' :
newDeposit();
system("pause");
break;
case 'l' : // enter
case 'L' :
list();
system("pause");
break;
case 'b' : // list
case 'B' :
//balance();
system("pause");
break;
case 'q' : // quit
case 'Q' :
exit(0);
default:
cerr << "Unknown option: " << operation << endl;
break;
}
}
}
void Checkbook::newDeposit()
{
char date[DATE];
char amount[AMOUNT];
cout << "Please enter date the check was written: ";
cin.getline(date, DATE);
cout << "Please enter the amount: $";
cin.getline(amount, AMOUNT);
newDeposit(date, amount);
}
void Checkbook::newDeposit(char* date, char* amount)
{
file.clear();
file.seekp(0, ios::end);
file << "Deposit" << ": " << date << ": " << "-" << ": " << "$ " << amount << endl;
file.flush();
}
void Checkbook::newCheck()
{
char checkNo[FIELD];
char date[DATE];
char recipient[RECIPIENT];
char amount[AMOUNT];
cout << "Please enter the check number: ";
cin.getline(checkNo, FIELD);
cout << "Please enter date the check was written: ";
cin.getline(date, DATE);
cout << "Please enter the recipient: ";
cin.getline(recipient, RECIPIENT);
cout << "Please enter the amount: $";
cin.getline(amount, AMOUNT);
newCheck(checkNo, date, recipient, amount);
}
void Checkbook::newCheck(char* checkNo, char* date, char* recipient, char* amount)
{
file.clear();
file.seekp(0, ios::end);
file << checkNo << ": " << date << ": " << recipient << ": " << "$" << amount << endl;
file.flush();
}
void Checkbook::list()
{
Check list[100];
int count = read(list);
for (int i = 0; i < count; i++)
print(&list[i]);
}
int Checkbook::read(Check stack[])
{
file.clear();
file.seekg(0);
int count = 0;
char line[100];
while (file.getline(line, 100))
{
stack[count] = Check(line);
count++;
}
return count;
}