C++
x
73
1
2
3
using std::cout;4
using std::string;5
using std::cin;6
7
int main()8
{9
// Prompts Input10
cout << "Enter a double value followed by a unit (cm, m, in, ft): ";11
12
// Variables13
bool first {true};14
double input {0.0};15
double smallest {0.0};16
double largest {0.0};17
string unit {};18
19
// Check Input20
while(cin >> input >> unit)21
{22
// Convert unit to m23
if (unit == "cm")24
{25
input /= 100;26
unit = "m";27
}28
else if (unit == "in")29
{30
input /= 39.37;31
unit = "m";32
}33
else if (unit == "ft")34
{35
input /= 3.281;36
unit = "m";37
}38
else if (unit == "m")39
{40
unit = "m";41
}42
else {43
break;44
}45
46
// Print input and unit47
cout << input << unit << "\n";48
49
// Main Loop50
if(first == true) 51
{52
first = false;53
smallest = input;54
largest = input;55
cout << input << unit << " is the first value therefore is the smallest and largest so far. \n";56
}57
else if(input > largest)58
{59
cout << input << unit << " is the largest so far. \n";60
largest = input;61
}62
else if (input < smallest)63
{ 64
cout << input << unit << " is the smallest so far. \n";65
smallest = input;66
}67
else68
{69
cout << input << unit << "\n";70
}71
}72
}73
$ g++ prog.cc -Wall -Wextra -std=c++17 -pedantic
Stdin
200m23cm39ftStart
Enter a double value followed by a unit (cm, m, in, ft): 200m 200m is the first value therefore is the smallest and largest so far. 0.23m 0.23m is the smallest so far. 11.8866m 11.8866m
0
Finish