Hello so this is the assignment statement:
o return 0 if you set the first and last numbers from the file; return -1 if the file doesn't open or read properly
o Return the first number in the file and the last number in the file through the reference parameters. Use a while loop to read until EOF to get to the last number.
o Close the file in the function.
If extraction fails (e.g. if a letter was entered where a digit is expected), value is left unmodified and failbit is set. (until C++11)
If extraction fails, zero is written to value and failbit is set. If extraction results in the value too large or too small to fit in value, std::numeric_limits<T>::max() or std::numeric_limits<T>::min() is written and failbit flag is set. (since C++11)
¿is `last' equal to 0?
in that case you may modify the loop
#include <string>
#include <fstream>
// Return the first number in the file and the last number in the file
// through the reference parameters.
int firstLast( const std::string& filename, double& first, double& last )
{
if( std::ifstream file{filename} ) // if the file was successfully opened for input
{
if( file >> first >> last ) // if at least two numbers were read from the file
{
// Use a while loop to read until EOF to get to the last number.
double temp = 0 ;
while( file >> temp) last = temp ; // keep reading and assigning to last till attempt to read fails
return 0 ; // return 0 if you set the first and last numbers from the file
}
// Close the file in the function
// the file is automagically closed when the variable goes out of scope
}
// return -1 if the file doesn't open or the file doesn't read properly
return -1 ;
}