Inputting test data in file

Hey everyone. I created a portion of my program to create test data and insert into a file. Is there a better way to do this than using arrays?

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
Info info;
	
	//file opened called trans.txt for output at the beginning of file
	ofstream outAccounts("trans.txt", ios::out);
	//if file could not be opened
	if (!outAccounts)
	{
		cerr << "File could not be opened" << endl;
		exit(1);
	}

	const int num = 4;		//number of array elements
	//arrays to store test data
	int accountArray[num] = { 100, 300, 400, 700 };
	string lastNameArray[num] = { "Jones", "Smith", "Sharp", "Green" };
	string firstNameArray[num] = { "Alan", "MAry", "Sam", "Suzy" };
	double balanceArray[num] = { 348.17, 27.19, 0.00, -14.22 };
	//inputting test data in file
	for (int i = 0; i < num; ++i)
	{
		info.setAccountNumber(accountArray[i]);
		info.setLastname(lastNameArray[i]);
		info.setFirstName(firstNameArray[i]);
		info.setBalance(balanceArray[i]);
		outAccounts << info.getAccountNumber() << " " << info.getLastName() << " "
			<< info.getFirstName() << " " << info.getBalance() << endl;
	}
Last edited on
You haven't shown the declaration for your Info class, so I'm going to have tomake some assumptions.

Lines 21-24: You will overwrite info each time through the loop, leaving info with on the last set of information when you exit the loop.

Line 25: This will appear correct since you're setting and writing info in the same iteration of the loop. As noted above, when you exit the loop, info will contain only the last set of information.

You can initialize an array of info using initializers. No need for your arrays at lines 14-17.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string> 
using namespace std;

const int MAX_ACCOUNTS = 4;
struct Info  
{   int account; 
    string lastname;
    string firstname;
    double balance; 
};

Info info[MAX_ACCOUNTS] = {
  { 100, "Jones", "Alan", 348.17 }, 
  { 300, "Smith", "Mary", 27.19 },
  { 400, "Sharp", "Sam", 0.00 }, 
  { 700,  "Green", "Suzy", -14.22 } 
};

So I would be assigning the data to a specific Info object? How would I then add that to a file?

I have the class constructor:

1
2
3
4
5
6
7
Info::Info(int _account, string _lastName, string _firstName, double _balance)
{
	setAccountNumber(_account);
	setLastName(_lastName);
	setFirstName(_firstName);
	setBalance(_balance);
}


that could store the data, not sure how I would bridge the gap to store it in the file though
Similar to what you had above:
1
2
3
4
5
6
7
    for (int i = 0; i < MAX_ACCOUNTS; ++i)
    {    outAccounts << info[i].getAccountNumber() << " " 
                              << info[i].getLastName() << " "
	              << info[i].getFirstName() << " " 
                              << info[i].getBalance() 
                              << endl;
    }


Note that the above code assumes you have exactly MAX_ACCOUNTS. This is generally a poor assumption. It works in this case because you have hard-coded exactly four initializers. However, if you change the program to accept the data from the user or read it from a file, the number of records you get may be less than the the number of info instances allocated.
yea understood. This problem came about after I had already written the code in which the user enters the data. Thanks for the replies, helped a lot! :)
Topic archived. No new replies allowed.