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 {

string symbol;
int numshare;
float price;

	stock(string symbol1, int numshare1, float price1);

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 >> symbol >> numshare >> price;
		if (!otherfiles.eof()){
			stockvector.push_back(symbol, numshare, price);


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.

9 Years
Discussion Span
Last Post by 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,



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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.