Possible Duplicate:
How to parse a string to an int in C++?
I have a simple program thats input regularly is numbers (guessing game pretty much), 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?
thanks!
EDIT: According to what I read around from your answers most of you say stringstream is the best way to go... I made a small piece of code to try to understand how stringstream works but I am getting a few errors... any idea why?
#include <iostream>
#include <string>
using namespace std;
int str2int (const string &str) {
  std::stringstream ss(str);
  int num;
  if((ss >> num).fail())
  { 
      num = 0;
      return num;
  }
  return num;
}
int main(){
    int test;
    int t = 0;
    std::string input;
    while (t !=1){
        std::cout << "input: ";
        std::cin >> input;
        test = str2int(input);
        if(test == 0){
            std::cout << "Not a number...";
        }else
            std::cout << test << "\n";
        std::cin >> t;
    }
    return 0;
}
Errors:
Error C2079:'ss' uses undefined class std::basic_stringstream<_elem,_traits,_alloc>'
Error C2228: left of '.fail' must have class/struct/union
Error C2440: 'initializing': cannot convert 'const std::string' into 'int'
 
     
     
     
    