My school assignment is a bank account program that can run in two modes, interactive and bash mode. Right now I'm working on bash mode and need to accept command line arguments to open a database account (just a text file with info for different accounts, like names, account numbers, passwords, and balance) and perform actions like deposit, transfer funds, etc. However, we have to be able to accept these commands in any order the user passes. For instance, if you wanted to deposit something, you would run something like ./bankacct /OTestDB.txt /A12bw3 /Pabc123 /D300.00 where /O opens the database file that stores user info, /A and /P check the account number and password as a way of "signing in", and /D deposits 300 dollars to the account. We've yet to learn classes, and the program is mostly based in using pointers and structures. Additionally, we're only allowed to use c-strings, no string class, but I don't think that has a bearing on this part. So my problem is trying to figure out how to get ./bankacct /OTestDB.txt /A12bw3 /Pabc123 /D300.00 in the same way as ./bankacct /D300.00 /Pabc123 /A12bw3 /OTestDB.txt
What I have tried:
My first approach to this problem was to set a switch statement that would call the relevant function and just run it through a for loop to cover every argument. But if a user doesn't open the database file first, then I don't have the arguments to pass to a function to "log in" and choose the account. So with my current approach, it seems like the order of command line arguments would need to be constant. So any tips on where to get started would be great, as right now I'm imagining having a set order in which I take arguments and just reading through my argument array with a for loop for each argument until I get to the argument I need. Here's some of my relevant functions:
#include "bankacct_v4.h"
int main(int argc, char * argv[])
{
bool flag = true;
int num_acct = 0; account* user = new account[100];
if (argc > 1)
{
for (int i = 0; i < argc; ++i)
{
cout << "Argument " << i << " : " << left << setw(16) << argv[i] << endl;
if (i) commands(argv[i], i);
else cout << endl;
}
}
else
{
cout << endl;
}
delete[] user;
cout << endl;
cout << "Programmed by: " << PROGRAMMER_NAME << " -- ";
cout << __DATE__ << " " __TIME__ << endl;
cout << endl;
return 0;
}
void read_file(char* file, account* user, int &num_accts)
{
ifstream infile(file);
if (!infile)
cout << "\nError opening file: " << file << "\n\n";
else
{
cout << "Input file: " << file << endl;
while(infile) {
create(user, cout, infile, 0, num_accts);
}
infile.close();
}
}
char check_arg(char arg[])
{
char buf[100];
const char SLASH = '/';
char valid_options[] = "?ACDFILMNOPRSTW";
char *p = valid_options;
bool first_char = arg[0] == SLASH;
bool second_char = false;
for (; *p != 0; ++p) {
second_char = arg[1] == *p;
if (second_char == true) break;
}
if (!first_char || !second_char)
cout << "Invalid argument" << endl;
else
{
cout << "Option: " << *p;
if (arg[2] != 0)
{
strcpy(buf, arg+2);
cout << " Value: " << buf;
}
cout << endl;
}
return *p;
}
void create(account* user, ostream &out, istream &in, bool mode, int &num_acct)
{
if (mode) out << "Last Name? ";
in >> user[num_acct].lname;
if (mode) out << "\nFirst Name? ";
in >> user[num_acct].fname;
if (mode) out << "\nMiddle Initial? ";
in >> user[num_acct].mi;
if (mode) out << "\nSocial Security Number? ";
in >> user[num_acct].ssn;
if (mode) out << "\nPhone Number? ";
in >> user[num_acct].phone;
if (mode) out << "\nBalance? ";
in >> user[num_acct].bal;
if (mode) out << "\nAccount Number? ";
in >> user[num_acct].acctnum;
if (mode) out << "\nPassword? ";
in >> user[num_acct].acctpass;
num_acct++;
}
void display(account* user, ostream &out, int num_acct)
{
out << user[num_acct].lname << endl;
out << user[num_acct].fname << endl;
out << user[num_acct].mi << endl;
out << user[num_acct].ssn << endl;
out << user[num_acct].phone << endl;
out << user[num_acct].bal << endl;
out << user[num_acct].acctnum << endl;
out << user[num_acct].acctpass << endl;
out << endl;
}
void output(account* user, int num_acct)
{
ofstream outfile;
outfile.open("output.txt");
for (int i = 0; i < num_acct; i++)
{
display(user, outfile, i);
}
outfile.close();
}
void commands(char argv[], int i)
{
switch (check_arg(argv[i]))
{
case '?':
break;
case 'A':
break;
case 'C':
break;
case 'D':
break;
case 'F':
break;
case 'I':
break;
case 'L':
break;
case 'M':
break;
case 'N':
break;
case 'O':
break;
case 'P':
break;
case 'R':
break;
case 'S':
break;
case 'T':
break;
case 'W':
break;
}
I'm also aware that I'm not passing the right arguments to check_arg when I call it in commands. The function works if I call it instead of commands in main, but I don't know how check_arg would know what to actually
do with the commands, as it just says what the commands were.