1

I have a requirment that accepts values from user input. It can be integers or strings. User input is accepted as below.

string strArg;
cout << "Enter price value "  << endl;
std::getline(std::cin, strArg);
int input;
std::stringstream(strArg) >> input;

Question is how do i check user has entered integer, rather than string, i.e., check for right input. How can we do this in portable way in C++? As in my project Boost is not allowed as my manager is not happy in using it:)

venkysmarty
  • 11,099
  • 25
  • 101
  • 184

5 Answers5

1

Something like this ought to do:

#include <iostream>
#include <sstream>
#include <string>

int main(void)
{
  std::cout << "input:" << std::endl; 
  std::string input;
  std::getline(std::cin, input);

  std::istringstream str(input);
  int iv = 0;
  // this checks that we've read in an int and we've consumed the entire input, else
  // treat as string.
  if (str >> iv && str.tellg() == input.length())
  {
    std::cout << "int: " << iv << std::endl;
  }
  else
  {
    std::cout << "string: " << input << std::endl;
  }     

  return 0;
}
Nim
  • 33,299
  • 2
  • 62
  • 101
  • can u pls eloborate, is that mean if we consume entire input is it int, if not is it string. for string don't we consume entire input. – venkysmarty Feb 15 '11 at 09:59
  • eh? from the given `input` string, the above checks to see if the read in int (`iv`) has consumed the whole string, if it has, then it is considered an `int`, else it's a `string`. If `int`, `iv` contains the integer value, else `input` contains the string value. The check handles cases like: `123foo`, is this a string or int? if you simply did `str >> iv`, you'll get `iv=123`, is that what you want? Anyway - this does not seem like a work related problem, be honest - is this homework? – Nim Feb 15 '11 at 10:04
  • thanks for the help. my another question is does above logic handles input fooabc for int values. No this is not homework, this is part of req at work. And another point is i am getting compilation error for if check that tellg can not check for length – venkysmarty Feb 15 '11 at 10:07
1

Use std::stringstream and operator>> and check if the conversion was successful.


Something like this:

#include <iostream>
#include <sstream>


int main()
{
    std::stringstream ss;

    ss << "1234";

    int a = 0;
    ss >> a;
    if ( ss.fail() )
    {
        std::cout << "it is not a number" << std::endl;
    }
    else
    {
        std::cout << a << std::endl;
    }
}
BЈовић
  • 62,405
  • 41
  • 173
  • 273
1

There is no real reason to go through stringstream except you do not need to clear it if the user enters something invalid, plus you can echo it back to them..

int arg;
while(! (cin >> arg) )
{
   cin.clear();
   cin.ignore(numeric_limits<streamsize>::max(), '\n');

   // tell the user their value was invalid
}

// now we have a valid number

If you allow the user to enter integers or strings but just behave differently when it is a string you can of course use istringstream.

Note that you may wish to check your stream is empty after reading the number in case the user entered 123XYZ which if you just streamed in would be considered "valid" as it starts with an integer.

CashCow
  • 30,981
  • 5
  • 61
  • 92
0

boost::lexical_cast is apprximately the same as doing:

std::stringstream i; i << strArg;
int k; i >> k;
Benoit
  • 76,634
  • 23
  • 210
  • 236
0

You may use regular expressions to check it. As far as I know, new C++ standart has support of regular expressions

maks
  • 5,911
  • 17
  • 79
  • 123