-3

i am trying to use atoi to convert a string into an int, and then check if the string was indeed all integers.

Here is my code:

printf("Please input the port: ");
scanf("%s",port_number);
int i = atoi(port_number);
if(i != port_number)
{
    errno = ERR_INVALID_PARAM;                  
    printf("Error invalid port number\n");
}

However, even when my string "port_number" is an integer, it still goes in the if statement? Any help?

anderish
  • 1,709
  • 6
  • 25
  • 58
  • 1
    You are trying to compare an integer and a string... – Oliver Charlesworth Feb 04 '14 at 19:16
  • 1
    You cannot compare an integer and a string in this way. Look here http://stackoverflow.com/questions/3850558/how-to-check-to-ensure-you-have-an-integer-before-calling-atoi – fede1024 Feb 04 '14 at 19:16
  • Your example is not complete. We are at least lacking the definition of `port_number`. – glglgl Feb 04 '14 at 19:18
  • Are you writing in C? Or C++? –  Feb 04 '14 at 19:23
  • How is `port_number` declared? If it's an `int`, then your call `scanf("%s",port_number)` has undefined behavior; the second argument needs to be a `char*` pointing to a buffer big enough to hold the result (which, for a bare `"%s"`, can be arbitrarily long). If it's a `char*`, then the comparison `i != port_number` shouldn't even compile. (Actually gcc merely issues a warning, but you need to pay attention to warnings.) Please show us a *complete* program. Since you don't know what the problem is, you don't know what parts of your code are irrelevant. http://sscce.org/ – Keith Thompson Feb 04 '14 at 19:31

4 Answers4

2

You need to do some error-checking on the string coming in before you convert it to an int. You may want to try the strtol() function. It checks whether a string can be converted to a long int. It returns either the converted value, or zero if the string cannot be converted.

the second arg to strtol() is a char**. After the function finishes, it points to the first character in the string that is NOT a legal number character. So if it points anywhere before the end of the string, then the conversion failed.

http://www.tutorialspoint.com/c_standard_library/c_function_strtol.htm

Kevin
  • 2,112
  • 14
  • 15
1

I'm assuming that port_number is of type char *.

The if statement you have there is comparing the address of the port_number C string with value of i, and I don't think that's what you want to do.

If you want to use ctype.h, then you can use the function "isdigit()" to check each element of port_number. Otherwise, the next best thing is to cycle through port_number, and figure out if each element is between ascii char '0' and '9', to make sure that the port number is entered correctly.

Neil
  • 44
  • 2
  • If `port_number` is of type `char*`, then the comparison `i != port_number` shouldn't even compile. At the very least, there should be a warning. (gcc merely warns about this by default.) – Keith Thompson Feb 04 '14 at 19:34
0

Think about what you are comparing there:

if(i != port_number)
{
    //code
}

i is of type int and port_number is a char*.

gmorrow
  • 101
  • 4
0
char *check = strdup(port_number);
sprintf(check, "%i", i);
if(strcmp(check, port_number))
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70