I'm working on a program that reads in from a file a list of customers and puts them in a vector. I did that. Next, there is a file for each customer that I put in this vector. I need to open the file, and "assign the data to a new instance of the class in the vector." I don't really know what this means. I have a class of stocks, where a stock consists of three arguements, a symbol, amount of stock and price. My class looks like this:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class stock {
private:

string symbol;
int numshare;
float price;

public:
	stock(string symbol1, int numshare1, float price1);
	stock();
};

There are other things in the class but I believe this is all that is important for my problem.
The files I am reading in look something like this:
CSCO 100 12.34
MSFT 200 56.78
where the numbers are the symbol, followed by numshare, follewed by price.
This is what I have been trying, and it hasn't been working:

for(int j = 0; j<namevector.size(); j++){
	otherfiles.open(namevector[j].c_str());
	while(!otherfiles.eof()){
		otherfiles >> symbol >> numshare >> price;
		if (!otherfiles.eof()){
			stockvector.push_back(symbol, numshare, price);
		}
	}

	otherfiles.close();
	otherfiles.clear();
	}

where namevector is my vector of customernames, they are the names of the files I have to open.
I'm really sorry, I know this probably isn't very clear, but it's the best job of explaining i can do.

Member Avatar for iamthwee

Hi sarahger9

You have to create a vector or stock objects.

Additionally using eof to control file i/o is flawed.

I would begin by first trying to read in a line, then break it up into three parts which represent the three attributes of your class.

Ok, so I would do something like

vector <stock> stockvector;

read in the file,

stockvector.push_back(stock);

??

but could you help me with declaring the stock from my file. Would I just do something like,

otherfiles >> symbol >> numshares >> price;
stock(symbol, numshares, price);

I'm sorry, I'm having a lot of trouble with vectors and classes and can't find many good resources to help me, but thank you for your help.

Member Avatar for iamthwee

I would recommend asking your tutor for help. There are many, many ways to do this.

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.