-1

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'
Community
  • 1
  • 1
Gal Appelbaum
  • 1,905
  • 6
  • 21
  • 33

3 Answers3

0

Use strtol (http://www.cplusplus.com/reference/clibrary/cstdlib/strtol/) instead of atoi.

long int strtol ( const char * str, char ** endptr, int base );

If successful you get a non-zero value AND the endptr has moved. If the endptr didn't move you know it is a failure. If a failure and the end pointer moved, you get a 0 and the memory address right before the end ptr is whitespace.

David D
  • 1,571
  • 11
  • 12
-1

Just check for your three cases, otherwise if any of the characters isn't a digit, handle, otherwise convert and proceed.

string input;
cin >> input;
if (input == "help" || input == "/h" or input == "?")
    help();
else {
    bool convertible = true;
    for(string::size_type i = 0; i < input.size(); ++i) {
        if (!isdigit((int)input[0])) {
            convertible = false;
            break;
        }
    }
    if (convertible) {
        int digit = atoi(input.c_str());
        // do guessing game stuff
    }
    else {
        // handle
    }
}
Walker
  • 1,215
  • 2
  • 13
  • 26
-1

if no valid conversion can be perform, atoi returns 0. You can handle it then.

Anthony Choi
  • 106
  • 4
  • what if the actual input is 0? – Gal Appelbaum Mar 22 '12 at 05:04
  • What if the number passed is 0? That's a good reason why a stringstream is a better option for string->int conversions. As seen in the duplicate link, it may not be the best, but it sure works. – chris Mar 22 '12 at 05:04
  • And this is why you don't use `atoi`. At least use `strtol`, but this is C++, so use a `stringstream`. – Ed S. Mar 22 '12 at 05:18