| | |
Function problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2006
Posts: 4
Reputation:
Solved Threads: 0
Hi everyone
I am trying to get done the stocks program
the first function is readStocks, it compiles but deosnt give me rigth ptompt, i guess i have some kind of execution error.
Could any one give me advice on that?
THank you.
I am trying to get done the stocks program
the first function is readStocks, it compiles but deosnt give me rigth ptompt, i guess i have some kind of execution error.
Could any one give me advice on that?
THank you.
C++ Syntax (Toggle Plain Text)
//Stock's statistic #include <iostream> #include <stdlib.h> #include <fstream> using namespace std; const int MAXSTOCKS = 5; const int NUMDAYS = 5; ifstream infile; ofstream outfile; void printHeader(void); void readStocks(double [][NUMDAYS], string[], int & ); //void findStockAvg(double [][NUMDAYS], double[], int ); //void printStocks(double [][NUMDAYS], double[], string[], int ); //void findDayAvg(double [][NUMDAYS], double [], int ); //void printDayAvg(double [], int); //void findMinDayOfStock(double[][NUMDAYS], string[], int); //void findStockOfDay(double [][NUMDAYS], string [], int); int main() { double stocks[MAXSTOCKS][NUMDAYS]; string stockName[MAXSTOCKS]; double stockAvg[MAXSTOCKS]; double dayAvg[MAXSTOCKS]; int numStocks; infile.open("C:\\Documents and Settings\\Admin\\Desktop\\job_study\\college\\C++Home\\Stocks_1.txt"); //outfile.open("C:\Documents and Settings\Admin\Desktop\job_study\college\C++Home\Stocks_Report.txt"); outfile.open("con"); outfile.setf(ios::fixed, ios::floatfield); printHeader(); readStocks( stocks, stockName, numStocks); //findStockAvg(stocks, stockAvg, numStocks); //printStocks(stocks, stockAvg, stockName, numStocks); //findDayAvg(stocks, dayAvg,numStocks); //printDayAvg(dayAvg, numStocks); //findMinDayOfStock(stocks, stockName, numStocks); //findStockOfDay(stocks, stockName, numStocks); system("pause"); infile.close(); outfile.close(); return 0; } void printHeader(void) { outfile <<" Anton Deinega CIS 1.5 EMY6 Program 4 Due: November 20, 2006"<< endl; outfile <<"--------------------------------------------------------------------"<<endl; outfile <<"\t\t\tStock Prices"<<endl<<endl; outfile.precision(2); outfile.setf(ios::right); outfile<<" Stocks"; outfile.width(10); outfile<<"Day 1"; outfile.width(10); outfile<<"Day 2"; outfile.width(10); outfile<<"Day 3"; outfile.width(10); outfile<<"Day 4"; outfile.width(10); outfile<<"Day 5"; outfile.width(10); outfile<<"Average"<<endl; outfile <<"--------------------------------------------------------------------"<<endl; return; } void readStocks(double stocks[][NUMDAYS], string stockName[], int & numStocks ) { int numStocks =0; while (!cin.eof()) { infile>> stockName[numStocks]; for (int i=0; i<NUMDAYS;i++) infile>>stocks[numStocks][i]; numStocks++; } return; } /* void findStockAvg(double stocks[][NUMDAYS], double stockAvg[], int numStocks) { double sum; double StockAvg; for (int stockNum = 1; stockNum < NUMDAYS; stockNum++) { sum = 0; for (int dayNum = 1; dayNum < stockAvg[MAXSTOCKS]; dayNum++) { sum += stocks[stockNum][dayNum]; StockAvg = (double)sum/numStocks; } } return; } /* void printStocks(double stocks[][NUMDAYS], double stockAvg[], string stockName[], int numStocks) { //ofstream outfile("C:\Documents and Settings\Admin\Desktop\job_study\college\C++Home\StockReport.txt"); ofstream outfile("con"); outfile.setf(ios::fixed,ios::floatfield); outfile.precision(2); outfile.setf(ios::right); outfile.width(10); for (int stockNum = 0; stockNum < numStocks; stockNum++ ) { outfile << stockName[stockNum] << " "; for (int dayNum = 0; dayNum < NUMDAYS; dayNum++) { outfile << stocks[stockNum][dayNum] << " "; } outfile << endl; } return; } /* void findDayAvg(double stocks[][NUMDAYS], double DayAvg[], int numStocks) { double sum; double DayAvg; for (int stockNum = 1; stockNum < NUMDAYS; stockNum++) { sum = 0; for (int dayNum = 1; dayNum < MAXSTOCKS; dayNum++) sum += stocks[stockNum][dayNum]; DayAvg = (double)sum/numStocks; } return; } */ /* void printDayAvg(double dayAvg[], int numStocks) { //ofstream outfile("C:\Documents and Settings\Admin\Desktop\job_study\college\C++Home\StockReport.txt"); ofstream outfile("con"); outfile<<"------------------------------------------------------------------------------"<<endl; outfile<<"Day Avg"; for (int stockNum = 1; stockNum < NUMDAYS; stockNum++) { outfile<<dayAvg[numStocks]<<endl; } outfile.setf(ios::fixed,ios::floatfield); outfile.precision(2); outfile.setf(ios::right); outfile.width(10); return; } /* void findMinDayOfStock(double stocks[][NUMDAYS], string stockName[], int numStocks) { int smallest, dayNum; for (int dayNum = 1; dayNum < NUMDAYS; dayNum++) cout<<stockName[0]<<" had its smallest value on day "<< day <<" with a value of "<< smallest<<endl; cout<<stockName[1]<<" had its smallest value on day "<< day <<" with a value of "<< smallest<<endl; cout<<stockName[2]<<" had its smallest value on day "<< day <<" with a value of "<< smallest<<endl; cout<<stockName[3]<<" had its smallest value on day "<< day <<" with a value of "<< smallest<<endl; return; } /* void findStockOfDay(double stocks [][NUMDAYS], string stockName[], int numStocks) { int smallest, day; for (int dayNum = 1; dayNum < NUMDAYS; dayNum++) cout<<"On day "<<day<<" C was the stock with the smallest value which was "<<smallest<<endl; cout<<"On day "<<day<<" C was the stock with the smallest value which was "<<smallest<<endl; cout<<"On day "<<day<<" C was the stock with the smallest value which was "<<smallest<<endl; cout<<"On day "<<day<<" MSFT was the stock with the smallest value which was "<<smallest<<endl; return; } */
Last edited by ~s.o.s~; Nov 18th, 2006 at 5:46 pm. Reason: Learn to use code tags on your own.
•
•
Join Date: Nov 2006
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
>> while (!cin.eof())
what does cin have to do with readStocks() function?
IBM 87.72 88.9 80.9 88.5 90.23
CSCO 65.2 75.5 76.8 60.23 49.7
ML 78.6 76.9 80.45 81.6 91.78
MSFT 73.8 73.7 68.8 55.25 63.87
C 50.45 51.67 52.89 55.98 60.55
I am trying to get an output like that
Stock DAY 1 DAY 2 DAY 3 DAY 4 DAY 5 Average
----------------------------------------------------------------------
IBM 87.72 88.90 80.90 88.50 90.23 87.25
CSCO 65.20 75.50 76.80 60.23 49.70 65.49
ML 78.60 76.90 80.45 81.60 91.78 81.87
MSFT 73.80 73.70 68.80 55.25 63.87 67.08
C 50.45 51.67 52.89 55.98 60.55 54.31
Press any key to continue........
by runing
void printHeader()
void readStocks();
void findStockAvg() ;
void printStocks() ;
functions.
but what I got so far its only that
Stock DAY 1 DAY 2 DAY 3 DAY 4 DAY 5 Average
----------------------------------------------------------------------
and blinking prompt
Even when I run only readStocks();
I get blinking prompt so it means that I have a mistake in this function
Could you give me any idea on void readStocks(); ?
Thank you.
P.S. I am so pissed I spend four days on that and steel do not have any result.
•
•
Join Date: Nov 2006
Posts: 4
Reputation:
Solved Threads: 0
Hi
Can give me any idea why I can not get "printDayAvg()"to my cout?
I am not really understand hot to set eof condition so I can get rid of thoes zeros
Right now I have output like this :
----------------------------------------------------------------------------
Stock Prices
Stocks Day 1 Day 2 Day 3 Day 4 Day 5 Average
----------------------------------------------------------------------------
IBM 87.72 88.90 80.90 88.50 90.23 87.25
CSCO 65.20 75.50 76.80 60.23 49.70 65.49
ML 78.60 76.90 80.45 81.60 91.78 81.87
MSFT 73.80 73.70 68.80 55.25 63.87 67.08
C 50.45 51.67 52.89 55.98 60.55 54.31
0.00 0.00 0.00 0.00 0.00 0.00
Press any key to continue . . .
Thank you.
Anton
Can give me any idea why I can not get "printDayAvg()"to my cout?
I am not really understand hot to set eof condition so I can get rid of thoes zeros
Right now I have output like this :
----------------------------------------------------------------------------
Stock Prices
Stocks Day 1 Day 2 Day 3 Day 4 Day 5 Average
----------------------------------------------------------------------------
IBM 87.72 88.90 80.90 88.50 90.23 87.25
CSCO 65.20 75.50 76.80 60.23 49.70 65.49
ML 78.60 76.90 80.45 81.60 91.78 81.87
MSFT 73.80 73.70 68.80 55.25 63.87 67.08
C 50.45 51.67 52.89 55.98 60.55 54.31
0.00 0.00 0.00 0.00 0.00 0.00
Press any key to continue . . .
Thank you.
Anton
C++ Syntax (Toggle Plain Text)
//Stock's statistic #include <iostream> #include <stdlib.h> #include <fstream> using namespace std; const int MAXSTOCKS = 5; const int NUMDAYS = 5; int numStocks =0; ifstream infile; ofstream outfile; void printHeader(void); void readStocks(double [][NUMDAYS], string[], int & ); void findStockAvg(double [][NUMDAYS], double[], int ); void printStocks(double [][NUMDAYS], double[], string[], int ); void findDayAvg(double [][NUMDAYS], double [], int ); //void printDayAvg(double [], int); //void findMinDayOfStock(double[][NUMDAYS], string[], int); //void findStockOfDay(double [][NUMDAYS], string [], int); int main() { double stocks[MAXSTOCKS][NUMDAYS]; string stockName[MAXSTOCKS]; double stockAvg[MAXSTOCKS]; double dayAvg[MAXSTOCKS]; infile.open("C:\\Documents and Settings\\Admin\\Desktop\\job_study\\college\\C++Home\\Stocks_1.txt"); //outfile.open("C:\Documents and Settings\Admin\Desktop\job_study\college\C++Home\Stocks_Report.txt"); outfile.open("con"); outfile.setf(ios::fixed, ios::floatfield); printHeader(); readStocks( stocks, stockName, numStocks); findStockAvg(stocks, stockAvg, numStocks); printStocks(stocks, stockAvg, stockName, numStocks); findDayAvg(stocks, dayAvg,numStocks); //printDayAvg(dayAvg, numStocks); //findMinDayOfStock(stocks, stockName, numStocks); //findStockOfDay(stocks, stockName, numStocks); system("pause"); infile.close(); outfile.close(); return 0; } void printHeader(void) { outfile <<"----------------------------------------------------------------------------"<<endl; outfile <<"\t\t\tStock Prices"<<endl<<endl; outfile.setf(ios::right); outfile.width(10); outfile<<"Stocks"; outfile.width(10); outfile<<"Day 1"; outfile.width(10); outfile<<"Day 2"; outfile.width(10); outfile<<"Day 3"; outfile.width(10); outfile<<"Day 4"; outfile.width(10); outfile<<"Day 5"; outfile.width(10); outfile<<"Average"<<endl; outfile <<"----------------------------------------------------------------------------"<<endl; return; } void readStocks(double stocks[][NUMDAYS], string stockName[], int & numStocks ) { while (!infile.eof()) { infile >> stockName[numStocks]; for (int i = 0; i < NUMDAYS; i++) infile >> stocks[numStocks][i]; numStocks++; } return; } void findStockAvg(double stocks[][NUMDAYS], double stockAvg[], int numStocks) { double sum; double StockAvg; for (int stockNum = 0; stockNum < numStocks; stockNum++) { sum = 0; for (int dayNum = 0; dayNum < NUMDAYS; dayNum++) { sum += stocks[stockNum][dayNum]; } stockAvg[stockNum]= (double)sum/NUMDAYS; } return; } void printStocks(double stocks[][NUMDAYS], double stockAvg[], string stockName[], int numStocks) { outfile.precision(2); for (int stockNum = 0; stockNum < numStocks; stockNum++ ) { outfile.width(10); outfile << stockName[stockNum] << " "; for (int dayNum = 0; dayNum < NUMDAYS; dayNum++) { outfile.width(9); outfile << stocks[stockNum][dayNum] << " "; } outfile.width(9); outfile << stockAvg[stockNum]; outfile << endl; } return; } void findDayAvg(double stocks[][NUMDAYS], double dayAvg[], int numStocks) { double sum; for (int dayNum = 0; dayNum < NUMDAYS; dayNum++) { sum = 0; for (int stockNum = 0; stockNum < numStocks; stockNum++){ sum += stocks[stockNum][dayNum]; } dayAvg [dayNum] = (double)sum/numStocks; } return; } void printDayAvg(double dayAvg[], int numStocks) { outfile.precision(2); outfile.setf(ios::right); outfile<<"------------------------------------------------------------------------------"<<endl; outfile.width(10); outfile<<"Day Avg"; for (int dayNum = 0; dayNum < NUMDAYS; dayNum++){ outfile.width(10); outfile<<dayAvg[dayNum]<< " "; } return; } /* void findMinDayOfStock(double stocks[][NUMDAYS], string stockName[], int numStocks) { int smallest, dayNum; for (int dayNum = 0; dayNum < NUMDAYS; dayNum++) outfile<<stockName[numStocks]<<" had its smallest value on day "<< day <<" with a value of "<< smallest<<endl; outfile<<endl; return; } /* void findStockOfDay(double stocks [][NUMDAYS], string stockName[], int numStocks) { int smallest, day; for (int dayNum = 1; dayNum < NUMDAYS; dayNum++) cout<<"On day "<<day<<" C was the stock with the smallest value which was "<<smallest<<endl; cout<<"On day "<<day<<" C was the stock with the smallest value which was "<<smallest<<endl; cout<<"On day "<<day<<" C was the stock with the smallest value which was "<<smallest<<endl; cout<<"On day "<<day<<" MSFT was the stock with the smallest value which was "<<smallest<<endl; return; } */
Last edited by ~s.o.s~; Nov 20th, 2006 at 1:34 pm. Reason: Learn to use code tags on your own.
The problem is in function readStocks() -- it attempts to read beyond the legal boundries of the array. Here is the correction
C++ Syntax (Toggle Plain Text)
void readStocks(double stocks[][NUMDAYS], string stockName[], int & numStocks ) { string name; while (infile >> name ) { stockName[numStocks] = name; for (int i = 0; i < NUMDAYS; i++) infile >> stocks[numStocks][i]; numStocks++; } }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- Insertion Sort Problem (C++)
- Ask Function problem?thanks (C)
- Abstract Class member function problem (C++)
- Function Problem. Please help (PHP)
- using OCI to call a ORACLE stored function (C++)
- static function problem (C++)
- AnsiString Template Data Return Problem Builder 6 (C++)
Other Threads in the C++ Forum
- Previous Thread: compinations
- Next Thread: trying to make this come out in color
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






