cmake/Help/guide/tutorial/Step10/tutorial.cxx

37 lines
904 B
C++
Raw Normal View History

2019-11-11 23:01:05 +01:00
// A simple program that computes the square root of a number
2022-11-16 20:14:03 +01:00
#include <cmath>
2019-11-11 23:01:05 +01:00
#include <iostream>
#include <string>
#include "TutorialConfig.h"
2022-11-16 20:14:03 +01:00
// should we include the MathFunctions header?
#ifdef USE_MYMATH
# include "MathFunctions.h"
#endif
2019-11-11 23:01:05 +01:00
int main(int argc, char* argv[])
{
if (argc < 2) {
2020-02-01 23:06:01 +01:00
// report version
2019-11-11 23:01:05 +01:00
std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "."
2020-02-01 23:06:01 +01:00
<< Tutorial_VERSION_MINOR << std::endl;
2019-11-11 23:01:05 +01:00
std::cout << "Usage: " << argv[0] << " number" << std::endl;
return 1;
}
2020-02-01 23:06:01 +01:00
// convert input to double
const double inputValue = std::stod(argv[1]);
2019-11-11 23:01:05 +01:00
2022-11-16 20:14:03 +01:00
// which square root function should we use?
#ifdef USE_MYMATH
const double outputValue = mysqrt(inputValue);
#else
const double outputValue = sqrt(inputValue);
#endif
2019-11-11 23:01:05 +01:00
std::cout << "The square root of " << inputValue << " is " << outputValue
<< std::endl;
return 0;
}