I'm in a project implementing a C ++ calculator that uses variables. So, if I define a variable x = 5, and I type x ^ 2 then it returns the value of 25. For this, I have to store the variable name and the corresponding value. The user would type a name or a double and depending on the entry, store the value or the name. There is my difficulty.
I am trying to input data in C ++ with the use of class and operator overload. However, I can not do that. I have a class called "variable":
class variavel
{
public:
std::string nome;
double valor;
friend std::istream& operator>> (std::istream& os, variavel &v);
};
What I want to do is something like:
std::istream& operator>> (std::istream& os, variavel &v)
{
os >> v.nome;
try
{
v.valor = ConverteParaDouble(v.nome); /* lança uma exceção se não for um numero */
v.nome = ""; /* nao executa se lançar a exceção */
}
catch(excecao &e) /* captura a exceção */
{
/* Outros comandos */
}
}
However, I do not know how to do this, be it convert (tried atoi
, stod
) or throw the exception.
For the stod documentation ( click here ), it would be necessary, but always when I put the code:
std::istream& operator>> (std::istream& os, variavel &v)
{
os >> v.nome;
try
{
v.valor = std::stod(v.nome);
v.nome = "";
}
catch(const std::invalid_argument& ia)
{
v.nome = "deu ruim";
}
}
But whenever I try to compile, it says stod
is not in std
. How do you arrange this?