-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertor_function.cpp
More file actions
100 lines (78 loc) · 2.78 KB
/
convertor_function.cpp
File metadata and controls
100 lines (78 loc) · 2.78 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
#include <iostream>
#include <string>
#include "log_function.cpp"
#include "timestamps.cpp"
void start();
std::string getConversionChoice();
void performConversion(std::string const &conversionChoice);
void performLengthConversion();
void performWeightConversion();
void ask_for_exit();
void start() {
std::string conversionChoice = getConversionChoice();
performConversion(conversionChoice);
ask_for_exit();
}
std::string getConversionChoice() {
std::string conversionChoice;
std::cout << "Choose conversion type \n 1: cm to inches \n 2: kg to lbs \n";
if (std::getline(std::cin >> std::ws, conversionChoice)) {
return conversionChoice;
}
return "";
}
void performConversion(std::string const &conversionChoice) {
if (conversionChoice == "1" || conversionChoice == "cm to inches" || conversionChoice == "CM TO INCHES") {
performLengthConversion();
} else if (conversionChoice == "2" || conversionChoice == "kg to lbs" || conversionChoice == "KG TO LBS") {
performWeightConversion();
} else {
std::cout << "Error: You have entered an incorrect value." << std::endl;
}
}
float roundoff(float value, unsigned char prec) {
float pow_10 = pow(10.0f, (float) prec);
return round(value * pow_10) / pow_10;
}
std::string roundOffString(float value, unsigned char prec) {
float number = roundoff(value, prec);
std::string num_text = std::to_string(number);
std::string rounded = num_text.substr(0, num_text.find(".") + 3);
return rounded;
}
void performLengthConversion() {
float inch, cen;
int feet;
std::cout << "Enter the value in centimeters: ";
std::cin >> cen;
inch = cen / 2.54f;
feet = static_cast<int>(inch / 12);
inch = inch - static_cast<float>(feet * 12);
std::string resultText =
roundOffString(cen, 3) + " centimeters are equal to " + roundOffString(feet, 3) + " feet " +
std::to_string(inch) + " inch(es).";
std::string resultWithTimestamp = timestamp() + " " + resultText;
write_log_file(resultWithTimestamp);
std::cout << resultWithTimestamp << std::endl;
}
void performWeightConversion() {
float kg, lbs;
std::cout << "Enter the value in kilograms: ";
std::cin >> kg;
lbs = kg / 2.20462f;
lbs = roundoff(lbs, 3);
std::string resultText = roundOffString(kg, 3) + " kilograms are equal to " + roundOffString(lbs, 3) + " pounds.";
std::string resultWithTimestamp = timestamp() + " " + resultText;
write_log_file(resultWithTimestamp);
std::cout << resultWithTimestamp << std::endl;
}
void ask_for_exit() {
char YesNo;
std::cout << "Do you want to continue? (Y or N) \n";
std::cin >> YesNo;
if (YesNo == 'N' || YesNo == 'n') {
//do nothing, just quit
} else {
start();
}
}