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:
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
usingnamespace std;
class Course {
public:
string get_name() const {
return name;
}
int get_students() const {
return students;
}
virtualvoid read(ifstream& infile) {
infile >> name >> students;
}
protected:
string name;
int students;
};
class Lecture : public Course {
public:
int get_num() {
return num;
}
virtualvoid 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.
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").
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)
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.
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?
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:
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.