I am writting a program that makes use of a vector of objects. Every time I add an object the windows system encounters a problem and needs to be closed.

#include<iostream>
#include<fstream>
#include<string>
#include<vector>

using namespace std;

class Catalogue
{
public:

	string vendor;
	string item;
	float price;
	int code;
	ifstream in;
	
	Catalogue()
	{
		in.open("catalogue.txt",ios::in);
	}
public:
	void read()
	{
		in>>vendor>>item>>price>>code;
	}
};

void main()
{
	Catalogue cat;
	Catalogue &cats = cat;
	vector<Catalogue> catalogue;
	string s;
	while(!cat.in.eof())
	{
		cat.read();
		catalogue.push_back(cat);
		cout<<catalogue.size();
		
	}
	for(int counter = 0; counter != catalogue.size(); counter++)
	{
		//cout<< catalogue[counter].vendor<<" - " << catalogue[counter].item <<" - " << catalogue[counter].price<<" - "<< catalogue[counter].code <<'\n';
	}
}

I cannot see why it is crashing, can a fresh pair of eyes perhaps shed light

Amny thanls

Recommended Answers

All 7 Replies

The push_back() method will create a duplicate copy of the class Catalog, and the class constructor gets called and the constructor opens a file. That means the file gets opened every time push_back is called, and eventually the program will exhaust all available file handles.

Why don't you try removing the file handling stuff from that class and add a simple >> overloaded methods. Declare ifstream in in main() then it can do something like this:

ifstream in("catalogue.txt");
Catalog cat;
while( in >> cat)
{
    catalogue.push_back(cat);
}

I am afraid not. The specification insisted the Catalogue class constructor opens the the file ("catalogue.txt") I have adjusted the code and even after having ommited the while loop to increment the catalogue vector, the program produces the windows crash asking if I want to send Microsoft error report. After the catalogue.push_back(cat) it does so once and then proceeds to crash.

#include<iostream>
#include<fstream>
#include<string>
#include<vector>

using namespace std;

class Catalogue
{
public:

	string vendor;
	string item;
	float price;
	int code;
	ifstream in;
	
	Catalogue()
	{
		if(in.is_open())
		{
			read();
		}
		else
		{
			in.open("catalogue.txt",ios::in);
			read();
		}
	}
I am 	void read()
	{
		in>>vendor>>item>>price>>code;
	}
};

void main()
{
	Catalogue cat;
	vector<Catalogue> catalogue;
	string s;
	int counter = 0;
	int line = 0;
	//while(!cat.in.eof())
	{		
		catalogue.push_back(cat);	
	}
	//for(int counter = 0; counter != catalogue.size(); counter++)
	{
		//cout<< catalogue[counter].vendor<<" - " << catalogue[counter].item <<" - " << catalogue[counter].price<<" - "<< catalogue[counter].code <<'\n';
	}
}

Are you using an IDE?
Did you try running debug?

I am using visual c++, does that make a difference? To you does that code look right, it certainly compiles with no objections and runs the first time and despite commenting out the loop it executes and displays the first line of the the txt file, then the windows crash message. It is obviously the vector that is causing the problem. If I ommit it, the program runs but obviously does nothing.

I have changed the vector to a string vector and attempted to loop the push_back function and it works without crashing, it is only the vector of objects i.e. catalogue objects.

Any more ideas?

It might.
I was was surprised that it crashed so bad that you got the "send to MS" message. IDE's usually contain that stuff in a shell.

Anyway, I'm a bit shaky on this topic, but I googled read(). I takes a char * and streamsize n. It returns a *this pointer.
Push_back might not like the pointer?

Also, why cant you break the class into more public methods?

Oh, and I forgot one thing.

How does it compile something like:
I am void read() ???????

I will attmept to do it on another system, perhaps mine is precarious or some such. I think the logic is right and trying to answer the same question in programming twice and differently is very difficult. My mind is already corrupted by my solution I do not seem able to come up with some thing completely different.

The instruction insists the class constructor must open the file so there is no option of opening it elsewhere.

I will report my findings after I have tested it elsewhere. If it is the same result then there is something chronically wrong with my code.


Many thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.