944,163 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1567
  • C++ RSS
Nov 7th, 2009
0

Can functions that return int, take files as parameters.

Expand Post »
So far, I have only read about using void functions to take on ifstreams as a parameter. Yet I want to be able to make a single function that takes a file of numbers as a parameter, and do some calculation to each number. I can do this with a void function pretty easily, but I want a separate the calculations and the opening of the file. How do I do this?

for an example Ill show you these prototypes.
//works
void average( ifstream& file_in);
//doesnt work to my knowledge
int average(ifstream& file_in);

Ps I am away from my computer with my code, and my compiler.
No arrays please I havent learned that yet!
Last edited by jascase901; Nov 7th, 2009 at 8:50 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jascase901 is offline Offline
12 posts
since May 2008
Nov 7th, 2009
2
Re: Can functions that return int, take files as parameters.
>//doesnt work to my knowledge
>int average(ifstream& file_in);

The parameters and return type are not dependent. If that prototype doesn't work, it's because you did something else wrong. How about posting a complete program that fails?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 7th, 2009
0
Re: Can functions that return int, take files as parameters.
Ill be away from my comp with the program for a few hours, but thanks for the answer. I spent like an hour google searching for that and found nothing.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jascase901 is offline Offline
12 posts
since May 2008
Nov 7th, 2009
0
Re: Can functions that return int, take files as parameters.
here is the code

C++ Syntax (Toggle Plain Text)
  1. #include <fstream>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <cctype>
  5. using namespace std;
  6.  
  7. void open_file(ifstream& fin);
  8. //Precondition: The File Written.dat must exist
  9. //Postcondition: opens that file
  10.  
  11. double calc_average(ifstream& fin);
  12. //Precondition, file must be opened and linked
  13. //Takes the average of a list of numbers
  14.  
  15.  
  16. int main()
  17. {
  18. double average;
  19. ifstream file_in;
  20.  
  21. open_file(file_in);
  22. average=calc_average(file_in);
  23.  
  24. }
  25.  
  26.  
  27.  
  28.  
  29. void open_file(ifstream& fin)
  30. {
  31. fin.open("written.dat");
  32. if (fin.fail())
  33. {
  34. cout<<"fin failed line 26";
  35. exit(1);
  36. }
  37. }
  38.  
  39.  
  40.  
  41.  
  42. double calc_average(fin)
  43. {
  44. double next, sum;
  45. int count=0;
  46.  
  47. while (fin>>next)
  48. {
  49. sum=next+sum;
  50. count++;
  51. }
  52.  
  53. return (sum/count);
  54. }
Last edited by jascase901; Nov 7th, 2009 at 11:02 pm. Reason: wrong code
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jascase901 is offline Offline
12 posts
since May 2008
Nov 8th, 2009
0
Re: Can functions that return int, take files as parameters.
call your calc_average inside openFile function.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,865 posts
since Dec 2008
Nov 8th, 2009
0
Re: Can functions that return int, take files as parameters.
main function should return a value
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. .
  4. .
  5. .
  6. .
  7. .
  8. .
  9.  
  10. return 0;
  11. }


double calc_average(ifstream& fin)
C++ Syntax (Toggle Plain Text)
  1. double calc_average(fin)
alg
Reputation Points: 10
Solved Threads: 3
Newbie Poster
alg is offline Offline
17 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ IM Client
Next Thread in C++ Forum Timeline: d.s. malik





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC