-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddData.cpp
More file actions
115 lines (102 loc) · 3.65 KB
/
addData.cpp
File metadata and controls
115 lines (102 loc) · 3.65 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
#include <iostream>
#include <fstream>
#include "include\nlohmann\json.hpp"
#include <string>
#include <iomanip>
using json = nlohmann::json;
using namespace std;
float take_valid_float(string input_request){
bool valid = false;
float input;
do {
cout << input_request;
cin >> input;
if (cin.good()) {
valid = true;
} else {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Invalid input; please re-enter.\n";
}
} while (!valid);
return input;
}
int take_valid_integer(string input_request){
bool valid = false;
int input;
do {
cout << input_request;
cin >> input;
if (cin.good()) {
valid = true;
} else {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Invalid input; please re-enter.\n";
}
} while (!valid);
return input;
}
int main(){
const string location = "calcData.json";
const string days[5] = {"Mon", "Tue", "Wed", "Thu", "Fri"};
ifstream fin {location};
string library;
int year, b;
float m;
json data;
fin >> data;
char choice = 'y';
while (choice == 'y') {
library = "";
cout << "Enter Library Jnr/Snr: ";
cin >> library;
while (library != "Jnr" && library != "Snr") {
cout << "Invalid input please retner library (Jnr/Snr): ";
cin >> library;
library[0] = toupper(library[0]);
library[1] = tolower(library[1]);
library[2] = tolower(library[2]);
}
bool valid = false;
do {
cout << "Enter year: ";
cin >> year;
if (cin.good()) {
if (year > 2010) {
valid = true;
} else {
cout << "Value entered for year too low re-enter.\n";
}
} else {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input; please re-enter.\n";
}
} while (!valid);
for (int i = 1; i < 5; i++) { /*For the 4 terms*/
for (int j = 0; j < z5; j++) { /*Each day of week*/
cout << "For term " << i << " " << days[j] << " enter the following values for:";
cout << "\n\tbefore school:\n";
m = take_valid_float("\tGradient (m): ");
b = take_valid_integer("\tConstant (b): ");
data[library][to_string(year)]["Term"+to_string(i)][days[j]]["bs"]["m"] = m;
data[library][to_string(year)]["Term"+to_string(i)][days[j]]["bs"]["b"] = b;
cout << "\n\tbreak 1:\n";
m = take_valid_float("\tGradient (m): ");
b = take_valid_integer("\tConstant (b): ");
data[library][to_string(year)]["Term"+to_string(i)][days[j]]["b1"]["m"] = m;
data[library][to_string(year)]["Term"+to_string(i)][days[j]]["b1"]["b"] = b;
cout << "\n\tbreak 2:\n";
m = take_valid_float("\tGradient (m): ");
b = take_valid_integer("\tConstant (b): ");
data[library][to_string(year)]["Term"+to_string(i)][days[j]]["b2"]["m"] = m;
data[library][to_string(year)]["Term"+to_string(i)][days[j]]["b2"]["b"] = b;
}
}
ofstream fout{location};
fout << setw(4) << data;
cout << "Data saved, continue to add more data (y/n): ";
cin >> choice;
}
}