| | |
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); }
I give up! 1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ] 2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ... 3) What is the 123456789 prime numer? Ask4Answer
![]() |
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?
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






