User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 397,851 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,316 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 467 | Replies: 4 | Solved
Reply
Join Date: Jun 2007
Posts: 3
Reputation: trl10 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
trl10 trl10 is offline Offline
Newbie Poster

Using functions with arrays

  #1  
Aug 8th, 2007
Hi Everyone,
I have to read data from a file into an array. I have that part figured out. I can't get my function to work. I have to write a sumArray, avgSales, highSales, and lowSales. Here is what I got so far. Could someone please help me with one of the functions. I think I can get the rest if I have a god example to follow. Thanks in advance.
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. //Constant array size declaration.
  8. const int LOCATIONS = 20;
  9.  
  10. //Function Prototypes
  11. void readData(int a[], int size, double& data);
  12. double sumArray(double& totalSales);
  13.  
  14. int main()
  15. {
  16. cout.setf(ios::fixed);
  17. cout.setf(ios::showpoint);
  18. cout.precision(2);
  19. ifstream in;
  20. char inFile[256];
  21. double a[LOCATIONS] = {0}, sales, totalSales = 0;
  22. int data = 0;
  23.  
  24. //Prompts the user for the input file name.
  25. cout << "Enter the input file name: ";
  26. cin >> inFile;
  27.  
  28. // Opens the stream and connects to the file.
  29. in.open(inFile);
  30.  
  31. //Checks to see if the input file opened properly.
  32. //Displays an error message if file not opened.
  33. if(in.fail( ))
  34. {
  35. cout << "Input file opening failed.\n";
  36. exit(1);
  37.  
  38. //Closes file explicitly.
  39. in.close( );
  40. }
  41.  
  42.  
  43. //Finds and prints the total sales amount.
  44. sumArray(totalSales);
  45. cout << "The total sales are $" << totalSales << endl;
  46.  
  47.  
  48. //Reads in and displays data.
  49. while(in >> a[data])
  50. {
  51. sales = a[data];
  52. data++;
  53.  
  54. }
  55. cout << "Number Of Locations In File: " << a[0] << endl << endl;
  56.  
  57. for(int index = 1; index < data; index++)
  58. {
  59. cout << "Location Number " << index << ": $" << a[index] << endl;
  60. }
  61.  
  62. }
  63.  
  64.  
  65.  
  66. double sumArray(double& totalSales)
  67. {
  68. ifstream in;
  69. double a[LOCATIONS] = {0}, sales;
  70. int data = 0;
  71.  
  72. //Reads in and displays data.
  73. while(in >> a[data])
  74. {
  75. for(int index = 1; index < data; index++)
  76. {
  77. totalSales = totalSales + a[index];
  78. index++;
  79. return(totalSales);
  80.  
  81. }
  82. }
  83.  
  84. }
My total sales output is $0.00, so I know that it's not reading in correctly. Please guide me.
Last edited by Ancient Dragon : Aug 8th, 2007 at 1:29 am. Reason: added language to code tags to get line numbers
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,643
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 867
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Using functions with arrays

  #2  
Aug 8th, 2007
line 29: open() wants a const char* for the filename and you passed a std::string object. I'm supprised your compiler did not object and spew out some nasty error messages. It should be this: in.open(inFile.c_str());

line 39: unnecessary -- if open() failed the stream can not be closed.

line 73: input file was never opened so lines 73-82 will probably never get executed.

lines 75-80 should not be inside the while loop beginning at line 73 because total sales will get counted several times. Move that for loop outside and below the while loop.

Actually, in sumArray() you don't need that double array at all because you don't do anything with it. A better solution is to read the array into memory once in main() then pass the array to sumArray() function. Just add another parameter to sumArray().
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Jun 2007
Posts: 3
Reputation: trl10 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
trl10 trl10 is offline Offline
Newbie Poster

Re: Using functions with arrays

  #3  
Aug 8th, 2007
Originally Posted by Ancient Dragon View Post
line 29: open() wants a const char* for the filename and you passed a std::string object. I'm supprised your compiler did not object and spew out some nasty error messages. It should be this: in.open(inFile.c_str()); //This is the way the book showed us, it works fine. It reads and prints the data. I'm not sure how to use what you have suggested.

line 39: unnecessary -- if open() failed the stream can not be closed.//Our teacher advised us to always close a file.

line 73: input file was never opened so lines 73-82 will probably never get executed.//So how do I go abut doing that. Do I have it all set up wrong?

lines 75-80 should not be inside the while loop beginning at line 73 because total sales will get counted several times. Move that for loop outside and below the while loop.// I did that because I thought that it would read from the text file while there was data to be read.

Actually, in sumArray() you don't need that double array at all because you don't do anything with it. A better solution is to read the array into memory once in main() then pass the array to sumArray() function. Just add another parameter to sumArray(). //Could you please provide an example on how to do this. My book seems vague and I'm really struggling.

Thanks for all the feedback. Could you please provide some type of example that may help me understand this better. I seem to do better when I can see how something is supposed to work. I'm not asking you to write the code, just some type of example. Thanks again.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,643
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 867
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Using functions with arrays

  #4  
Aug 8th, 2007
here's what the functions would look like
double sumArray(double a[LOCATIONS], double& totalSales) 
{

}

and here is how to call it from main () on line 44. And don't forget to change the function prototype on line 12 too.

sumArray(a, totalSales);

BTW: you should name arrays something other than one-letter names.
Last edited by Ancient Dragon : Aug 8th, 2007 at 8:29 am.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Jun 2007
Posts: 3
Reputation: trl10 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
trl10 trl10 is offline Offline
Newbie Poster

Re: Using functions with arrays

  #5  
Aug 8th, 2007
Thanks, I did change the array name to sales. Thanks for the tips.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 8:12 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC