Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   How to read from a file, stored the data in an array, and return number of records read back to main (http://www.velocityreviews.com/forums/t460737-how-to-read-from-a-file-stored-the-data-in-an-array-and-return-number-of-records-read-back-to-main.html)

tyeung168@gmail.com 02-26-2007 04:31 PM

How to read from a file, stored the data in an array, and return number of records read back to main
 
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.


osmium 02-26-2007 05:08 PM

Re: How to read from a file, stored the data in an array, and return number of records read back to main
 
<tyeung168@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.


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.



Thomas Matthews 02-27-2007 07:12 AM

Re: How to read from a file, stored the data in an array, and returnnumber of records read back to main
 
tyeung168@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


All times are GMT. The time now is 12:14 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.