forked from portfoliocourses/cplusplus-example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlargest_num_in_file.cpp
More file actions
117 lines (99 loc) · 3.68 KB
/
largest_num_in_file.cpp
File metadata and controls
117 lines (99 loc) · 3.68 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*******************************************************************************
*
* Program: Find The Largest Number In A File
*
* Description: Program to find the largest number in a file using C++.
*
* YouTube Lesson: https://www.youtube.com/watch?v=PwU6sAkGYJs
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
// This program assumes the file is formatted as follows, with a number on each
// line, possibly a real number number that includes decimal places and not
// only integers without decimal places:
//
// 4.4
// 5.3
// 9.5
// 2.8
// 10.2
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
// Declare a string variable to store the name of the file
string filename;
// Prompt the user to enter the name of the file, store what they enter
// into the filename string variable
cout << "Filename: ";
getline(cin, filename);
// Create an ifstream object to access the file
ifstream file;
// Use the open() member function of the ifstream object to open the file
// with the provided filename so we can read the numbers from the file
file.open(filename);
// It's possible an error could occur when opening the file, for example the
// file may not exist. If an error occurs, the fail() member function will
// return true, in which case the program will exit with an error message
// and status.
if (file.fail())
{
// Output 'error opening file' as an exit message to inform the user what
// has gone wrong.
cout << "Error opening file!" << endl;
// Returning 1 will exit the program with an error status
return 1;
}
// Declare number to store each number that is read from the file
double number = 0;
// Declare max to store the largest number read thus far from the file
double max = 0;
// Declare firstNumber to keep track of whether we have read the first
// number from the file or not.
bool firstNumber = true;
// We can read each number from the file using the stream extraction operator
// >> with file >> number. file >> number will return true if a number is
// successfully read from the file, and false when we reach the end of the
// file. As a result the loop will continue to execute until we reach the
// end of the file, and with each iteration of the loop the variable
// 'number' will be set to the next number in the file.
while (file >> number)
{
// If this is the first number we've read from the file, by default it's
// also the maximum number we've read from the file so far, so we set
// max to the number read from the file.
if (firstNumber)
{
max = number;
// Acknowledge we've read a number by setting firstNumber to false
firstNumber = false;
}
// Otherwise if it's not the first number we've read from the file we
// check if it is larger than max (the largest number we've read so
// far), and if it is we update max and set it to the number.
else if (number > max)
{
max = number;
}
}
// By the end of the loop max will contain the largest number found in the
// file as we will have checked all numbers in the file and updated max
// whenever we found a number largest than max.
//
// If firstNumber is still set to true that means we NEVER read any numbers
// from the file, in which case we'll output a message indicating this.
// Otherwise we'll output the largest number we read from the file.
if (firstNumber)
{
cout << "No numbers read from file" << endl;
}
else
{
cout << "Max: " << max << endl;
}
// Close the file as we are done working with it now
file.close();
return 0;
}