build records for a class

Hi can someone help with this part of the question and show me how to build 3 records for the New_Records class.(or even a example that uses this?)
Thanks!

public
class New_Records {
int account;
String Name;
String lastName;
double minutes;
char status;

*Required
You must create a file of 3 records for the New_Records class.
- pipe in the data from a text file.

Last edited on
This code will read the records from the file, store them in a vector and then display the read 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
#include <string>
#include <iostream>
#include <vector>
#include <fstream>

struct New_Records {
	int account;
	std::string Name;
	std::string lastName;
	double minutes;
	char status;
};

std::istream& operator>>(std::istream& ifs, New_Records& nr)
{
	return ifs >> nr.account >> nr.Name >> nr.lastName >> nr.minutes >> nr.status;
}

std::ostream& operator<<(std::ostream& ofs, const New_Records& nr)
{
	return ofs << nr.account << " " << nr.Name << " " << nr.lastName << " " << nr.minutes << " " << nr.status;
}

int main()
{
	std::ifstream ifs("accts.txt");

	if (!ifs.is_open())
		return (std::cout << "Cannot open file\n"), 1;

	std::vector<New_Records> records;

	for (New_Records nr; ifs >> nr; records.push_back(nr));

	for (const auto& r : records)
		std::cout << r << '\n';
}


The accts.txt file is


1 first1 last1 1.1 a
2 first2 last2 2.2 b
3 first3 last3 3.3 c


and the program displays:


1 first1 last1 1.1 a
2 first2 last2 2.2 b
3 first3 last3 3.3 c

records and text files don't mix well, but if you are going to do that, I cannot recommend enough spitting out how many records you have up front (in this case, 3).

so your file looks like
3
record 1
record 2
record 3

double minutes; this really looks like an int value to me. you can have fractional min but people get frustrated reading 18.5 min (18 min 30 sec) or 11.25 etc. Beyond the simple fractions, 13.6521 min is what? You have to stare at it for a few moments to figure out what that means in terms of time and it will be approximate if you don't run it thru a calculator. (-> H.ms FTW HP11C !)

Last edited on
Topic archived. No new replies allowed.