Hi,
I'm trying to learn C++ by myself and I'm new to programming. I am getting compiler error and don't know how to solve it. I would be grateful if somebody can help me out.
The question is "Compute the median of a data file. The median is the number that has the same number of data elements greater than the number as
there are less than the number. For purposes of this problem, you
are to assume that the data is sorted (that is, is in increasing order).
The median is the middle element of the file if there are an odd
number of elements, or the average of the two middle elements if
the file has an even number of elements. You will need to open the
file, count the members, close the file and calculate the location of
the middle of the file, open the file again (recall the "start over" discussion
in this chapter), count up to the file entries you need, and
calculate the middle."
Here is the code that I've written:
// programming_project_06.03a.cpp
// Compute the median of a data file. The median is the number that
// has the same number of data elements greater than the number as
// there are less than the number. For purposes of this problem, you
// are to assume that the data is sorted (that is, is in increasing order).
// The median is the middle element of the file if there are an odd
// number of elements, or the average of the two middle elements if
// the file has an even number of elements. You will need to open the
// file, count the members, close the file and calculate the location of
// the middle of the file, open the file again (recall the "start over" discussion
// in this chapter), count up to the file entries you need, and
// calculate the middle.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
// function definition
double median_calc(int count, ifstream fin);
int main()
{
ifstream fin; // "fin" object declaration
char in_file_name[50];
int number, count;
cout << "Enter the file name: ";
cin >> in_file_name;
// opening the file
fin.open(in_file_name);
// validating the success in opening the file
if(fin.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
count = 0;
// loop to find out how many elements are in the file
while(!fin.eof())
{
fin >> number;
count++;
}
double median = median_calc(count, fin);
fin.close();
cout << "There are " << count << " elements in the file." << endl << endl;
cout << "The median is " << median << endl << endl;
I get compiling error. I use visual C++ 2010 express. I tried to figure out where the problem is and found that it gives me error when I invoke the function double median = median_calc(count, fin);
If I comment it out, it compiles.
The compiler error I get is
error begin
error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ios(176) : see declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> This diagnostic occurred in the compiler generated function 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream(const std::basic_ifstream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
error end