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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
//Tylr Taormina
//CST 116
//Dec 3, 2021
#include <iostream>
#include <fstream> // For the files!!!!
#include <iomanip> // For manipulators & formatting options
#include <stdio.h>
#include <string>
#include <algorithm>
using std::cin;
using std::cout;
using std::endl;
using std::setw;
using std::ios;
using std::resetiosflags;
using std::setiosflags;
using std::ifstream;
using std::ofstream;
const int MAX = 10;
int ReadData (ifstream &inFile, int data_arr[]);
void Sort (int data_arr[], int counter);
void Display_LS(int data_arr[], int counter);
int main()
{
std::string input;
int i;
int record_counter = 0;
int data[MAX];
int flag;
cout << "THE FILES WE HAVE TO CHOOSE FROM: " << endl;
cout << "===================================================" << endl;
cout << "data.txt" << endl;
cout << "data2.txt" << endl;
cout << "hello.txt\n\n" << endl;
cout << "Please enter a name for the file to open: ";
cin >> input;
transform(input.begin(), input.end(), input.begin(), ::tolower);
ifstream inFile;
// Notice how this automatically opens the file
inFile.open (input);
if ( inFile.is_open ( ) )
{
record_counter = ReadData (inFile, data);
inFile.close ( );
flag = 1;
}
else
{
cout << "Trouble Opening File: " << input << endl;
cout << "Check to make sure your spelling is correct for the file you are trying to open. Include .txt or whatever file extension is relevant." << endl;
cout << "\n\n\t\t ** About to EXIT NOW! ** ";
flag = 0;
}
if (flag == 1)
{
Display_LS(data, record_counter);
Sort(data, record_counter);
for (i = 0; i < record_counter; i++)
cout << data[i] << endl;
}
return 0;
}
int ReadData(ifstream &inFile, int data_arr[])
{
int counter = 0;
inFile >> data_arr[counter]; // Priming Read
while ( !inFile.eof ( ) )
{
counter++;
inFile >> data_arr[counter] ;
}
return counter;
}
void Sort (int data_arr[], int counter)
{
int i;
int j;
int temp;
for (i = 0; i < counter; i++)
{
for (j = i+1; j < counter; j++)
{
if (data_arr[i] > data_arr[j])
{
temp = data_arr[i];
data_arr[i] = data_arr[j];
data_arr[j] = temp;
}
}
}
}
void Display_LS(int data_arr[], int counter)
{
int large, small;
int i;
small = data_arr[0];
large = data_arr[0];
for (i = 1; i < counter; i++)
{
if (small > data_arr[i])
small = data_arr[i];
if (large < data_arr[i])
large = data_arr[i];
}
cout << "The smallest number is: " << small << endl;
cout << "The largest number is: " << large << endl;
}
|