-1

i need to check whether user has entered an integer like 1,15,-1 or a string like 2bda or 5afnd etc. It can easily be done by using a string of characters.

can it be done using an int variable in c or c++?

Brij Vaid
  • 53
  • 1
  • 2

5 Answers5

2

No, int cannot store an arbitrary string of characters.

int is a signed integral type that holds values between some min and max value, depending on the implementation-defined size. There is no error state or error value built into the integer (unlike floating point types which have a Not a Number value, NaN). However, you can choose values that are invalid, commonly the min or max value, or a negative value if only positive values are expected. Then you can check for that value before you process. For example:

#include <climits> // for INT_MIN

bool is_valid( int value )
{
    return value != INT_MIN;
}

int get_value( const std::string& str_value )
{
    std::istringstream ss( str_value );
    int value;
    if( ss >> value )
        return value;
    else
        return INT_MIN;
}

void print_if_valid( int value )
{
    if( is_valid( value ) ) 
        std::cout << value << std::endl;
    else
        std::cout << "invalid" << std::endl;
}

int main()
{
    int num1 = get_value( "2" );
    int num2 = get_value( "a" );

    print_if_valid( num1 );
    print_if_valid( num2 );

    return 0;
}

Note that with this solution, if the value is actually INT_MIN (if int is 32 bit than this will be -2147483648), it will still be treated as invalid even though it was a valid int.

clcto
  • 9,530
  • 20
  • 42
1

Read input as a string. Attempt various parsings.

char buffer[100];
fgets(buffer, sizeof buffer, stdin);
errno = 0;
char *endptr;
long i = strtol(buffer, &endptr, 10);
if (errno == 0 && *endptr == '\n') {
  Handle_Integer(i);
}
else {
  HandleInputAsString(buffer);
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

You can use atoi or stoi function: Convert string to int C++

If the function stoi cannot convert the string to a int, then it throws an exception, in that case you can assume that it is a string. http://en.cppreference.com/w/cpp/string/basic_string/stol

Community
  • 1
  • 1
carlosvin
  • 979
  • 9
  • 22
0

In my humble opinion you could use boost::lexical_cast to efficiently turn user input to numerical input:

#include <boost/lexical_cast.hpp>
#include <iostream>

int main() {
  std::string input;
  std::getline(std::cin, input);
  int i(0);
  try {
    i = boost::lexical_cast<int>(input);
  } catch(const boost::bad_lexical_cast &) {
    std::cout << "Wrong Input!" << std::endl;
  }
  std::cout << i << std::endl;
  return 0;
}
101010
  • 41,839
  • 11
  • 94
  • 168
0

No. it is not possible to check the entered input is integer or string using int data type. Because when you use int variable to scan data 2bda or 5try like this, the first integer only will get stored in that variable. Remaining input are omitted, Because integer can't store string's.

Sample program-

#include<stdio.h>
int main()
{
        int num1,num2,ret;
        printf("Enter the integer\n");
        scanf("%d",&num1);

        printf("%d\n",num1);
        return 0;
}

Test 1:

root@ubuntu:~/c/array/string# ./a.out 
Enter the integer
123
123

When you give integer as input you will get proper output.

Test 2:

root@ubuntu:~/c/array/string# ./a.out 
Enter the integer
2bda
2

It takes only first integer, Remaining are omitted. Results undefined.

Test 3:

root@ubuntu:~/c/array/string# ./a.out 
Enter the integer
bdas2
-1217114112
root@ubuntu:~/c/array/string# 

If your input is pure string, its totally undefined behavior! so you can't do that.

Sathish
  • 3,740
  • 1
  • 17
  • 28