How to read from a file, stored the data in an array, and return number of records read back to main

T

tyeung168

I am taking a C++ class in college and having problems completing last
week's homework. Any help is greatly appreciated.

The program is to read from a filing containing stock trades. Here is
an example:

CSCO 200 92.5 100.3
QQQ 250 24.52 21.52
DELL 600 25.45 27.52

The fields are stock name, number of shares, purchase price and
current price. These field should be stored as arrays. After
processing the file the function should send number of records back to
main. The program should be written regardless of number of lines in
the file.

Here is the line from main: numrec = readData(stockname, numShares,
buyPrice, currPrice);

I don't know how to complete the function to complete this.

Again any help is greatly appreciated.
 
O

osmium

I am taking a C++ class in college and having problems completing last
week's homework. Any help is greatly appreciated.

The program is to read from a filing containing stock trades. Here is
an example:

CSCO 200 92.5 100.3
QQQ 250 24.52 21.52
DELL 600 25.45 27.52

The fields are stock name, number of shares, purchase price and
current price. These field should be stored as arrays. After
processing the file the function should send number of records back to
main. The program should be written regardless of number of lines in
the file.

Here is the line from main: numrec = readData(stockname, numShares,
buyPrice, currPrice);

I don't know how to complete the function to complete this.

I am not sure I understand your question. It seems to be written as though
we can look over your shoulder and see what you are looking at and see what
numrec and readData are - for a small example.

The way to count records is to read each record and count them as you go
along. When EOF is reached, stop and do what you want with the datum you
have collected. If this suggests further questions, post them.

readData seems to be a function. To read a file it is going to need some
way of identifying a particular file, and no such way is shown in your
parameter list.
 
T

Thomas Matthews

I am taking a C++ class in college and having problems completing last
week's homework. Any help is greatly appreciated.

The program is to read from a filing containing stock trades. Here is
an example:

CSCO 200 92.5 100.3
QQQ 250 24.52 21.52
DELL 600 25.45 27.52

The fields are stock name, number of shares, purchase price and
current price. These field should be stored as arrays. After
processing the file the function should send number of records back to
main. The program should be written regardless of number of lines in
the file.

Here is the line from main: numrec = readData(stockname, numShares,
buyPrice, currPrice);

I don't know how to complete the function to complete this.

Again any help is greatly appreciated.

Write a simple snippet to test out:

std::string stock_name;
double share_qty;
double purchase_price;
double current_price;
//...
ifstream data_file;
//...
data_file >> stock_name;
data_file >> share_qty;
data_file >> purchase_price;
data_file >> current_price;
//...

Once this works, you could create a record class:
class Record
{
friend std::istream& operator>>(istream& inp,
Record& r);
//...
};
std::istream&
Record ::
operator>>(istream& inp,
Record& r)
{
inp >> r.stock_name;
//...
inp >> r.current_price;
return inp;
}

To read in a record:
ifstream data_file;
Record new_record;
//...
data_file >> new_record;

I suggest you violate the requirements and use std::vector<Record>
instead of arrays. Vectors expand as necessary at run-time.
Arrays are fixed size at run-time.

-- Thomas Matthews.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top