File I/O loop

I'm trying to write data of a struct to a file in a loop until a sentinel value is reached.

The instructions are to:
"Alter the program to include more than one record as input. Use an array of records."


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
 // This program reads in from the keyboard a record of financial information
// consisting of a person's name, income, rent, food cost, utilities and
// miscellaneous expenses. It then determines the net money
// (income minus all expenses)and places that information in a record
// which is then written to an output file.

#include <fstream>
#include <iostream>
#include <iomanip>

using namespace std;

const int NAMESIZE = 15;
struct budget //declare a structure to hold name and financial information
{
	char name[NAMESIZE + 1];
	float income;				// person's monthly income
	float rent;					// person's monthly rent
	float food;					// person's monthly food bill
	float utilities;			// person's monthly utility bill
	float miscell;				// person's other bills
	float net;					// person's net money after bills are paid
};

int main(){

	fstream indata;
	ofstream outdata; // output file of student.
	char response;


	indata.open("income.dat", ios::out | ios::binary); // open file as binary output.
	outdata.open("student.out"); // output file that we will write student information to.

	outdata << left << fixed << setprecision(2); // left indicates left justified for fields

	budget person; //defines person to be a record

	cout << "Enter the following information" << endl;

		cout << "Person's name: ";
		cin.getline(person.name, NAMESIZE);
		cout << "Income :";
		cin >> person.income;

		// FILL IN CODE TO READ IN THE REST OF THE FIELDS: rent, food, utilities AND miscell TO THE person RECORD
		cout << "Rent :";
		cin >> person.rent;
		cout << "Food :";
		cin >> person.food;
		cout << "Utilities :";
		cin >> person.utilities;
		cout << "Miscell :";
		cin >> person.miscell;
		// find the net field
		person.net = person.income - (person.rent + person.food + person.utilities + person.miscell); // FILL IN CODE TO DETERMINE NET INCOME (income - expenses)
	
		// write this record to the file Fill IN CODE TO WRITE THE RECORD TO THE FILE indata (one instruction)

		indata.write((char*)&person, sizeof(person));
		indata.close();

		// FILL IN THE CODE TO REOPEN THE indata FILE, NOW AS AN INPUT FILE.
		indata.open("income.dat", ios::in | ios::binary);
		// FILL IN THE CODE TO READ THE RECORD FROM indata AND PLACE IT IN THE person RECORD (one instruction)
		indata.read((char*)&person, sizeof(person));
	
		// write information to output file
		outdata << setw(20) << "Name" << setw(10) << "Income" << setw(10) << "Rent"
			<< setw(10) << "Food" << setw(15) << "Utilities" << setw(15)
			<< "Miscellaneous" << setw(10) << "Net Money" << endl << endl;

	
		// FILL IN CODE TO WRITE INDIVIDUAL FIELD INFORMATION OF THE RECORD TO
		// THE outdata FILE.(several instructions)
		outdata << setw(20) << person.name << setw(10) << person.income << setw(10) << person.rent
			<< setw(10) << person.food << setw(15) << person.utilities << setw(15)
			<< person.miscell << setw(10) << person.net << endl << endl;
	
	return 0;
}


This is the loopless version.

I don't understand how to approach this problem with an array, as the index is undefined and could keep going up.
I don't understand how to approach this problem with an array, as the index is undefined and could keep going up.

This one of the limitations of statically allocated arrays, their sizes are fixed at compile time. So your options are:

1. Use a fairly large array making sure you limit the data entry so as not to overflow the bounds of the array.

2. Use messy error prone dynamic memory management with new/delete. Allocate a dynamic array of a fairly large size and allocate more memory if required. You'll probably need to copy the existing data into the newly allocated memory and don't forget to properly delete the memory when finished.

3. Probably the safest way would be to use a std::vector, a std::vector can grow to accommodate your needs as required.

I would suggest using either #3 or #1, in that order. I would recommend against using manual dynamic memory until you much more familiar with the language.

Topic archived. No new replies allowed.