iostream issues

The program will ask the user to enter in data, then copy this data to a file, and will then print it out in a table, then store the data in a master file (copies it to another file). What it wont do is allow the user to enter an account number to search for the data (and then display the data)....

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

//main.cpp

#include <iostream>
#include <fstream>		
#include <string>		//in order to input string
#include <iomanip>

using namespace std;

//prints table for data
void dataTable();
//prints out data from file
void fileInfo(int, string, string, double);
int getrequest();
//tests if account number is in master and account files
bool inAccountsFile(int, int);

int main()
{
	//local variables
	int account;
	string lastName, firstName;
	double balance;
	
	//file opened called trans.txt for output at the beginning of file
	ofstream outAccounts("trans.txt", ios::out);
	ofstream outMasterAccounts;		//file to use later for master account
	//if file could not be opened
	if (!outAccounts)
	{
		cerr << "File could not be opened" << endl;
		exit(1);
	}

	cout << "Enter Account Number, Last Name, First Name, and Balance:\n?";
	//inputs information into file
	while (cin >> account >> lastName >> firstName >> balance)
	{
		outAccounts << account << " " << lastName << " " << firstName << " " << balance << endl;
		cout << "?";
	}
	
	//file opened to read data from file
	ifstream inAccounts("trans.txt", ios::in);
	//if file could not be opened
	if (!inAccounts)
	{
		cerr << "File could not be opened" << endl;
		exit(1);
	}

	//Takes file data and puts it in Master file for end of day simulation
	outMasterAccounts.open("master.txt", ios::out);
	if (!outMasterAccounts)
	{
		cerr << "File could not be opened" << endl;
		exit(1);
	}
	dataTable();		//draws table
	while (inAccounts >> account >> lastName >> firstName >> balance)
	{
		//prints file data in table
		fileInfo(account, lastName, firstName, balance);
		//puts file info into master file
		outMasterAccounts << account << " " << lastName << " " << firstName << " " << balance << endl;
	}
	inAccounts.clear();		//reset eof
	inAccounts.seekg(0, ios::beg);	//reposition to beginning of file
	//asks user for account number, tests against both files to come up with data if available
	//inputs zero if wanting to stop
	int request = getrequest();



	//while user keeps entering account numbers to look up
	while (request != 0)		
	{
		while (!inAccounts.eof())	//until end of file
		{
			if (inAccountsFile(request, account))
			{
				dataTable();	//draws new table
				//read info from file
				inAccounts >> account >> lastName >> firstName >> balance;
				fileInfo(account, lastName, firstName, balance);	//displays current info
				break;
			}
		}
		cout << "\n?";
		cout << "Enter the account number you'd like to see\n";
		cout << "Zero to stop\n?";
		request = 0;
	}

	
	cout << "\n\n\nEnd of data\n\n\n";

	system("pause");
	return 0;
}

void dataTable()
{
	cout << setw(12) << left << "Account #" << setw(15) << "Last Name" << setw(15) << "First Name" << setw(12) << right << "Balance\n";
}

void fileInfo(int account, string lastName, string firstName, double balance)
{
	cout << setw(12) << left << account << setw(15) << lastName << setw(15) << firstName << setw(12) << right << balance << endl;
}

bool inAccountsFile(int request, int account)
{
	//if the requestion matches the account number
	if (request == account)
		return true;
	//if not
	return false;
}

int getrequest()
{
	int request = 1;
	do
	{
		cout << "Enter the account number you'd like to see\n";
		cout << "Zero to stop\n";
		cin >> request;
	} while (request < 0);
	return request;
}
It would be better if you declare all the variables and functions inside a class and open the files in binary mode and use read and write to do the operations on required files one by one.It will be easy to sort and search data
Last edited on
Hey man thanks! Your reply didnt really solve my issue, but it pointed me in the right direction! Thanks a bunch!
ur welcome :)
Topic archived. No new replies allowed.