this line of code is wrong and won’t compile but i am wondering if it can be fixed.
#include <iostream>
template <typename T>
auto getValue()
{
std::cout << "Enter an integral value: ";
T value{};
std::cin >> value;
// I would do input handing later
return value;
}
int main()
{
auto value1{getValue()};
std::cout << value1 << '\n';
}
please point out if there is a way to make it possible or if it is not possible then why?
thank you.
I want to receive an integral value from the user and create a variable depending on the value that they entered.
for example
if they entered
56
then an integer variable will be created.
and if they entered
56.78
then a double variable will be created since it is a lateral.
>Solution :
Types are fixed at compile time so your code can’t work. If you really need to do this then there are a few approaches.
- The javascript approach of just using
doubleeverywhere, a double can hold 53 bit integers without losing precision. - Return a
std::variant
Option 2 could look something like this:
#include <variant>
#include <iostream>
#include <string>
#include <optional>
std::optional<int> parseInt(const std::string& str)
{
try
{
size_t pos;
int value = std::stoi(str, &pos);
if (pos == str.size())
{
return value;
}
}
catch (std::exception&)
{
}
return {};
}
std::optional<double> parseDouble(const std::string& str)
{
try
{
size_t pos;
double value = std::stod(str, &pos);
if (pos == str.size())
{
return value;
}
}
catch (std::exception&)
{
}
return {};
}
std::variant<int, double> getValue()
{
while (true)
{
std::cout << "Enter a numerical value: ";
std::string str;
std::getline(std::cin, str);
auto i = parseInt(str);
if (i)
{
return *i;
}
auto d = parseDouble(str);
if (d)
{
return *d;
}
}
}