473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.

Feb 26 '07 #1
2 2015
<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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Joseph | last post by:
Hi, I'm having bit of questions on recursive pointer. I have following code that supports upto 8K files but when i do a file like 12K i get a segment fault. I Know it is in this line of code. ...
19
by: ranjeet | last post by:
Hay Guys can you all suggest me the points on the below issue Problem : The thing is that I have the data some thing like this. 1486, 2168, 3751, 9074, 12134, 13944, 17983, 19173, 21190,...
11
by: kaisersose1995 | last post by:
Hi, I've got an import procedure working, using a standard import specification to import a .csv file into a temporary table. The problem i'm having is that i have 4 different sets of borrower...
9
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still...
7
by: ashtek | last post by:
Hi, I have a generic function that executes a stored procedure & returns a data table. Code: === public static DataTable ExecuteStoredProcedure(string strProc,SqlParameter paramArray) {...
3
by: phwashington | last post by:
I am new to C++ and have a data file I want to read, which was stored in binary. I have looked at the data with a hex editor and it appears to be correct. Whenever I try to read it though as an...
6
by: xdeath | last post by:
Hi guys, i've currently got an assignment, whereby, im supposed to create 2 classes, a Vehicle superclass, and a Taxi subclass. Vehicle class needs to have Reg Number, model, price, and Taxi is...
13
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.