Reputation: 1145
I get the following errors when I try to build this project:
error C2182: 'read_data':illegal use of type 'void'
error C2078: too many initializers
errors c2440: 'initializing': cannot convert from 'std::ofstream' to int
All of the above seem to be pointing to my function call on line 72, which is this line: void read_data(finput, foutput);
I've looked up these error codes on the MSDN site but wasn't able to use the description to deduce what might be wrong.
Any ideas/tips are appreciated.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
void read_data(ifstream& finput, ofstream& foutput);
//PRE: The address of the ifstream & ofstream objects is passed to the function
//POST: The data values are read in until the end of the file is reached
void print_data(ofstream& foutput, string fname, string lname, int largest, int smallest);
//PRE: The address of the ofstream object and the values of fname, lname and largest and smallest integer
// in each row is passed to the function
//POST: The values are outpur to the file with formatting
int max(int num1, int num2, int num3, int num4);
//PRE: Four integer values are passed to the function
//POST: The largest of the four integer values is returned
int min(int num1, int num2, int num3, int num4);
//PRE: Four integer values are passed to the function
//POST: The smallest of the four integer values is returned
int main()
{
//Declare the filestream objects
ifstream finput;
ofstream foutput;
//Attempt to open the input & output files
//In each case, print an error message and quit if they fail to open
finput.open("program4_input.txt");
if (finput.fail())
{
cout <<"The input file failed to open!" << endl;
return exit(1);
}
foutput.open("output.txt");
if (foutput.fail())
{
cout <<"The output file failed to open!" << endl;
return exit(2);
}
void read_data(finput, foutput);
return 0;
}
//Function definitions
void read_data(ifstream& finput, ofstream& foutput)
{
string fname, lname;
int num1, num2, num3, num4, largest, smallest;
while(finput >> fname)
{
finput >> lname >> num1 >> num2 >> num3 >> num4;
largest = max(num1, num2, num3, num4);
smallest = min(num1, num2, num3, num4);
print_data(foutput, fname, lname, largest, smallest);
}
}
void print_data(ofstream& foutput, string fname, string lname, int largest, int smallest)
{
foutput << setw(15) << fname << setw(15) << lname << setw(10) << largest << setw(10) << smallest
<< endl;
}
int max(int num1, int num2, int num3, int num4)
{
int lnum, lnum1, lnum2;
if (num1 > num2)
{
lnum1 = num1;
}
else
lnum1 = num2;
if (num3 > num4)
{
lnum2 = num3;
}
else
lnum2 = num4;
if (lnum1 > lnum2)
{
lnum = lnum1;
}
else
lnum = lnum2;
return lnum;
}
int min(int num1, int num2, int num3, int num4)
{
int snum, snum1, snum2;
if (num1 < num2)
{
snum1 = num1;
}
else
snum1 = num2;
if (num3 > num4)
{
snum2 = num3;
}
else
snum2 = num4;
if (snum1 > snum2)
{
snum = snum1;
}
else
snum = snum2;
return snum;
}
Upvotes: 1
Views: 208
Reputation: 1547
You have void before your call which is the signature of the function - you are not actually calling it which is why the compiler is complaining.
Upvotes: 1
Reputation: 247899
Yes, the problem is the line
void read_data(finput, foutput);
inside the main function. Don't specify the return type when calling the function. Only when declaring it. In other words, the line should just read
read_data(finput, foutput);
Upvotes: 6