Function problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2006
Posts: 4
Reputation: adkozak is an unknown quantity at this point 
Solved Threads: 0
adkozak adkozak is offline Offline
Newbie Poster

Function problem

 
0
  #1
Nov 18th, 2006
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.
  1. //Stock's statistic
  2. #include <iostream>
  3. #include <stdlib.h>
  4. #include <fstream>
  5. using namespace std;
  6. const int MAXSTOCKS = 5;
  7. const int NUMDAYS = 5;
  8. ifstream infile;
  9. ofstream outfile;
  10.  
  11. void printHeader(void);
  12. void readStocks(double [][NUMDAYS], string[], int & );
  13. //void findStockAvg(double [][NUMDAYS], double[], int );
  14. //void printStocks(double [][NUMDAYS], double[], string[], int );
  15. //void findDayAvg(double [][NUMDAYS], double [], int );
  16. //void printDayAvg(double [], int);
  17. //void findMinDayOfStock(double[][NUMDAYS], string[], int);
  18. //void findStockOfDay(double [][NUMDAYS], string [], int);
  19. int main()
  20. {
  21. double stocks[MAXSTOCKS][NUMDAYS];
  22. string stockName[MAXSTOCKS];
  23. double stockAvg[MAXSTOCKS];
  24. double dayAvg[MAXSTOCKS];
  25. int numStocks;
  26.  
  27. infile.open("C:\\Documents and Settings\\Admin\\Desktop\\job_study\\college\\C++Home\\Stocks_1.txt");
  28. //outfile.open("C:\Documents and Settings\Admin\Desktop\job_study\college\C++Home\Stocks_Report.txt");
  29. outfile.open("con");
  30. outfile.setf(ios::fixed, ios::floatfield);
  31.  
  32. printHeader();
  33.  
  34. readStocks( stocks, stockName, numStocks);
  35. //findStockAvg(stocks, stockAvg, numStocks);
  36. //printStocks(stocks, stockAvg, stockName, numStocks);
  37. //findDayAvg(stocks, dayAvg,numStocks);
  38. //printDayAvg(dayAvg, numStocks);
  39. //findMinDayOfStock(stocks, stockName, numStocks);
  40. //findStockOfDay(stocks, stockName, numStocks);
  41. system("pause");
  42. infile.close();
  43. outfile.close();
  44. return 0;
  45. }
  46. void printHeader(void)
  47. {
  48. outfile <<" Anton Deinega CIS 1.5 EMY6 Program 4 Due: November 20, 2006"<< endl;
  49. outfile <<"--------------------------------------------------------------------"<<endl;
  50. outfile <<"\t\t\tStock Prices"<<endl<<endl;
  51. outfile.precision(2);
  52. outfile.setf(ios::right);
  53. outfile<<" Stocks";
  54. outfile.width(10);
  55. outfile<<"Day 1";
  56. outfile.width(10);
  57. outfile<<"Day 2";
  58. outfile.width(10);
  59. outfile<<"Day 3";
  60. outfile.width(10);
  61. outfile<<"Day 4";
  62. outfile.width(10);
  63. outfile<<"Day 5";
  64. outfile.width(10);
  65. outfile<<"Average"<<endl;
  66. outfile <<"--------------------------------------------------------------------"<<endl;
  67. return;
  68. }
  69. void readStocks(double stocks[][NUMDAYS], string stockName[], int & numStocks )
  70. {
  71. int numStocks =0;
  72. while (!cin.eof())
  73. {
  74. infile>> stockName[numStocks];
  75. for (int i=0; i<NUMDAYS;i++)
  76. infile>>stocks[numStocks][i];
  77. numStocks++;
  78. }
  79. return;
  80. }
  81. /*
  82.   void findStockAvg(double stocks[][NUMDAYS], double stockAvg[], int numStocks)
  83.   {
  84.   double sum;
  85.   double StockAvg;
  86.   for (int stockNum = 1; stockNum < NUMDAYS; stockNum++)
  87.   {
  88.   sum = 0;
  89.   for (int dayNum = 1; dayNum < stockAvg[MAXSTOCKS]; dayNum++)
  90.   {
  91.   sum += stocks[stockNum][dayNum];
  92.   StockAvg = (double)sum/numStocks;
  93.   }
  94.   }
  95.   return;
  96.   }
  97. /*
  98.  void printStocks(double stocks[][NUMDAYS], double stockAvg[], string stockName[], int numStocks)
  99.   {
  100.  
  101.   //ofstream outfile("C:\Documents and Settings\Admin\Desktop\job_study\college\C++Home\StockReport.txt");
  102.   ofstream outfile("con");
  103.   outfile.setf(ios::fixed,ios::floatfield);
  104.   outfile.precision(2);
  105.   outfile.setf(ios::right);
  106.   outfile.width(10);
  107.  
  108.   for (int stockNum = 0; stockNum < numStocks; stockNum++ )
  109.   {
  110.   outfile << stockName[stockNum] << " ";
  111.   for (int dayNum = 0; dayNum < NUMDAYS; dayNum++)
  112.   {
  113.   outfile << stocks[stockNum][dayNum] << " ";
  114.   }
  115.   outfile << endl;
  116.   }
  117.   return;
  118.   }
  119. /* void findDayAvg(double stocks[][NUMDAYS], double DayAvg[], int numStocks)
  120.   {
  121.   double sum;
  122.   double DayAvg;
  123.   for (int stockNum = 1; stockNum < NUMDAYS; stockNum++)
  124.   {
  125.   sum = 0;
  126.   for (int dayNum = 1; dayNum < MAXSTOCKS; dayNum++)
  127.   sum += stocks[stockNum][dayNum];
  128.   DayAvg = (double)sum/numStocks;
  129.   }
  130.   return;
  131.   }
  132. */
  133. /* void printDayAvg(double dayAvg[], int numStocks)
  134.   {
  135.   //ofstream outfile("C:\Documents and Settings\Admin\Desktop\job_study\college\C++Home\StockReport.txt");
  136.   ofstream outfile("con");
  137.  
  138.   outfile<<"------------------------------------------------------------------------------"<<endl;
  139.   outfile<<"Day Avg";
  140.   for (int stockNum = 1; stockNum < NUMDAYS; stockNum++)
  141.   {
  142.   outfile<<dayAvg[numStocks]<<endl;
  143.   }
  144.   outfile.setf(ios::fixed,ios::floatfield);
  145.   outfile.precision(2);
  146.   outfile.setf(ios::right);
  147.   outfile.width(10);
  148.   return;
  149.   }
  150. /*
  151.   void findMinDayOfStock(double stocks[][NUMDAYS], string stockName[], int numStocks)
  152.   {
  153.   int smallest, dayNum;
  154.  
  155.   for (int dayNum = 1; dayNum < NUMDAYS; dayNum++)
  156.  
  157.   cout<<stockName[0]<<" had its smallest value on day "<< day <<" with a value of "<< smallest<<endl;
  158.   cout<<stockName[1]<<" had its smallest value on day "<< day <<" with a value of "<< smallest<<endl;
  159.   cout<<stockName[2]<<" had its smallest value on day "<< day <<" with a value of "<< smallest<<endl;
  160.   cout<<stockName[3]<<" had its smallest value on day "<< day <<" with a value of "<< smallest<<endl;
  161.   return;
  162.   }
  163. /*
  164.   void findStockOfDay(double stocks [][NUMDAYS], string stockName[], int numStocks)
  165.   {
  166.   int smallest, day;
  167.  
  168.   for (int dayNum = 1; dayNum < NUMDAYS; dayNum++)
  169.  
  170.   cout<<"On day "<<day<<" C was the stock with the smallest value which was "<<smallest<<endl;
  171.   cout<<"On day "<<day<<" C was the stock with the smallest value which was "<<smallest<<endl;
  172.   cout<<"On day "<<day<<" C was the stock with the smallest value which was "<<smallest<<endl;
  173.   cout<<"On day "<<day<<" MSFT was the stock with the smallest value which was "<<smallest<<endl;
  174.   return;
  175.   }
  176. */
Last edited by ~s.o.s~; Nov 18th, 2006 at 5:46 pm. Reason: Learn to use code tags on your own.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,629
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Function problem

 
0
  #2
Nov 18th, 2006
>> while (!cin.eof())
what does cin have to do with readStocks() function?
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 4
Reputation: adkozak is an unknown quantity at this point 
Solved Threads: 0
adkozak adkozak is offline Offline
Newbie Poster

Re: Function problem

 
0
  #3
Nov 18th, 2006
Originally Posted by Ancient Dragon View Post
>> while (!cin.eof())
what does cin have to do with readStocks() function?
What I mean is by "while (!infile.eof())" is that its define the end of my infile which is:
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,121
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Function problem

 
0
  #4
Nov 18th, 2006
Deja vu!

I've seen this somewhere before
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 4
Reputation: adkozak is an unknown quantity at this point 
Solved Threads: 0
adkozak adkozak is offline Offline
Newbie Poster

Re: Function problem

 
0
  #5
Nov 20th, 2006
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
  1. //Stock's statistic
  2. #include <iostream>
  3. #include <stdlib.h>
  4. #include <fstream>
  5. using namespace std;
  6. const int MAXSTOCKS = 5;
  7. const int NUMDAYS = 5;
  8. int numStocks =0;
  9. ifstream infile;
  10. ofstream outfile;
  11.  
  12. void printHeader(void);
  13. void readStocks(double [][NUMDAYS], string[], int & );
  14. void findStockAvg(double [][NUMDAYS], double[], int );
  15. void printStocks(double [][NUMDAYS], double[], string[], int );
  16. void findDayAvg(double [][NUMDAYS], double [], int );
  17. //void printDayAvg(double [], int);
  18. //void findMinDayOfStock(double[][NUMDAYS], string[], int);
  19. //void findStockOfDay(double [][NUMDAYS], string [], int);
  20. int main()
  21. {
  22. double stocks[MAXSTOCKS][NUMDAYS];
  23. string stockName[MAXSTOCKS];
  24. double stockAvg[MAXSTOCKS];
  25. double dayAvg[MAXSTOCKS];
  26.  
  27. infile.open("C:\\Documents and Settings\\Admin\\Desktop\\job_study\\college\\C++Home\\Stocks_1.txt");
  28. //outfile.open("C:\Documents and Settings\Admin\Desktop\job_study\college\C++Home\Stocks_Report.txt");
  29. outfile.open("con");
  30. outfile.setf(ios::fixed, ios::floatfield);
  31.  
  32. printHeader();
  33. readStocks( stocks, stockName, numStocks);
  34. findStockAvg(stocks, stockAvg, numStocks);
  35. printStocks(stocks, stockAvg, stockName, numStocks);
  36. findDayAvg(stocks, dayAvg,numStocks);
  37. //printDayAvg(dayAvg, numStocks);
  38. //findMinDayOfStock(stocks, stockName, numStocks);
  39. //findStockOfDay(stocks, stockName, numStocks);
  40. system("pause");
  41. infile.close();
  42. outfile.close();
  43. return 0;
  44. }
  45. void printHeader(void)
  46. {
  47.  
  48. outfile <<"----------------------------------------------------------------------------"<<endl;
  49. outfile <<"\t\t\tStock Prices"<<endl<<endl;
  50. outfile.setf(ios::right);
  51. outfile.width(10);
  52. outfile<<"Stocks";
  53. outfile.width(10);
  54. outfile<<"Day 1";
  55. outfile.width(10);
  56. outfile<<"Day 2";
  57. outfile.width(10);
  58. outfile<<"Day 3";
  59. outfile.width(10);
  60. outfile<<"Day 4";
  61. outfile.width(10);
  62. outfile<<"Day 5";
  63. outfile.width(10);
  64. outfile<<"Average"<<endl;
  65. outfile <<"----------------------------------------------------------------------------"<<endl;
  66. return;
  67. }
  68. void readStocks(double stocks[][NUMDAYS], string stockName[], int & numStocks )
  69. {
  70. while (!infile.eof())
  71. {
  72. infile >> stockName[numStocks];
  73. for (int i = 0; i < NUMDAYS; i++)
  74. infile >> stocks[numStocks][i];
  75.  
  76. numStocks++;
  77. }
  78. return;
  79. }
  80.  
  81. void findStockAvg(double stocks[][NUMDAYS], double stockAvg[], int numStocks)
  82. {
  83. double sum;
  84. double StockAvg;
  85. for (int stockNum = 0; stockNum < numStocks; stockNum++)
  86. {
  87. sum = 0;
  88. for (int dayNum = 0; dayNum < NUMDAYS; dayNum++)
  89. {
  90. sum += stocks[stockNum][dayNum];
  91. }
  92. stockAvg[stockNum]= (double)sum/NUMDAYS;
  93. }
  94. return;
  95. }
  96. void printStocks(double stocks[][NUMDAYS], double stockAvg[], string stockName[], int numStocks)
  97. {
  98. outfile.precision(2);
  99.  
  100. for (int stockNum = 0; stockNum < numStocks; stockNum++ )
  101. {
  102. outfile.width(10);
  103. outfile << stockName[stockNum] << " ";
  104. for (int dayNum = 0; dayNum < NUMDAYS; dayNum++)
  105. {
  106. outfile.width(9);
  107. outfile << stocks[stockNum][dayNum] << " ";
  108. }
  109. outfile.width(9);
  110. outfile << stockAvg[stockNum];
  111. outfile << endl;
  112. }
  113. return;
  114. }
  115. void findDayAvg(double stocks[][NUMDAYS], double dayAvg[], int numStocks)
  116. {
  117. double sum;
  118.  
  119. for (int dayNum = 0; dayNum < NUMDAYS; dayNum++) {
  120. sum = 0;
  121. for (int stockNum = 0; stockNum < numStocks; stockNum++){
  122. sum += stocks[stockNum][dayNum];
  123. }
  124. dayAvg [dayNum] = (double)sum/numStocks;
  125. }
  126. return;
  127. }
  128. void printDayAvg(double dayAvg[], int numStocks)
  129. {
  130. outfile.precision(2);
  131. outfile.setf(ios::right);
  132. outfile<<"------------------------------------------------------------------------------"<<endl;
  133. outfile.width(10);
  134. outfile<<"Day Avg";
  135. for (int dayNum = 0; dayNum < NUMDAYS; dayNum++){
  136. outfile.width(10);
  137. outfile<<dayAvg[dayNum]<< " ";
  138. }
  139. return;
  140. }
  141. /* void findMinDayOfStock(double stocks[][NUMDAYS], string stockName[], int numStocks)
  142.   {
  143.   int smallest, dayNum;
  144.  
  145.  
  146.   for (int dayNum = 0; dayNum < NUMDAYS; dayNum++)
  147.   outfile<<stockName[numStocks]<<" had its smallest value on day "<< day <<" with a value of "<< smallest<<endl;
  148.   outfile<<endl;
  149.   return;
  150.   }
  151. /*
  152.   void findStockOfDay(double stocks [][NUMDAYS], string stockName[], int numStocks)
  153.   {
  154.   int smallest, day;
  155.  
  156.   for (int dayNum = 1; dayNum < NUMDAYS; dayNum++)
  157.  
  158.   cout<<"On day "<<day<<" C was the stock with the smallest value which was "<<smallest<<endl;
  159.   cout<<"On day "<<day<<" C was the stock with the smallest value which was "<<smallest<<endl;
  160.   cout<<"On day "<<day<<" C was the stock with the smallest value which was "<<smallest<<endl;
  161.   cout<<"On day "<<day<<" MSFT was the stock with the smallest value which was "<<smallest<<endl;
  162.   return;
  163.   }
  164. */
Last edited by ~s.o.s~; Nov 20th, 2006 at 1:34 pm. Reason: Learn to use code tags on your own.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,629
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Function problem

 
0
  #6
Nov 20th, 2006
The problem is in function readStocks() -- it attempts to read beyond the legal boundries of the array. Here is the correction
  1. void readStocks(double stocks[][NUMDAYS], string stockName[], int & numStocks )
  2. {
  3. string name;
  4. while (infile >> name )
  5. {
  6. stockName[numStocks] = name;
  7. for (int i = 0; i < NUMDAYS; i++)
  8. infile >> stocks[numStocks][i];
  9.  
  10. numStocks++;
  11. }
  12. }
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 4
Reputation: adkozak is an unknown quantity at this point 
Solved Threads: 0
adkozak adkozak is offline Offline
Newbie Poster
 
0
  #7
Nov 20th, 2006
Thak you.

Thanks a lot.
Last edited by ~s.o.s~; Nov 21st, 2006 at 2:56 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC