forked from leonor-loureiro/codespaces-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
66 lines (62 loc) · 1.68 KB
/
main.cpp
File metadata and controls
66 lines (62 loc) · 1.68 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
#include <iostream>
#include <string>
#include <sstream>
int main(int argc, char *argv[])
{
std::cout << "Unit Converter CLI App\n";
std::cout << "Type commands (help, cm-to-inch <cm>, inch-to-cm <inch>, exit):\n";
std::string line;
while (true)
{
std::cout << "> ";
if (!std::getline(std::cin, line))
break;
if (line.empty())
continue;
std::istringstream iss(line);
std::string cmd;
iss >> cmd;
if (cmd == "exit")
{
break;
}
else if (cmd == "help")
{
std::cout << "Commands:\n";
std::cout << " cm-to-inch [cm] - Convert centimeters to inches\n";
std::cout << " inch-to-cm [inch] - Convert inches to centimeters\n";
std::cout << " exit - Quit the app\n";
}
else if (cmd == "cm-to-inch")
{
double cm;
if (iss >> cm)
{
double inch = cm / 2.54;
std::cout << cm << " cm = " << inch << " inches\n";
}
else
{
std::cout << "Usage: cm-to-inch [cm]\n";
}
}
else if (cmd == "inch-to-cm")
{
double inch;
if (iss >> inch)
{
double cm = inch * 2.54;
std::cout << inch << " inches = " << cm << " cm\n";
}
else
{
std::cout << "Usage: inch-to-cm [inch]\n";
}
}
else
{
std::cout << "Unknown or incomplete command. Use 'help' for instructions.\n";
}
}
return 0;
}