| | |
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; 32 Days Ago at 8:50 pm.
•
•
Join Date: May 2008
Posts: 12
Reputation:
Solved Threads: 0
0
#4 32 Days Ago
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; 32 Days Ago at 11:02 pm. Reason: wrong code
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?•
•
Join Date: Oct 2009
Posts: 7
Reputation:
Solved Threads: 3
0
#6 32 Days Ago
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 |
.dll ada api array arrays assign assignment background binary bitmap c++ calculator char char* class classes client codeblocks commentinghelp compile compression console convert crashcourse data desktop development download email encryption error evc faq file floatingpoint fpx fstream function functions game gnu graph guess hash html ide ifstream int integer introduction java keyboard kioti16 math modal newbie node number opengl output partnership pause php pong prime print problem professor program programming projects python read recursion recursive regqueryvalueex rpg search send set snakes space stream string strings superclass symbian template test text toolbar tree university url variable vector visual visualstudio web win32







