returning the first and last numbers in a file

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.

This is what I have:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <algorithm>

using namespace std;

int firstLast(string filename, double& first, double& last);

int main()
{
int ret2;
double first, last;
string filename;
ifstream file;

cout << "Enter a filename to find a max and min: ";
cin >> filename;

ret2 = firstLast( filename, first, last );
cout << "FIRST=" << first << ", LAST=" << last << endl << endl;

}

int firstLast(string filename, double& first, double& last)
{
ifstream fn;
fn.open(filename.c_str());
if ( !fn )
{
return -1;
}

fn >> first >> last;
while ( fn )
{
fn >> last;
}
fn.close();
return 0;
}

basically my error message is :
a direct call to firstLast() does not produce the correct LAST.

I am only supposed to edit the function int firstLast()
anyone have any idea?
> does not produce the correct LAST.
your function would give an incorrect result if the file is empty or only holds one number.

https://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt
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
1
2
3
double input;
while(fn >> input)
   last = input;
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
#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 ;
}

http://coliru.stacked-crooked.com/a/3d37aaa57a31ea47
Last edited on
Registered users can post here. Sign in or register to post.