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

Discussion in 'C++' started by tyeung168@gmail.com, Feb 26, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. osmium Guest

    <> 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.
     
    osmium, Feb 26, 2007
    #2
    1. Advertising

  3. Re: How to read from a file, stored the data in an array, and returnnumber of records read back to main

    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
     
    Thomas Matthews, Feb 27, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wl
    Replies:
    2
    Views:
    606
    Dimitri Maziuk
    Mar 5, 2004
  2. Luke Airig
    Replies:
    0
    Views:
    821
    Luke Airig
    Dec 31, 2003
  3. Replies:
    3
    Views:
    942
    Frederick Gotham
    Nov 30, 2006
  4. Army1987

    int main(void) { return main(); }

    Army1987, Mar 29, 2007, in forum: C Programming
    Replies:
    37
    Views:
    1,484
    Daniel Rudy
    Apr 3, 2007
  5. Ronald S. Cook
    Replies:
    3
    Views:
    3,176
    Alexey Smirnov
    May 6, 2007
Loading...

Share This Page