| | |
file operation
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2009
Posts: 27
Reputation:
Solved Threads: 0
Hi all,
I am having problem about file operations. There are a few numbers in my txt file. I want the program to calculate average of these numbers in the txt file. The code I have written is as below. Number of numbers in the file is 5. When I return count it returns 5 .The sum of numbers should be 92 but I get 94, and I get average 18.8 but the correct average should be 18,4 (txt file is attached). So can anyone help me about it? Do you think is it a good idea to calculate it in this way?
Thanks
the code:
I am having problem about file operations. There are a few numbers in my txt file. I want the program to calculate average of these numbers in the txt file. The code I have written is as below. Number of numbers in the file is 5. When I return count it returns 5 .The sum of numbers should be 92 but I get 94, and I get average 18.8 but the correct average should be 18,4 (txt file is attached). So can anyone help me about it? Do you think is it a good idea to calculate it in this way?
Thanks
the code:
C++ Syntax (Toggle Plain Text)
int main() { ifstream fin; fin.open("numbers.txt"); if (fin.fail()) { cout << "erorr on opening the file \n"; exit (1); } cout << "the caculated average is " << avg_file(fin) << endl; double calculated_avg = avg_file(fin); } double avg_file(ifstream& source_file) { double number_in_file; double total = 0; int count = 0; source_file >> number_in_file; while (! source_file.eof()) { source_file >> number_in_file; total = number_in_file + total; count ++; } //average of numbers in file return (count); }
double avg_file(ifstream& source_file)
{
double number_in_file;
double total = 0;
int count = 0;
while (source_file >> number_in_file)
{
total = number_in_file + total;
count ++;
}
//average of numbers in file
return (count);
} Last edited by Dave Sinkula; Aug 24th, 2009 at 5:57 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
•
•
http://www.daniweb.com/forums/post15...tml#post155265double avg_file(ifstream& source_file) { double number_in_file; double total = 0; int count = 0; while (source_file >> number_in_file) { total = number_in_file + total; count ++; } //average of numbers in file return (count); }
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
![]() |
Similar Threads
- Simple File Operation Question (Python)
- File I/O input being misread? (C++)
- File Operation in VB (Visual Basic 4 / 5 / 6)
- File issues in in C++ (C++)
- Problem with ACE file operation append mode in ACE_OS::fopen function (C++)
- Writing string to a file (Java)
- Extract header info from image file (Python)
- Errors writing a vector to a file (C++)
- "File operations in c++" (C++)
Other Threads in the C++ Forum
- Previous Thread: Output of struct to BIN file
- Next Thread: Why can a nested class access to the private member data?
Views: 244 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines linker list loop looping loops map math matrix memory newbie news number output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






