Finding the largest number in a file.

Alright, so my goal here is to find the largest number in a file and output it to another file. My program asks the user to enter the name of the output file. No idea how to go about this. Thanks! Apologize in advance, my code is messy right now.

In main
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
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include "Functions.h"

using namespace std;
int main()
{
    string name[1000];
    string destination[1000];
    double total[1000];
    int count;

    Read(name, destination, total, count);




    if (Read(name, destination, total, count))
    {
        for (int i = 0; i < count; ++i)
        {
            cout << name[i] << "" << destination[i] << " " << total[i] << endl;
        }
    }
    string outFile;
    cout << "Please enter filename:  ";
    getline(cin, outFile);
    //Open ofstream
    ofstream out(outFile);

    if (out.is_open())
    {

        if (Read(name, destination, total, count))
        {
            for (int i = 0; i < count; ++i)
            {
                cout << name[i] << "" << destination[i] << "" << total[i] << endl;
            }
        }

        return 0;




    }

}


In Functions.cpp (where my functions are defined)
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
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
using namespace std;

bool Read(string name[], string destination[], double total[], int& count)
{
    ifstream  infile;
    infile.open("TravelSept2015toPresent-en-us.csv");
    if (!infile)
    {
        cout << "Could not open file!" << endl;
        return false;
    }

    string line;
    // Get ride of header
    getline(infile, line);
    string element;
    count = 0;
    while (getline(infile, element, '\t'))
    {
        getline(infile, element, '\t');
        name[count] = element;
        getline(infile, element, '\t');
        destination[count] = element;
        getline(infile, element, '\t');
        //cout << element << "\t";
        getline(infile, element, '\t');
        //cout << element << "\t";
        getline(infile, element, '\t');
        //cout << element << "\t";
        getline(infile, element, '\t');
        //cout << element << "\t";
        getline(infile, element, '\t');
        //cout << element << "\t";
        getline(infile, element, '\t');
        //cout << element << "\t";
        getline(infile, element);
        total[count] = stod(element);
        count++;
    }
}
bool AnalyzeMaxNumber(double total[])
{
    cout << total << endl;
    return true;
}


In a header file (where the functions are prototyped)
1
2
3
4
5
6
7
8
9
#ifndef _FUNCTIONS_H
#define _FUNCTIONS_H
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
bool Read(string name[], string destination[], double total[], int& count);
bool AnalyzeMaxNumber(double total[]);
#endif 
Last edited on
Forgot to mention, this is the infile that I'm using.
https://mega.nz/file/OJNCTQjL#o2Se58Rlvl9KfBwwqdh9wH86PjQf46rpVNMyPrEQEwg
Your file contains a bunch of fields on each line. Which field's number are you looking for? Is the output just the number or the whole field?

Please post the first 5 or 10 lines of the input file and the exact expected output when given just those lines for the input.
assuming that your file contains space seperated numbers you could do something like this

myFile.txt
 
12 47 8 66 99 1 -4

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

bool GetMax(const std::string &filename, int &max)
{
    std::ifstream file(filename);
    if (file.fail())
    {
        std::cerr << "failed to open file" << std::endl;
        return false;
    }
    
    bool firstRead = true;

    while (true)
    {
        int number;
        file >> number;

        if (!file) break; // are we done yet?

        if (firstRead) { max = number; firstRead = false; }
        else if (number > max) { max = number; }
    }

    file.close();
    return true;
}

// main
int main()
{
    int max;

    if (GetMax("myFile.txt", max))
        std::cout << "the max is " << max << std::endl;
    
}


I hope that I helped you
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <fstream>
#include <sstream>
#include <iterator>
#include <algorithm>
using namespace std;

int main()
{
// ifstream      in( "myfile.txt" );
   istringstream in( "12 47 8 66 99 1 -4" );

   cout << "Maximum is " << *max_element( istream_iterator<int>{ in }, {} ) << '\n';
}

Maximum is 99
Last edited on
Topic archived. No new replies allowed.