Having trouble reading content from to txt files and writing to a third. Using vectors

Basically need to read information from two files and write to a third one. Each file has specific types of information that I must cover in classes. Here is my code:

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
  #include <iostream>
  #include <string>
  #include <vector>
  #include <fstream>
  #include <algorithm>
  using namespace std;

  class Course {
  public:
	string get_name() const {
		return name;
	}
	int get_students() const {
		return students;
	}
	virtual void read(ifstream& infile) {
		infile >> name >> students;
	}
  protected:
	string name;
	int students;
  };

  class Lecture : public Course {
  public:
	int get_num() {
		return num;
	}
	virtual void read(ifstream& infile) {
		Course:: read (infile);
		infile >> num;
	}
	void write(ostream& outfile) {
		outfile << get_name() << " " << get_students() << num << "n";
	}
	
  private:
	int num;

  };

  class Lab : public Course {
  public:
	int get_numberofpc(){
		return numberofpc;
	}
	void read(ifstream& infile) {
		Course::read(infile);
		infile >> numberofpc;
	}
	void write(ostream& outfile) {
		outfile << get_name() << " " << get_students() << numberofpc << 
  "n";
	}

  private:
	int numberofpc;
  };



  int main()
  {
	vector <Lecture* > lec;
	vector <Lab* > lab;
	int lec_count = 0;
	int lab_count = 0; 

	string lecCourse;
	cout << "First file: " << "\n";
	cin >> lecCourse;
	ifstream inf;
	inf.open(lecCourse.c_str());
	if (inf.fail()) {
		cout << "No such file - First file" << "\n";
		return 1;
	}
	else {
		int temp;
		inf >> temp;
		for(int i = 0;i<temp;i++)
		{
			lec[lec_count] = new Lecture();
			lec[lec_count]->read(inf);
			lec.push_back(new Lecture());
			lec_count++;
		}
		inf.close();
	}


	string lab_ex;
	cout << "Second file: " << "\n";
	cin >> lab_ex;
	ifstream inn;
	inn.open(lab_ex.c_str());
	if (inn.fail()) {
		cout << "No such file - Second file" << "\n";
		return 1;
	}
	else {
		int temp;
		inn >> temp;
		for (int i = 0; i < temp; i++){
			lab[lab_count] = new Lab();
			lab[lab_count]->read(inn);
			lab.push_back(new Lab());
			lab_count++;
		}
		inn.close();
	}

	for (int i = 0; i < lab_count; i++)
	{
		lab[i]->write(cout);
	}
	for (int i = 0; i < lec_count; i++) {
		lec[i]->write(cout);
	}
	string exit;
	cout << "Exit file: " <<"\n";
	cin >> exit;
	ofstream output;
	output.open(exit.c_str());
	if (output.fail()) {
		cout << "No such file - Third file" << "\n";
		return 1;
	}
	else {
		for (int i = 0; i < lec_count; i++)
		{
			lec[i]->write(output);
		}
		for (int i = 0; i < lab_count; i++)
		{
			lab[i]->write(output);
		}
		output.close();
	}
        }


I am 99% sure my mistake is somewhere in here:
1
2
3
4
5
6
7
  for(int i = 0;i<temp;i++)
  {
    lec[lec_count] = new Lecture();
    lec[lec_count]->read(inf);
    lec.push_back(new Lecture());
    lec_count++;
  }


but I just cannot figure it out. Am i actually reading and writing anything in my new Lecture()? Any help would be greatly appreciated. Sorry for posting my whole code, just wanted to show all the functions I am using. And yes, i know there is a much more simple way to do it without those classes, but it's a requirement.

The error itself is that nothing gets written to the output text file.No syntax or compilation errors.
Hello,

I think it might help if you also provide an example of the files you are working with and a description of what you see is currently happening (hopefully a bit more detailed than "I don't get the expected result").

Kind regards, Nico
Alright.
So here's an example of the first file:
CITB201 80 40-1
CSCC203 70 40-2
CCBT101 4 55-1
...
And so on. Can have any number of lines in it.
CITB201 = name
80 = students
40-1 = num (Lecture)

Second file:
CITB21 80 7-3
CSCC5 70 40-2
CCBT3 4 55-1
...

CITB201 = name
80 = students
7-3 = numberofpc (Lab)

Basically i need to combine those two files, using the variables from my classes.
The problem is that nothing gets written into my third file (output) where thе information from those two files is supposed to go.
Last edited on
Also I see quite a few "virtual" member functions, but no constructors, destructors, copy, assignment operators. Your compiler should be warning you about these issues.

Also I see a whole bunch of "new" without any deletes (why all the pointers?).

IMO, your attempt at inheritance doesn't really make much sense, perhaps you may want to rethink the purpose of the inheritance.



Tell my about it. I'm a second year college student in a private univ in my country (the most expencive one). Like 90% of this code is copied form an example code that has been given by a teacher. It somehow works for him
As already stated you need to post your example input files, show the output your program is producing with this input and show the expected output with that input.

It somehow works for him

I'd guess that depends on what you mean by "works". Perhaps that "code" was meant for a starting framework and that you need to add to meaningful content to that framework?

Last edited on
I expect your teacher has explained to you the use of functions and tat they help us to write better code by splitting the project goal into smaller more manageable problems. This does not only apply for functions, it applies for all problems you encounter when you are coding.

You seem to be trying to debug the complete program and you seem to have difficulty doing so. So you should apply the same logic to this coding related problem: reduce the size of your program and make sure that the reduced program is doing what you expect it to do. Only if the reduced program is working properly can you activate a new part of it. When you go through this process, always start at the beginning of the program, because shit in guarantees shit out.

For me, the first "strange" thing in this code occurred when I I reached this point:
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
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;

/*
class Course
{
public:
    string get_name() const
    {
        return name;
    }
    int get_students() const
    {
        return students;
    }

    virtual void read(ifstream& infile)
    {
        infile >> name >> students;
        cout << name << "\t" << students << "\n";
    }
protected:
    string name;
    int students;
};

class Lecture : public Course
{
public:
       int get_num()
       {
           return num;
       }

    virtual void read(ifstream& infile)
    {
        Course:: read (infile);
        infile >> num;
        cout << num << "\n";
    }

    void write(ostream& outfile)
    {
        outfile << get_name() << " " << get_students() << num << "n";
    }

private:
    int num;

};

class Lab : public Course {
  public:
	int get_numberofpc(){
		return numberofpc;
	}
	void read(ifstream& infile) {
		Course::read(infile);
		infile >> numberofpc;
	}
	void write(ostream& outfile) {
		outfile << get_name() << " " << get_students() << numberofpc <<
  "n";
	}

  private:
	int numberofpc;
  };
*/

int main()
{
//  vector <Lecture* > lec;
//	vector <Lab* > lab;
    int lec_count = 0;
//    int lab_count = 0;

    string lecCourse = "file1.txt";
    // cout << "First file: " << "\n";
    // cin >> lecCourse;
    ifstream inf;
    inf.open(lecCourse.c_str());
    if (inf.fail())
    {
        cout << "No such file - First file" << "\n";
        return 1;
    }
    else
    {
        int temp;
        inf >> temp;
        cout << "temp = " << temp << "\n";
        /*
                for(int i = 0; i<temp; i++)
                {
                    lec[lec_count] = new Lecture();
                    lec[lec_count]->read(inf);
                    lec.push_back(new Lecture());
                    lec_count++;
                }
                inf.close();
            }

            	string lab_ex = "file2.txt";
            	// cout << "Second file: " << "\n";
            	// cin >> lab_ex;
            	ifstream inn;
            	inn.open(lab_ex.c_str());
            	if (inn.fail()) {
            		cout << "No such file - Second file" << "\n";
            		return 1;
            	}
            	else {
            		int temp;
            		inn >> temp;
            		for (int i = 0; i < temp; i++){
            			lab[lab_count] = new Lab();
            			lab[lab_count]->read(inn);
            			lab.push_back(new Lab());
            			lab_count++;
            		}
            		inn.close();
            	}

            	for (int i = 0; i < lab_count; i++)
            	{
            		lab[i]->write(cout);
            	}
            	for (int i = 0; i < lec_count; i++) {
            		lec[i]->write(cout);
            	}
            	string exit;
            	cout << "Exit file: " <<"\n";
            	cin >> exit;
            	ofstream output;
            	output.open(exit.c_str());
            	if (output.fail()) {
            		cout << "No such file - Third file" << "\n";
            		return 1;
            	}
            	else {
            		for (int i = 0; i < lec_count; i++)
            		{
            			lec[i]->write(output);
            		}
            		for (int i = 0; i < lab_count; i++)
            		{
            			lab[i]->write(output);
            		}
            		output.close();
            	}
            */
    }
}


Please let us know what you noticed and what you plan to do or did about it.

Kind regards, Nico

P.s. file1.txt and file2.txt each contain the three lines that you provided, I just did not feel like typing the file name multiple times.
Having Lecture inherit from Course looks weird. Inheritance models an "is-a" relationship. A lecture isn't a course.
At line 80 you try to read an int from the start of your 'inf' -stream, but in the file content which you showed to us, there is no integer at the beginning of it. Hence the read-in fails and 'inf' falls into 'fail' state. If you would keep care about your filestream-states at each operations on it, you would have detected that.
Last edited on
Topic archived. No new replies allowed.