New to c++ problem with small bit of code im working on

Trying to get this code to work but having no luck.Thank if anyone can show what i need to do.
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
#include <string>
#include <iostream>
#include <vector>
#include <fstream>

int getline(char *,int);
void pause();


char instr[84];
FILE *stream;
class Custome{
	double minutes;
	double sms;

	public:
		Custome(){//default constructor
			minutes = 0.0;
			sms = 0.0;
						
		}
			
		Custome(double minutes, double sms){//parameterized constructor
			this->minutes = minutes;
			this->sms = sms;
						
		}
	
		void set_minutes(){//mutator
			getline(instr,81);
			minutes = atof(instr);
		}
		double get_minutes(){return minutes;}//accessor
		
		void set_bonus(){
			getline(instr,81);
			sms = atof(instr);
		}
		double get_sms(){return sms;}
		
		~Custome(){};
};

class Comcast_Records : public Custome{
std::string firstName[81];
std::string lastName[81];
        int account;
std::string plan;
        char status;
public:
Comcast_Records(){};
 

friend std::istream& operator>>(std::istream& ifs, Comcast_Records& cr)
{
return ifs >> cr.firstName >> cr.lastName >> cr.account >> cr.minutes >> cr.plan >> cr.sms >> cr.status;
}
friend std::ostream& operator<<(std::ostream& ofs, const Comcast_Records& cr)
{
return ofs << cr.firstName << " " << cr.lastName << " " << cr.account << " " << cr.minutes << " " << cr.plan << " " << cr.sms << " " << cr.status;
}

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

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

	std::vector<Comcast_Records> records;

	for (Comcast_Records cr; ifs >> cr; records.push_back(cr));

	for (const auto& r : records)
		std::cout << r << '\n';
}
};
Last edited on
Does it compile?

If it does compile, does it crash?

If it doesn't crash, is "accts.txt" in the same directory (folder) as the running binary executable? Note that this might NOT be the same directory as your source code.
This will now compile, read the data file and display it's contents:

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
#include <string>
#include <iostream>
#include <vector>
#include <fstream>

class Custome {
protected:
	double minutes {};
	double sms {};

public:
	Custome() {}
	Custome(double min, double s) : minutes(min), sms(s) {}

	void set_minutes() {
		std::string instr;

		std::cout << "Enter minutes: ";
		std::getline(std::cin, instr);
		minutes = std::stod(instr);
	}

	double get_minutes() const { return minutes; }

	void set_bonus() {
		std::string instr;

		std::cout << "Enter sms: ";
		std::getline(std::cin, instr);
		sms = std::stod(instr);
	}

	double get_sms() const { return sms; }
};

class Comcast_Records : public Custome {
	std::string firstName;
	std::string lastName;
	int account {};
	std::string plan;
	char status {};
public:
	Comcast_Records() {};

	friend std::istream& operator>>(std::istream& ifs, Comcast_Records& cr);
	friend std::ostream& operator<<(std::ostream& ofs, const Comcast_Records& cr);
};

std::istream& operator>>(std::istream& ifs, Comcast_Records& cr)
{
	return ifs >> cr.firstName >> cr.lastName >> cr.account >> cr.minutes >> cr.plan >> cr.sms >> cr.status;
}

std::ostream& operator<<(std::ostream& ofs, const Comcast_Records& cr)
{
	return ofs << cr.firstName << " " << cr.lastName << " " << cr.account << " " << cr.minutes << " " << cr.plan << " " << cr.sms << " " << cr.status;
}

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

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

	std::vector<Comcast_Records> records;

	for (Comcast_Records cr; ifs >> cr; records.push_back(cr));

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


accts.txt

first1 last1 1 1.1 plan1 3.3 a
first2 last2 2 2.2 plan2 4.4 b
first3 last3 3 3.3 plan3 5.5 c


displays:

first1 last1 1 1.1 plan1 3.3 a
first2 last2 2 2.2 plan2 4.4 b
first3 last3 3 3.3 plan3 5.5 c

Topic archived. No new replies allowed.