I have an assignment and I just and stuck and do not know where to start. The assignment is as follows:
____________________________________________
Write a C++ program that reads a file that holds certain stock information. The input data file looks something like:
**(the text file has 10 companies, each with abbreviaition and w/ 7 stock prices for each)**
International Business Machines
IBM
35.67
36.79
39.66
32.09
31.67
31.67
34.76
Epson Manufacturing Corporation
EPS
31.99
28.87
24.60
29.65
31.33
32.67
34.08
Company Name
3-letter stock exchange ID
Value for Sunday
Value for Monday
Value for Tuesday
Value for Wednesday
Value for Thursday
Value for Friday
Value for Saturday
The file will have no more than 10 records.
You will need to store the data in two separate (parallel) arrays. Store the company names and IDs in a 2-d array of type string. Store the values in a 2-d array of type float. That is, the string array should have dimensions [10][2] so it will have 10 rows and 2 columns numbered 0,1,2,3,4,5,6,7,8,9 and 0,1 respectively. The numeric array should have dimensions of [10][7] so it will have 10 rows and 7 columns, numbered 0,1,2,3,4,5,6,7,8,9 and 0,1,2,3,4,5,6 respectively.
Phase 1:
Your program should compute the 7-day average for each company’s stock and also the day’s total sum of values for each day. Use manipulators so your output is neat and lined up correctly. An example of your preliminary report follows:
Company ID Closing Price
----------------------------------------------------------
International Business Machines IBM 34.56
Epson Manufacturing Co. EPS 42.36
. . . . . . . . . . . . ... .....
Company Name XXX 99.99
----------------------------------------------------------
Total 999.99
10 1479
Here is my failed attempt: I was frustrated b/c I counld not get the file to input so I manually typed everything in! - #include <iostream>
-
#include <string>
-
#include <fstream>
-
#include <iomanip>
-
using namespace std;
-
-
-
-
-
-
int main()
-
{
-
ifstream inFile;
-
inFile.open("stock_data.txt");
-
if (inFile.fail())
-
{
-
cout << "Input file open failed.\n";
-
cout << "Call for help.\n";
-
exit (0);
-
}
-
-
const int NUM_ROWS = 10;
-
const int NUM_COLS = 2;
-
const int NUM_COLSS = 7;
-
string company[NUM_ROWS][NUM_COLS] = {"International Business Machines", "IBM", "Earth Bio Fuels dba Bio-Willie", "EBF",
-
"Hewlett-Packard Corporation, HPQ", "Harley-Davidson Inc.", "HOG",
-
"Apple Computer Inc., APL", {J.C. Penney Company, JCP}, {Wal-Mart Stores, WMT},
-
{Exxon-Mobil Corporation, XOM}, {McDonalds Food Stores, MCD}, {Dell Computer, DEL}};
-
-
int quotes[NUM_ROWS][NUM_COLSS] = {{99.54, 97.11, 97.24, 98.45, 99.01, 99.20, 100.65},
-
{6.49, 5.98, 5.34, 2.87, 2.32, 1.87, 0.68},
-
{42.02, 42.05, 42.34, 42.56, 39.88, 41.54, 43.60},
-
{71.62, 70.67, 71.98, 72.25, 71.15, 72.22, 72.23},
-
{86.79, 84.33, 82.89, 89.82, 91.32, 85.43, 86.78},
-
{82.21, 83.56, 81.87, 70.21, 82.54, 83.21, 81.09},
-
{47.96, 47.99, 48.04, 48.67, 49.21, 48.34, 49.87},
-
{72.90, 73.01, 73.90, 75.32, 76.10, 79.87, 80.87},
-
{44.34, 43.43, 42.98, 44.91, 45.23, 44.01, 43.87},
-
{24.49, 23.87, 23.16, 21.67, 22.90, 22.34, 21.76}};
-
-
-
-
-
cout << "This program displays the stock information for 10 companies.\n";
-
cout << "Company ID Monday Tuesday Wednesday Thursday Friday Saturday Sunday Average.\n";
-
-
for (int count = 0; count < NUM_ROWS; count++)
-
{
-
cout << company[count] << endl;
-
cout << quotes[count] << endl;
-
}
-
return 0;
-
}
Here is my failed attempt: I was frustrated b/c I counld not get the file to input so I manually typed everything in!
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
ifstream inFile;
inFile.open("stock_data.txt");
if (inFile.fail())
{
cout << "Input file open failed.\n";
cout << "Call for help.\n";
exit (0);
}
const int NUM_ROWS = 10;
const int NUM_COLS = 2;
const int NUM_COLSS = 7;
string company[NUM_ROWS][NUM_COLS] = {"International Business Machines", "IBM", "Earth Bio Fuels dba Bio-Willie", "EBF",
"Hewlett-Packard Corporation, HPQ", "Harley-Davidson Inc.", "HOG",
"Apple Computer Inc., APL", {J.C. Penney Company, JCP}, {Wal-Mart Stores, WMT},
{Exxon-Mobil Corporation, XOM}, {McDonalds Food Stores, MCD}, {Dell Computer, DEL}};
int quotes[NUM_ROWS][NUM_COLSS] = {{99.54, 97.11, 97.24, 98.45, 99.01, 99.20, 100.65},
{6.49, 5.98, 5.34, 2.87, 2.32, 1.87, 0.68},
{42.02, 42.05, 42.34, 42.56, 39.88, 41.54, 43.60},
{71.62, 70.67, 71.98, 72.25, 71.15, 72.22, 72.23},
{86.79, 84.33, 82.89, 89.82, 91.32, 85.43, 86.78},
{82.21, 83.56, 81.87, 70.21, 82.54, 83.21, 81.09},
{47.96, 47.99, 48.04, 48.67, 49.21, 48.34, 49.87},
{72.90, 73.01, 73.90, 75.32, 76.10, 79.87, 80.87},
{44.34, 43.43, 42.98, 44.91, 45.23, 44.01, 43.87},
{24.49, 23.87, 23.16, 21.67, 22.90, 22.34, 21.76}};
cout << "This program displays the stock information for 10 companies.\n";
cout << "Company ID Monday Tuesday Wednesday Thursday Friday Saturday Sunday Average.\n";
for (int count = 0; count < NUM_ROWS; count++)
{
cout << company[count] << endl;
cout << quotes[count] << endl;
}
return 0;
}
I dont believe your doing anything with the file that your reading in?? It looks like you just have a statement saying what happens if it cant be read.
I guess that is where I get frustrated...I do not know how to make the file read out like it is suppose to. I know I have to set up the arrays and make so loops but ????
Well, you know the file is written in the format
Full Name
3-Letter Abbrev
Value1
Value2
Value3
Value4
Value5
Value6
Value7
So, in a loop (until end of file), first get the full name and store it into a string, then store that string in company[i][0], where i is the number of times the loop has been executed. Then get the abbreviation and store it in company[i][1]. Next, in a second loop, get each of the 7 values and store them in quotes[i][j], where j is the number of values you have read.
This was my other attempt:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
ifstream inFile;
inFile.open("stock_data.txt");
if (inFile.fail())
{
cout << "Input file open failed.\n";
cout << "Call for help.\n";
exit (0);
}
string company[10][2] = {""};
int quotes[10][7] = {0.0};
cout << "This program displays the stock information for 10 companies.\n";
cout << "Company ID Monday Tuesday Wednesday Thursday Friday Saturday Sunday Average << endl;
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 2; j++)
{
cout << company[i][j] << endl
}
}
return 0;
}
--------------------------------------------------------------------------------
Well, you know the file is written in the format
Full Name
3-Letter Abbrev
Value1
Value2
Value3
Value4
Value5
Value6
Value7
So, in a loop (until end of file), first get the full name and store it into a string, then store that string in company[i][0], where i is the number of times the loop has been executed. Then get the abbreviation and store it in company[i][1]. Next, in a second loop, get each of the 7 values and store them in quotes[i][j], where j is the number of values you have read.
***What does that look like in code?***
- int i = 0; // 0 companies have been read
-
while (!inFile.eof()) { // While there is still data to read
-
string compName, compAbbrev;
-
// Get company name and abbrev from inFile
-
company[i][0] = compName;
-
company[i][1] = compAbbrev;
-
for (int j = 0; j < 7; j++) {
-
float price;
-
// Get the float value from inFIle
-
stocks[i][j] = price;
-
}
-
i++; // Increment the number of companies read so far
-
}
- int i = 0; // 0 companies have been read
-
while (!inFile.eof()) { // While there is still data to read
-
string compName, compAbbrev;
-
// Get company name and abbrev from inFile
-
company[i][0] = compName;
-
company[i][1] = compAbbrev;
-
for (int j = 0; j < 7; j++) {
-
float price;
-
// Get the float value from inFIle
-
stocks[i][j] = price;
-
}
-
i++; // Increment the number of companies read so far
-
}
THANK YOU FOR YOUR HELP....I WILL TRY IT AND LET YOU KNOW! :)
So now what do I cout << ? (Here is the updated version)
:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
ifstream inFile;
inFile.open("stock_data.txt");
if (inFile.fail())
{
cout << "Input file open failed.\n";
cout << "Call for help.\n";
exit (0);
}
string compName, compAbbrev;
company[i][0] = compName;
int quotes[i][1] = compAbbrev;
stocks[i][j] = price;
cout << "This program displays the stock information for 10 companies.\n";
cout << "Company ID Monday Tuesday Wednesday Thursday Friday Saturday Sunday Average.\n";
for (int j = 0; j < 7; j++)
{
float price;
cout <<
// Get the float value from inFile
}
i++; // Increment the number of companies read so far
return 0;
}
Need last minute help with this program......Can someone code this darn thing for me in the mext hour??????? I have PayPal....:)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Jake |
last post by:
OK Ive got a huge amount of If Then statements that Im sure can be stripped
way down to a simple loop or two but Im not familiar enough with them to
know where to start.
Basically this is a truck...
|
by: manning_news |
last post by:
I've got the following query in SQL 2000:
select a.SSN, a.MonthName, a.IMClinicDay,
b.IMClinicDay as SecondDay
from tblResidentRotations a
inner join view7 b
on a.SSN = b.SSN
where...
|
by: Fran?ois Laroche |
last post by:
hey guys, I know that you will tell me that's easier to do in php or
even in asp, but I just dont have the time to learn it
1- I need a javascript that will show the date of tomorrow
2- I need a...
|
by: Dom Boyce |
last post by:
Hi
First up, I am using MS Access 2002.
I have a database which records analyst rating changes for a list of
companies on a daily basis. Unfortunately, the database has been set
up (by my...
|
by: Dennis Schulz |
last post by:
Hi all,
the following programm is supposed to check for login and password
with inbuilt linux functions. in line 57 and 64 there are erorrs:
incompatible types in asignment. whats wrong with...
|
by: vj |
last post by:
I'm doing:
a = now()
delta = ReltaiveDateTime(days=+6, weekday(mx.DateTime.Friday, 0))
Next Friday: a+delta
a: march 23
a+delta: Gives me March 31st and not March 24th
Any ideas?
|
by: Maximus |
last post by:
I need some help. I have an .asp page that interfaces with an Access
table (wjs_SuperInput). The .asp page looks to the table and pulls a
recordset based on a job number, 12345, and a weekending...
|
by: =?Utf-8?B?QWw=?= |
last post by:
I am working in vb2005. how can I calculate business days (not including
holidays and weekends) between 2 dates? thanks
Al
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |