Can functions that return int, take files as parameters.

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

Join Date: May 2008
Posts: 12
Reputation: jascase901 is an unknown quantity at this point 
Solved Threads: 0
jascase901 jascase901 is offline Offline
Newbie Poster

Can functions that return int, take files as parameters.

 
0
  #1
32 Days Ago
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; 32 Days Ago at 8:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,783
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 744
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess
 
2
  #2
32 Days Ago
>//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?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 12
Reputation: jascase901 is an unknown quantity at this point 
Solved Threads: 0
jascase901 jascase901 is offline Offline
Newbie Poster
 
0
  #3
32 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 12
Reputation: jascase901 is an unknown quantity at this point 
Solved Threads: 0
jascase901 jascase901 is offline Offline
Newbie Poster
 
0
  #4
32 Days Ago
here is the code

  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; 32 Days Ago at 11:02 pm. Reason: wrong code
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,346
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 168
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
0
  #5
32 Days Ago
call your calc_average inside openFile function.
1) What word becomes shorter if you add a letter to it? 
      [ Solved by : niek_e, Paul Thompson, SgtMe, murtan, xavier666, jonsca]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
      [*solved by : murtan, xavier666]
3) What is the 123456789th prime numer?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 7
Reputation: alg is an unknown quantity at this point 
Solved Threads: 3
alg alg is offline Offline
Newbie Poster
 
0
  #6
32 Days Ago
main function should return a value
  1. int main()
  2. {
  3. .
  4. .
  5. .
  6. .
  7. .
  8. .
  9.  
  10. return 0;
  11. }


double calc_average(ifstream& fin)
  1. double calc_average(fin)
Reply With Quote Quick reply to this message  
Reply

Tags
c++, ifstream, int

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC