forked from IMACULGY/CPPExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassifyNumbers.cpp
More file actions
136 lines (103 loc) · 3.36 KB
/
ClassifyNumbers.cpp
File metadata and controls
136 lines (103 loc) · 3.36 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <filesystem>
using namespace std;
//Function prototypes
void initialize(int & zeroCount, int & oddCount, int & evenCount, int & counter, int & sum);
void getNumber(int & line, ofstream & outputFile, int & counter);
void classifyNumber(int num, int & zeroCount, int & oddCount, int & evenCount, int& sum);
void printResults(int zeroCount, int oddCount, int evenCount, int sum, float average, ofstream& outputFile);
int main() {
//Variable declaration
int counter; //loop control variable
int number; //variable to store the new number
int zeros; //variable to store the number of zeros
int odds; //variable to store the number of odd integers
int evens; //variable to store the number of even integers
int sum; //variable to store the sum of all numbers
float average; //variable to store average of all numbers
string inputFileName; //variable to store input file's name
string outputFileName; //variable to store output file's name
initialize(zeros, odds, evens, counter, sum); //Step 1
cout << "Please enter input file name: " << flush;
cin >> inputFileName;
ifstream inputFile(inputFileName);
if (!inputFile) {
cerr << "Error: Could not open file for reading" << endl;
return 1;
}
cout << "Please enter output file name: " << flush;
cin >> outputFileName;
cin.ignore();
if (filesystem::exists(outputFileName)) {
cerr << "Output file already exists" << endl;
inputFile.close();
return 1;
}
ofstream outputFile(outputFileName);
if (!outputFile) {
cerr << "Error: Could not open file for writing" << endl;
inputFile.close();
return 1;
}
string line;
while (getline(inputFile, line)) {
if (line.empty()) {
continue;
}
try {
number = stoi(line);
}
catch (const exception& e) {
cerr << e.what() << endl;
inputFile.close();
outputFile.close();
return 1;
}
getNumber(number, outputFile, counter);
classifyNumber(number, zeros, odds, evens, sum);
}
if (counter % 11 != 0) {
outputFile << endl;
}
average = sum/(1.0*(odds + evens));
printResults(zeros, odds, evens, sum, average, outputFile); //Step 4
inputFile.close();
outputFile.close();
return 0;
}
void initialize(int & zeroCount, int & oddCount, int & evenCount, int & counter, int & sum) {
zeroCount = 0;
oddCount = 0;
evenCount = 0;
counter = 1;
sum = 0;
}
void getNumber(int & line, ofstream & outputFile, int & counter) {
outputFile << line << " " << flush;
if (counter % 10 == 0) {
outputFile << endl;
}
counter ++;
}
void classifyNumber(int num, int & zeroCount, int & oddCount, int & evenCount, int& sum) {
sum += num;
switch (num % 2) {
case 0:
evenCount++;
if (num == 0)
zeroCount++;
break;
case 1:
case -1:
oddCount++;
} //end switch
} //end classifyNumber
void printResults(int zeroCount, int oddCount, int evenCount, int sum, float average, ofstream& outputFile) {
outputFile << "\nThere are " << evenCount << " evens, " << "which includes " << zeroCount << " zeros" << endl;
outputFile << "The number of odd numbers is: " << oddCount << endl;
outputFile << "The sum of the numbers is: " << sum << endl;
outputFile << "The average of the numbers is: " << average << endl;
} //end printResults