Need some advice on input data

The current input in the text file is:
IBM 100 93.2 98.6
MER 200 67.2 43.89
QQQ 300 78.9 70.0
C 200 35.78 50.02
CSCO 400 45.67 57.23


From left to right it's stock name, number of shares, price bought at and current price.

Is it correct to read each row like this..? I've no idea how to do it. I need to somehow use the data from the text and use it to find the profit gain/loss.

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
int numrec; //Given
string stockName[50]; //Given
double numShares[50], buyPrice[50], currPrice[50], pctPL[50],dlrPL[50]; //Given

ifstream inFile;
ofstream outFile;

inFile.open("C:\\text1in.txt");
outFile.open("C:\\text2out.txt");


for(int i=0; i<4; i++)
{
inFile>>stockName[i]>>numShares[i]>>buyPrice[i]>>currPrice[i];
}

inFile.close();
outFile.close();

return 0;
}
Does it work? Are you getting expected output at runtime?

Okay, obviosly you don't have any output yet. It looks like you're reading the file correctly.
Why do you have ofstream outFile? Are you going to use an output file later, and just haven't gotten to it yet?
Last edited on
Why do you have ofstream outFile? Are you going to use an output file later, and just haven't gotten to it yet?


Yeah, I haven't gotten to the output yet. I was wondering if I was doing the input right because when I tried doing cout<<inFile<<endl; nothing happens.

Trying to get the output to look something like this, but I have to get the values from input to work first. Any suggestions?

Stock # Shares Buy Price Current Price $ P/L % P/L

==================================================================
IBM 100.00 93.20 98.60 540.00 5.79
MER 200.00 67.20 43.89 ( 4662.00)( 34.69)
QQQ 300.00 78.90 70.00 ( 2670.00)( 11.28)
C 200.00 35.78 50.02 2848.00 39.80
CSCO 400.00 45.67 57.23 4624.00 25.31
Last edited on
Hmm.. do you need a nested loop to read each row/col?
Topic archived. No new replies allowed.