By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,146 Members | 1,610 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,146 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Feb 26 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
<ty*******@gmail.comwrote:
>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.
Feb 26 '07 #2

P: n/a
ty*******@gmail.com wrote:
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
Feb 27 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.