| | |
Can functions that return int, take files as parameters.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2008
Posts: 12
Reputation:
Solved Threads: 0
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!
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.
•
•
Join Date: May 2008
Posts: 12
Reputation:
Solved Threads: 0
0
#4 Nov 7th, 2009
here is the code
C++ Syntax (Toggle Plain Text)
#include <fstream> #include <iostream> #include <cstdlib> #include <cctype> using namespace std; void open_file(ifstream& fin); //Precondition: The File Written.dat must exist //Postcondition: opens that file double calc_average(ifstream& fin); //Precondition, file must be opened and linked //Takes the average of a list of numbers int main() { double average; ifstream file_in; open_file(file_in); average=calc_average(file_in); } void open_file(ifstream& fin) { fin.open("written.dat"); if (fin.fail()) { cout<<"fin failed line 26"; exit(1); } } double calc_average(fin) { double next, sum; int count=0; while (fin>>next) { sum=next+sum; count++; } return (sum/count); }
Last edited by jascase901; Nov 7th, 2009 at 11:02 pm. Reason: wrong code
0
#5 Nov 8th, 2009
call your calc_average inside openFile function.
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle. 2) Problem 2[b]solved by : jonsca
•
•
Join Date: Oct 2009
Posts: 7
Reputation:
Solved Threads: 3
0
#6 Nov 8th, 2009
main function should return a value
double calc_average(ifstream& fin)
C++ Syntax (Toggle Plain Text)
int main() { . . . . . . return 0; }
double calc_average(ifstream& fin)
C++ Syntax (Toggle Plain Text)
double calc_average(fin)
![]() |
Similar Threads
- need a sample project in VB.NET/ASP.NET (VB.NET)
- txt to sql database (C#)
- Recursive File List - Help me problem solve please (Java)
- Function to return int[] (C++)
- Huffman Coding Doubt (C++)
- numerical value of EOF (C)
- help with rock paper scissor requirement (C++)
- why return an int from main? (C++)
- help with creating and calling a function (C++)
- FindFirstFile & FindNextFile return extra files? (C++)
Other Threads in the C++ Forum
- Previous Thread: C++ IM Client
- Next Thread: d.s. malik
| Thread Tools | Search this Thread |
Tag cloud for c++, ifstream, int
3d add api array arrays assignment based beginner binary bitmap bmp borland c# c++ char char* class compile compiler console convert delayload delete development dialog download ebook ect encryption engine equation error exam file fstream function functions game gdi+ graph ifstream insert int java jni line lines linker linux lnk2019 math matrix maze memory method microsoft multiple music newbie noob numbers output parallel position practice primenumbersinrange problem program programming python read recursion recursive registry rpg samples savefile selection sql sticky stop store string strings struct studio subclass system template templates text toolkit traverse tree url validate visual visualization win32 wxwidgets







