1 |
|
|
// |
2 |
|
|
// A simple program that outputs the square root of a number |
3 |
|
|
// and the sum of two of this number just for example ... |
4 |
|
|
// |
5 |
|
|
#include <fmt/core.h> |
6 |
|
|
#include <mathfunctions/Addition.h> |
7 |
|
|
#include <mathfunctions/SquareRoot.h> |
8 |
|
|
|
9 |
|
|
#include <exception> |
10 |
|
|
#include <iostream> |
11 |
|
|
#include <string> |
12 |
|
|
|
13 |
|
4 |
auto main(int argc, char* argv[]) -> int { |
14 |
✓✓ |
4 |
if (argc < 2) { |
15 |
|
1 |
fmt::print(stderr, "Usage: {} number\n", *argv); |
16 |
|
1 |
return 0; // Note: not an error, just a hint ;-) |
17 |
|
|
} |
18 |
|
|
|
19 |
|
|
// convert input to double |
20 |
|
3 |
double inputValue{1.0}; |
21 |
|
|
try { |
22 |
✓✗✓✓
|
3 |
inputValue = std::stod(argv[1]); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) |
23 |
✓✗ |
3 |
} catch (const std::exception& e) { |
24 |
✓✗ |
1 |
fmt::print(stderr, "Oops: {}; this is not valid number: {}!\n", e.what(), argv[1]); |
25 |
|
1 |
return 0; |
26 |
|
1 |
} |
27 |
|
|
|
28 |
|
|
// calculate square root |
29 |
|
2 |
const double sqrt = MathFunctions::sqrt(inputValue); |
30 |
|
2 |
fmt::print("The square root of {} is {}\n", inputValue, sqrt); |
31 |
|
|
|
32 |
|
|
// calculate sum |
33 |
|
2 |
const double sum = MathFunctions::add(inputValue, inputValue); |
34 |
|
2 |
fmt::print("{0} + {0} = {1}\n", inputValue, sum); |
35 |
|
|
|
36 |
|
2 |
return 0; |
37 |
|
5 |
} |