Find the highest number in a file

I am able to generate 100 random numbers but i need to find the highest after that. What i have now gives me the last number in the sequence not the highest. Any help is appreciated.

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
 #include <iostream>
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
	ofstream outputFile;  // File stream object
	ifstream inputFile;
	int numberOfNums = 100;  // Number of loops
	int num;         // Sales amount for a day
	int number;
	int Largest = 0;

	srand(time(NULL));

	const int MIN_VALUE = 1;   // Minimum value
	const int MAX_VALUE = 100;//Maximum value

							  // Open a file named Numbers.txt.
	outputFile.open("Numbers.txt");

	// Get a number and write it
	// to the file.
	for (int count = 1; count <= numberOfNums; count++)
	{
		//Get a number
		num = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;
		cout << num << endl;



		// Write the number to the file.
		outputFile << num << endl;
	}

	// Close the file.
	outputFile.close();

	cout << "Data written to Numbers.txt\n";

	//+++++++++++++++++++++++++++++++++++++++++++

	// This program reads data from a file.
	// Open the file.

	inputFile.open("Numbers.txt");

	// Read the numbers from the file and
	// display them.
	cout << "Printing inputFile contents\n";
	while (inputFile >> number)
	{
		if (num > Largest)
		{
			cout << " The largest number is: ";
			Largest = num;
			cout << num << endl;
		}
	}

	// Close the file.
	inputFile.close();
	cout << "inputFile closed\n";

	return 0;
}
closed account (48T7M4Gy)
if( number> largest)
That just seems to make the program give me my last number a random amount of times instead of the highest once. Was i suppose to replace my if statement on line 57 with yours or add yours somewhere else?
Tip: avoid declaring a bunch of variable right at the beginning, much before they are actually used.
Postpone declarations to as late as possible and the code becomes less error prone, more readable and in some cases more efficient.

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
52
53
54
55
56
57
58
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>

int main()
{
	const int MIN_VALUE = 1;   // Minimum value
	const int MAX_VALUE = 100;//Maximum value

	// sanity check
	static_assert( MAX_VALUE >= MIN_VALUE, "invalid min/max values" ) ;
	const int RANGE = MAX_VALUE - MIN_VALUE + 1 ;

	const int NUMBER_OF_NUMS = 100; // const
	const std::string file_name = "Numbers.txt" ;

    std::srand( std::time(nullptr) ) ; // consider using facilities in <random>
                             // http://en.cppreference.com/w/cpp/numeric/random

    {
        // Open the file for output.
        std::ofstream outputFile(file_name);

        // canonical for loop to loop numberOfNums times
        for( int count = 0 ; count < NUMBER_OF_NUMS; ++count )
        {
            // Get a number and write it to the file.
            outputFile << ( rand() % RANGE + MIN_VALUE ) << '\n' ;
        }

        // outputFile is automagically closed when we exit from the block scope
    }

    std::cout << "Data written to " << file_name << '\n' ;

    //+++++++++++++++++++++++++++++++++++++++++++

    std::cout << "Printing inputFile contents\n";

    // Open the file for input.
    std::ifstream inputFile(file_name) ;

    int largest = MIN_VALUE ;
    int number ;

    // Read the numbers from the file and display them.
    while( inputFile >> number )
    {
        std::cout << number << '\n' ;
        if( number > largest ) largest = number ;
    }

    std::cout << "\nthe largest number is: " << largest << '\n' ;

    // inputFile is automagically closed when we return from main
}
use sort to get the largest number
while it does not matter for tiny problems, sorting (NlgN run time) vs just peeling off the value (N) is a bit wasteful.

OP, you are on the right track...

as stated, but not explicitly

while (inputFile >> number)
{
if (num > Largest)

NUM and NUMBER are not the same thing. You mismatched your variables and created a bug.

Note that you also assign off num. But num is constant in this loop. All 3 uses need to be number. Or num. But not mixed.


Last edited on
closed account (48T7M4Gy)
Much the same as previous advice showing another way of doing comments and combining with more obvious variable names which add to self-documenting code. Note that comments can be derived directly fro pseudo-code, albeit that 'nobody ever does that'.

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <string>
#include <fstream>
#include <ctime>

using namespace std;

int main()
{
    // +++ PART (A) WRITE TO FILE +++
    
    // SETUP OUTPUT (WRITE) FILE STREAM
    ofstream outputFile;
    
    // TRY AND OPEN OUTPUT FILE
    outputFile.open("numbers.txt");
    if(outputFile.is_open())
    {
        // PROCEED WITH GENERATING NUMBERS
        int limit = 100;
        int random_number = 0;
        
        // SETUP RANDOM NUMBER GENERATOR
        srand(time(NULL));
        const int MIN_VALUE = 1;
        const int MAX_VALUE = 100;
        
        // GENERATE RANDOM VALUES, WRITE TO FILE AND DISPLAY
        for (int count = 1; count <= limit; count++)
        {
            random_number = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;
            
            cout << random_number << endl;
            outputFile << random_number << endl;
        }
        
        outputFile.close();
        cout << "\tData written to numbers.txt and file closed\n";
    }
    else
    {
        cout << "Output file failed - program aborted\n";
        return -1;
    }
    
    
    // +++ PART (B) READ FROM FILE +++
    
    // PREPARE INPUT (READ) STREAM
    ifstream inputFile;
    
    // TRY AND OPEN FILE
    inputFile.open("numbers.txt");
    
    if(inputFile.is_open())
    {
        int largest = 0;
        int number_from_file = 0;
        cout << "\tPrinting inputFile contents\n";
        
        // READ IN AND EVALUATE NUMBERS FROM FILE
        while (inputFile >> number_from_file)
        {
            if (number_from_file > largest)
            {
                largest = number_from_file;
            }
        }
        
        // ALL DONE CLOSE OFF FILE
        inputFile.close();
        cout << "\tData all read from numbers.txt and file closed\n";
        
        // DISPLAY RESULT
        cout << "THE LARGEST NUMBER IS: " << largest << endl;
    }
    else
    {
        cout << "Input file failed - program aborted\n";
        return -2;
    }
    
    return 0;
}
93
38
45
45
47
100
13
etc etc
9
90
90
48
52
1
	Data written to numbers.txt and file closed
	Printing inputFile contents
	Data all read from numbers.txt and file closed
THE LARGEST NUMBER IS: 100
Program ended with exit code: 0
Last edited on
closed account (48T7M4Gy)
Note: MAX_VALUE = 1000 is probably a more convincing choice, or alternatively limit = 50 say.
Topic archived. No new replies allowed.