passing ifstream object as a function parameter

I am getting error messages with my code and im not sure whats wrong. The ones I am getting are:

projects\hw7\hw7\hw7.cpp(53): warning C4305: 'initializing' : truncation from '__int64' to 'long'
projects\hw7\hw7\hw7.cpp(53): warning C4309: 'initializing' : truncation of constant value
projects\hw7\hw7\hw7.cpp(77): error C2275: 'std::ifstream' : illegal use of this type as an expression
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\iosfwd(647) : see declaration of 'std::ifstream'
projects\hw7\hw7\hw7.cpp(77): error C2146: syntax error : missing ')' before identifier 'ifile'
projects\hw7\hw7\hw7.cpp(77): error C2059: syntax error : ')'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#define _USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib> 
#include <math.h>
#include <string>
#include <fstream>


using namespace std;

/** 
* <The function reads lines of numbers and returns the largest, smallest, desired numbers>
*
* @return 0   Indicating successful completion.
*/

//creating the function that will be used to look through the file
int processFile(ifstream& ifile, long ithVal, long& retIthVal, long& minimum, long& maximum);


int main()
{
	//declaring all the variables to be used in the program
	long ithVal;
	long retIthVal;
	long minimum = 100000000000000;
	long maximum = 0;
	int check;


	//prompting the user to input the file name for the program to open and read
	ifstream ifile;
	string fileName;
	cout << "Iput File Name: ";
	cin >> fileName;
	ifile.open(fileName.c_str());

	//in case the file does not exist or is mispelled, the program will let the user know
	if (!ifile.is_open())
	{
		cout << "File doesn't exist" << endl;
		return 1;
	}

	//prompting the user which number to look for
	cout << "Which number do you want to return? " << endl;
	cin >> ithVal;

	//runs the function to look for the max, min, and desired numbers
	check = processFile(ifstream ifile, long ithVal, long& retIthVal, long& minimum, long& maximum);

	//outputting the max and min values
	cout << "Min is {" << minimum << "}." << endl;
	cout << "Max is {" << maximum << "}." << endl << endl;

	//checks to see if the searched for number exits in the file and outputs either the number or an error message
	if(check)
	{
		cout << "Value{" << ithVal << "} is " << retIthVal << endl;
	}
	else
	{
		cout << "There aren't that many numbers in the file!" << endl;
	}
	return 0;
}

//function used to look through file
int processFile(ifstream& ifile, long ithVal, long& retIthVal, long& minimum, long& maximum)
{
	//declaring variables to be used in the function
	long currentVal;
	long count = 0;

	//takes a vale from the file
	ifile >> currentVal;

	//while loop used to look through every value of the file until there are no more left
	while (ifile)
	{
		//counter used to keep track of the number of values in the file
		count ++;

		//if the desired number is reached, set it as the return value
		if (count == ithVal)
		{
			retIthVal = currentVal;
		}

		//if the current value is less than the previous minimum, set it as the new minimum
		if (currentVal < minimum)
		{
			minimum = currentVal;
		}

		//if the current value is larger than the previous maximum, set it as the new maximum 
		if (currentVal > maximum)
		{
			maximum = currentVal;
		}

		//reads the next value in the file
		ifile >> currentVal;

	}

	//checks to see if the prorgam was able to find the ith value
	if (count <= ithVal)
	{
		return 0;
	}
	else 
	{
		return 1;
	}
}
Last edited on
Line 28: Exceeds the value that can be stored in a long (implementation dependent).
Line 52: Get rid of the types and the &s.

 
check = processFile(ifile, ithVal, retIthVal, minimum, maximum);


You don't need to include <math.h> if you've included <cmath>.
Last edited on
Topic archived. No new replies allowed.