How do I call out this function?

//Prototype

int readRainFall (ifstream& inFileName, double rainfalls[], int MONTHS);


//define input
int readRainFall (ifstream& inFile, double rainfalls[], int MONTHS)
{
string inFileName;
int count=0;
cout << "Please enter the name of the input file: ";
cin>>inFileName;
inFile.open(inFileName);
if (!inFile.is_open())
{
cout << "Error opening file"<<endl;
}
else {
while(count<MONTHS && inFile >> rainfalls[count])
count++;
}
inFile.close();
return 0;


}



I put this, but it was wrong:

readRainFall ( inFile "in6s17.txt", rainfallAmount, MONTHS);
Rather than using a c-style array you could read the file into std::vector<double>. That way you don't need to make any a-priori assumption about the number of months' data in the file or how big a vector you need. You could std::vector::reserve() some size beforehand if you have an idea about the number of months or else if the # of months is not too large std::vector::push_back() should be sufficient …
the return value of the function, which returns int in the header but says return 0 within the function body – this needs to be corrected – it'd probably be the average rainfall per month (in which case return type should be double) or number of months for which rainfall data was recorded (in which case size_t return type could also work) – either of these can be gleaned from the vector quite easily
Last edited on
Topic archived. No new replies allowed.