How do I add data from my program to a file for future use?

Here is my code, I want to be able to store customers, their data, etc and be able to view that data again and add to it the next time I run the program.

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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <iostream>

using namespace std;
//include protype functions
enum AccountType 
{unknown = -1, Checking, Savings, CreditCard, InstantAccess};

struct customerInfo 
	{
		char firstName[20]; 
		char lastName[20];
		float amount;
		AccountType type;
	};

int main()
{
	char choice;
	bool terminate = false;
	int i = 0;
	const int SIZE = 100;

	customerInfo myAccount[SIZE];

	//float depositAmount (){};
	//float withdrawAmount (){};

	while (!terminate)
	{
		cout << "WELCOME TO SO-AND-SO BANK!\n\n" << "Please enter an option, are you a:\n\n" 
		<< "A)Customer\n" 
		<< "B)Bank Employee\n" 
		<< "C)Bank Supervisor\n" 
		<< "D)Quit the application\n\n";
		cin >> choice;

		switch(choice)
		{
		case 'a':
		case 'A':
		cout << "Welcome to So-and-So Bank Customer Services!\n" << "Please enter an option:\n\n";
		//Customer options
		cout << "A)Functions: Balance Inquiry\n" 
		<< "B)Deposit Funds\n" 
		<< "C)Withdraw funds\n"
		<< "D)Transfer Funds to another account (like from Checking to Savings or to Credit Card)\n"
		<< "E)Anything else that you want to add\n" 
		<< "F)Quit the application\n\n";
		cin >> choice;

		switch(choice)
		{
		case 'a':
		case 'A':
			//struct name, amount, type
			for(i = 0; i < SIZE; i++) //edit
			{
				cout << "Enter your first name (20 characters or less):\n\n";
				cin >> myAccount[i].firstName;
				cout << "Enter your last name (20 characters or less):\n\n";
				cin >> myAccount[i].lastName;

				loop:
				cout << "What type of account do you wish to view?\n\n"
					<< "1. Checking\n" << "2. Savings\n"  << "3. CreditCard\n" 
					<< "4. InstantAccess\n" << "5. Quit";
				cin >> choice; //consider
			{
				if(choice == '1')
				{
					myAccount[i].type = Checking;
				}
				if(choice == '2')
				{
					myAccount[i].type = Savings;
				}
				if(choice == '3')
				{
					myAccount[i].type = CreditCard;
				}
				if(choice == '4')
				{
					myAccount[i].type = InstantAccess;
				}
				if(choice == '5')
				{
					cout << "You have chosen to quit, we appreciate your business.";
					terminate = true;
				}
				else
				{
					cout << "You've entered an invalid option, please try again";
					goto loop;
				}
			}

				//include breakable
				cout << "Your account balance is: " << myAccount[i].amount;
				//
			};

			break;
		case 'b':
		case 'B':
			//depositAmount();
			break;
		case 'c':
		case 'C':
			//withdrawAmount();
			break;
		case 'd':
		case 'D':
			//transfer enum
			break;
		case 'e':
		case 'E':
			//while or goto loop
			break;
		case 'f':
		case 'F':
		cout << "You have chosen to quit, we appreciate your business.";
		terminate = true;
			break;
		default:
		cout << "You have entered an invalid option, please try again.\n\n";
			break;
		}
		break;

		case 'b':
		case 'B':
		cout << "Welcome to So-and-So Bank Employee Services!\n" << "Please enter an option:\n\n";
		//Cascading customer data
		//a.Everything in the Customer Interface, plus
		cout << "A)Interface to ADD a new Customer to the Bank\n"
		<< "B)Interface to DELETE a  Customer from the Bank\n"
		<< "C)Search for a Record based on a “Name of a Customer” (using simple Sequential Search)\n"
		<< "D)Quit the application\n\n";
		cin >> choice;

		switch(choice)
		{
		case 'a':
		case 'A':
			//struct new customer
			for(i = 0; i < SIZE; i++) //edit
			{
				cout << "Enter your first name (20 characters or less):\n\n";
				cin >> myAccount[i].firstName;
				cout << "Enter your last name (20 characters or less):\n\n";
				cin >> myAccount[i].lastName;
			};
			break;
		case 'b':
		case 'B':
			//struct delete customer (Case C w/ remove name)
			break;
		case 'c':
		case 'C':
			//for loop name search, cin i, i customer
			cout << "So-and-So Bank Customers:\n";
			for(i = 0; i < SIZE; i++)
			{
				cout << i << ". " << myAccount[i].lastName << ", " << myAccount[i].firstName << "\n";
			};
			cout << "Select customer number:\n\n";
			cin >> i;

			//cout << myAccount[i].amount;
			
			break;
		case 'd':
		case 'D':
		cout << "You have chosen to quit, we appreciate your business.";
		terminate = true;
			break;
		default:
		cout << "You have entered an invalid option, please try again.\n\n";
			break;
		}
		break;

		case 'c':
		case 'C':
		cout << "Welcome to So-and-So Bank Supervising Services!\n" << "Please enter an option:\n\n";
		//Cascading banker and customer data
		//a.Everything in Bank Teller Interface, plus
		cout << "A)Ability to List Total Number of Customers in Bank\n"
		<< "B)Ability to List Total Amount of Money in the Bank\n"
		<< "C)Ability to List Total Deposits in a Day\n"
		<< "D)Ability to List Total Withdrawals in a Day\n"
		<< "E)Ability to see/query detailed LOG of all transactions\n"
		<< "F)Quit the application\n\n";
		cin >> choice;

		switch(choice)
		{
		case 'a':
		case 'A':
			//for loop count customers
			break;
		case 'b':
		case 'B':
			//for loop count money 
			break;
		case 'c':
		case 'C':
			//for loop count deposits (amount?)
			break;
		case 'd':
		case 'D':
			//for loop count withdrawals (amount?)
			break;
		case 'e':
		case 'E':
			//enum results from b,c, and d. for loop allow search
			break;
		case 'f':
		case 'F':
		cout << "You have chosen to quit, we appreciate your business.";
		terminate = true;
			break;
		default:
		cout << "You have entered an invalid option, please try again.\n\n";
			break;
		}
		break;

		case 'd':
		case 'D':
		cout << "You have chosen to quit, we appreciate your business.";
		terminate = true;
		break;

		default:
		cout << "You have entered an invalid option, please try again.\n\n";
		break;
		}

	}
return 0;
}
you can use something like
1
2
3
4
5
6
7
8
9
ofstream fout; // you declare an instance of the ofstream class 

fout.open("your file name"); //this opens your file 

int x=4;

fout << x; // you add 4 in your txt file
 
fout.close();  // when you finish you should call this method. 


For reading from a file you use

1
2
3
4
5
6
7
8
9
ifstream fin; 

int x;

fin.open("the same file as above");

fin >> x; // now x == 4; :) considering the example above

fin.close();


You need to #include "fstream"
Last edited on
And do I need to do this every time I declare or assign values to variables throughout the program?
Yes. Except for an int x, you should be writing and reading customerInfo data to/from file.

One simple way to do it would simply to write each field of the customerInfo to its own line --meaning that every four lines would be for a single customer. You might even want to add a blank line between customers, just to make the file more human-readable.
1
2
3
4
5
6
7
8
9
10
John
Jones
12.80
checking

Mary
Smith
4279.15
savings
etc.

Now, all you need to do is write some functions to read and write a customerInfo structure. Since you are using C++, you can overload the << and >> stream operators:
1
2
3
4
5
6
7
8
9
10
11
12
13
ostream& operator << ( ostream& outs, const customerInfo& ci )
  {
  outs << ci.firstName << endl;
  ...  // Fill in the rest here
  return outs;
  }

istream& operator >> ( istream& ins, customerInfo& ci )
  {
  ins.getline( ci.firstName, sizeof( ci.firstName ) );
  ... // Fill in the rest here
  return ins;
  }

After that, you can load the entire file into a std::deque (or std::vector, or std::list, etc), make changes, then write the entire thing back out to file.

Hope this helps.
Last edited on
I'm a little vague about overloading, why do I do this?
If you are unsure, write functions to do it instead.
1
2
void write_customerInfo( ostream& outs, const customerInfo& ci );
void read_customerInfo( istream& ins, customerInfo& ci );


Overloading is so you can do stuff like:
1
2
3
4
5
deque <customerInfo> customers;

customerInfo customer;
myfile >> customer;  // here's the overloaded extraction (input) operator
customers.push_back( customer );
instead of:
1
2
3
4
5
deque <customerInfo> customers;

customerInfo customer;
read_customerInfo( myfile, customer );
customers.push_back( customer );

Hope this helps.
Topic archived. No new replies allowed.