I have a simple program thats input regularly is numbers, however I need to add an option where if the user types "help" or "/h" or "?" the program will display a help menu...
So essentially I need to be able to differentiate if the input is a string or a number and if its a number, convert it to an int...
What's the best way to get this done? I know I can use atoi to convert a string to an int, but how do I check if the value is numeric?
#include <iostream>
#include <stringstream>
int main()
{
std::string strIn;
std::stringstream strConvert;
std::cout << "Enter a number: (? -- for help)" << std::endl;
std::getline(cin, strIn);
double nInput;
strConvert << strIn;
strConvert >> nInput;
if(!strConvert.good())
{
if(strIn == "?")
{
std::cout << "A number can be any Real number." << endl;
}
else
{
std::cout << "I didn't understand that input please refer to ?-help for more information." << std::endl;
}
}
else
{
std::cout << "The number you entered: " << nInput << std::endl;
}
return 0;
} // end of main.