-Accounting program using a .txt file chosen at random by my professor
-The .txt file will have a list of made-up bank accounts with the three columns
in the format of bank account number(int), bank account user's last name(char?
he also suggested long might work to store their last name?), and the user's
balance (double).
-The exact lines of bank accounts will be random and the account
numbers/balance/lastnames will be random, but they will follow the same format
of account number, space/tab, last name, space/tab, and balance.
-The program asks the user to look up account by account number or last name.
-Account number lookup: this will display their last name and their balance.
-Lastname lookup: this will display their account number and their balance.
-The data read from the .txt file is stored in three parallel arrays.
-Sequential search used to find account number/lastname which the user types.
-If the user enters an account number/last name which is in the .txt file list,
the program saves the index and uses the index to look up the account number &
balance (or the last name and balance).
-And of course if the account number/last name is not found, the program says so.
The program thus far: There is a problem with the way it's reading the three types of input.
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
|
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
int acctnum[1000];
char name[1000];
double balance[1000];
//These are the three input-types used in the file.
char input;
int search_acct;
int search_name;
cout << "**This is the Accounting Program!**\n\n"
<< "Please enter the name of the file we\n"
<< "will be working with: ";
//User-chosen text file, which will be stored as "random"
char filename[50];
ifstream random;
cin.getline(filename, 50);
random.open(filename);
//If the file cannot be opened.
if(!random.is_open())
{
cerr << "Error opening the program!!" << endl;
system("pause");
exit (1);
}
cout << "Would you like to search by account number or last name?"
<< "Type 'n' for search by account number or 'l' for\n"
<< "search by last name.";
cin >> input;
//If the user desires to search by account number.
if (input == 'n' || 'N')
{
cout << "Searching by account number...\n";
cout << "Please enter the account number: ";
cin >> search_acct;
//search_acct==acctnum; I tried this and it gives me an error
random >> acctnum;
do
{
//Reads each line as: account number, last name, and balance.
random >> acctnum >> name >> balance;
if (acctnum == search_acct)
{
//Outputs last name and balance for the account number searched.
cout << "For the account " << acctnum << " the \n"
<< "last name is: \n" << name << "and the "
<< "account balance is: \n" << balance;
}
}
}
else
{
cout << "I could not find the account number.";
system ("pause");
}
}while(!random.eof())
}
//If the user desires to search by last name.
else if (input == 'l' || 'l')
{
cout << "Searching by last name...\n";
cout << "Please enter the last name of the account holder: ";
cin >> search_name;
random >> name;
do
{
//Reads each line as: account number, last name, and balance.
random >> acctnum >> name >> balance;
if (name == search_name)
{
//Outputs account number and balance for the last name searched.
cout << "For the user " << name << " the \n"
<< "account number is: \n" << acctnum << " and the "
<< "account balance is: \n" << balance;
}
else
{
cout << "I could not find the last name.";
system ("pause)";
}
} while (!random.eof())
}
else
{
cout << "Your search method was incorrect.";
system ("pause");
}
random.close();
system("pause");
return 0;
}
|
A sample of what a .txt file will look like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
7221379 Abula 970.12
6853921 Allen 268.01
4215534 Austin 3821.94
5551298 Bailey 3193.97
6224649 Balovich 444.21
6502286 Bates 4253.79
8448936 Bettencourt 2916.99
2883903 Blackburn 906.31
6752376 Boucher 1291.99
3564698 Brown 2001.18
6373150 Duncan 2963.2
6375372 Estrada 1152.6
8736368 Fernandez 1113.05
7218536 Gallagher 3703.46
6681985 Gonzales 2667.88
3909086 Ha 3042.25
5221192 Hernandez 72.04
5901163 Howard 2229.66
2427821 Johnston 3130.92
6331655 Nguyen 3004.99
|