Your error is because of this line:
dNumber = stod(Equation);
Its exception documentation says: If no conversion could be performed, an std::invalid_argument
exception is thrown.
When you entered a letter, no conversion could be performed and you got the exception.
Secondly, you're trying to convert the entire equation to a double.. If that is the case, what was the point of checking if each index in the string is a digit?
I think you meant to put: stod(to_string(Equation[I]))
instead..
You can also create your own conversion functions using the ones already provided for you in C.
#include <iostream>
#include <string>
using namespace std;
inline double strtodouble(char c) noexcept
{
return strtod(&c, NULL);
}
inline double strtodouble(const char* str) noexcept
{
return strtod(str, NULL);
}
inline double strtodouble(const std::string str) noexcept
{
return strtod(str.c_str(), NULL);
}
int main()
{
string Equation;
double dNumber;
cout << ": ";
getline(cin, Equation);
for (int i = 0; i < Equation.size(); i++)
{
if (isdigit(Equation[i]))
{
cout << "Number: ";
dNumber = strtodouble(Equation[i]);
cout << dNumber << endl;
}
else
{
if (Equation[i] != ' ')
{
cout << "Character" << endl;
}
}
}
cout << endl;
system("pause");
}