Using functions with arrays

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2007
Posts: 3
Reputation: trl10 is an unknown quantity at this point 
Solved Threads: 0
trl10 trl10 is offline Offline
Newbie Poster

Using functions with arrays

 
1
  #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 2:29 am. Reason: added language to code tags to get line numbers
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,408
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: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Using functions with arrays

 
0
  #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().
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: Jun 2007
Posts: 3
Reputation: trl10 is an unknown quantity at this point 
Solved Threads: 0
trl10 trl10 is offline Offline
Newbie Poster

Re: Using functions with arrays

 
0
  #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 Quick reply to this message  
Join Date: Aug 2005
Posts: 15,408
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: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Using functions with arrays

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

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.

  1. sumArray(a, totalSales);

BTW: you should name arrays something other than one-letter names.
Last edited by Ancient Dragon; Aug 8th, 2007 at 9:29 am.
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: Jun 2007
Posts: 3
Reputation: trl10 is an unknown quantity at this point 
Solved Threads: 0
trl10 trl10 is offline Offline
Newbie Poster

Re: Using functions with arrays

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

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC