Hello. I am trying to read data from a file into 2 different arrays. I store the company name, symbol, and price into a class array named stocks. Then I store the frequency into and array name freq. The problem is I can't seem to read the file correctly. I used Visual Studio's debugger and it looks like every element in the stock array is the same, and for some reason it only reads the first frequency in the file and not the rest.
Here are a few lines from the file I'm reading:
Code:
Apple Inc.
AAPL 114.95
0.1
Amazon.com, Inc.
AMZN 15.46
0.1
And then here is the code I am having trouble with:
Code:
ifstream file;
string name;
string symbol;
double price;
double frequency;
const int numStocks = 10;
Stock stocks[numStocks];
double freq[numStocks];
//read file into arrays
file.open("stock.txt");
if (!file)
cout << "Can't open file\n";
int i = 0;
while (getline(file, name))
{
stocks[i].setName(name);
file >> symbol;
stocks[i].setSymbol(symbol);
file >> price;
stocks[i].setPrice(price);
file >> frequency;
freq[i] = frequency;
//file.ignore();
file.ignore(1000, '\n');
++i;
}
file.close();
Any help is appreciated.