-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileProcessing.cpp
More file actions
133 lines (101 loc) · 3.47 KB
/
Copy pathFileProcessing.cpp
File metadata and controls
133 lines (101 loc) · 3.47 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
// Title: Lab 1 - FileProcessing.cpp
//
// Purpose: **Get the input of json file and output in csv format. finding the minimum, maximum, and average age
//
// Class: CSC 2430 Winter 2022
// Author: **Kainoa Aqui
#include <cassert>
#include "Formatting.h"
#include "FileProcessing.h"
#include <string>
#include <iostream>
#include <fstream>
using std :: string;
using std:: cout;
using std:: cin;
using std :: endl;
using std :: ifstream;
using std :: ofstream;
// Operates in a loop so multiple files can be processed.
// On each iteration, reads name of input and output files from user and opens them
// Then calls ProcessFile to do the actual file processing
// - If user enters a blank line for the input file, loop ends
// - If either file cannot be opened, loop restarts
//
// Parameters:
// none
// Returns:
// nothing
void ProcessFile(ifstream& in,ofstream& out);
void ProcessFiles() {
string inputFileName;
string outputFileName;
bool inputDone = false;
while (!inputDone) {
cout << "Input File Name: ";
getline(cin, inputFileName);
char period = '.';
int foundPeriod = inputFileName.find(period);
string findJson = "json";
int foundJson = inputFileName.find(findJson);
if (inputFileName == "") {
break;
}
// breaks if blank entered
if (foundPeriod == string::npos) {
cout << "ERROR: " << inputFileName << " not found" << endl;
continue;
}
// error message if no period is found
if (foundPeriod != string::npos && foundJson == string::npos) {
cout << "ERROR: Incorrect file format, please provide a JSON file" << endl;
continue;
}
// error if period found but not in json format
cout << "Output File Name: " ;
getline(cin, outputFileName);
ifstream inputFile(inputFileName);
if (!inputFile.is_open()) {
cout << "Could not open file: " << inputFileName;
cout << endl;
continue;
// checks if file exists
}
ofstream outputFile(outputFileName);
if (!outputFile.is_open()) {
cout << "Could not open file: " << outputFileName;
cout << endl;
continue;
}
ProcessFile(inputFile, outputFile);
inputFile.close();
outputFile.close();
}
}
void ProcessFile(ifstream& in,ofstream& out) {
string json;
int minAge = INT_MAX;
int maxAge = INT_MIN;
int ageCount = 0;
double aveAge;
double totalAge = 0.0;
while (getline(in, json)) {
string personAge = GetAge(json);
int stringAgeToInt = atoi(personAge.c_str()); // changes string to integer
if (stringAgeToInt >= 0) {
totalAge = totalAge + stringAgeToInt;
ageCount++; // stores these values to calculate average
if (stringAgeToInt > maxAge) {
maxAge = stringAgeToInt; // finding maximum age
}
if (stringAgeToInt < minAge) {
minAge = stringAgeToInt; // finding minimum age
}
}
}
in.close();
aveAge = totalAge / ageCount; // calculates average age
cout << "Minimum age: " << minAge << endl;
cout << "Maximum age: " << maxAge << endl;
cout << "Average age: " << aveAge << endl;
}