-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
97 lines (78 loc) · 1.84 KB
/
main.cpp
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
#include <iostream>
#include <string>
bool CheckStringForDoubleConvert(std::string& str)
{
if (str.empty())
return false;
for (auto& c : str)
{
if (!(c == '.' || c == ',' || isdigit(c)))
return false;
}
return true;
}
double ConvertToDouble(std::string& str)
{
while (!CheckStringForDoubleConvert(str))
{
std::cout << "Wrong Input! Try again: ";
std::getline(std::cin, str);
}
return std::stod(str);
}
int main()
{
double f, m, a;
std::string temp;
std::cout << "What would you like to calculate?" << '\n';
std::cout << "Type F for force, M for mass, or A for acceleration: ";
while(true)
{
//Get Input
std::getline(std::cin, temp);
//Calculate force
if (temp == "f" || temp == "F")
{
std::cout << "What is the acceleration? ";
std::getline(std::cin, temp);
a = ConvertToDouble(temp);
std::cout << "What is the mass? ";
std::getline(std::cin, temp);
m = ConvertToDouble(temp);
f = m * a;
std::cout << "The force is " << f << '\n' << '\n';
break;
}
//Calculate mass
if (temp == "m" || temp == "M")
{
std::cout << "What is the acceleration? ";
std::getline(std::cin, temp);
a = ConvertToDouble(temp);
std::cout << "What is the force? ";
std::getline(std::cin, temp);
f = ConvertToDouble(temp);
m = f / a;
std::cout << "The mass is " << m << '\n' << '\n';
break;
}
//Calculate acceleration
if (temp == "a" || temp == "A")
{
std::cout << "What is the force? ";
std::getline(std::cin, temp);
f = ConvertToDouble(temp);
std::cout << "What is the mass? ";
std::getline(std::cin, temp);
m = ConvertToDouble(temp);
a = f / m;
std::cout << "The acceleration is " << a << '\n' << '\n';
break;
}
//Wrong Input
std::cout << "Wrong Input! Try again: ";
}
std::cout << "Press Enter to continue . . . ";
std::cin.get();
return 0;
}