views:

457

answers:

4

In college I was asked if our program detects if the string enter from command line arguments is a integer which it did not(./Program 3.7). Now I am wondering how I can detect this. So input as for example a is invalid which atoi detects, but input like for example 3.6 should be invalid but atoi will convert this to an integer.

#include <stdio.h>

int main(int argc, char *argv[]) {
    if (argc > 1) {
        int number = atoi(argv[1]);
        printf("okay\n");
    }
}

But offcourse okay should only be printed if argv[1] is really an integer. Hope my question is clear. Many thanks.

+9  A: 

Have a look at strtol.

If endptr is not NULL, strtol() stores the address of the first invalid character in *endptr. If there were no digits at all, however, strtol() stores the original value of str in *endptr. (Thus, if *str is not \0' but **endptr is\0' on return, the entire string was valid.)

#include <stdio.h>

int main(int argc, char *argv[]) {
  if (argc > 1) {
    char* end;
    long number = strtol(argv[1], &end, 0);
    if (*end == '\0')
      printf("okay\n");
  }
}
Gregory Pakosz
I have looked at strtol but i could not implement it to detect 3.6 as invalid input ..
Alfred
What problems specifically did you have with using `strtol`? Did you check the value to which the pointer points to on return?
Pavel Minaev
@gregory Thanks gregory. This was exactly what I was looking for and I think the code is pretty dense.@Pavel I think I did not do enough error checking I just did strtol(argv[1],
Alfred
+2  A: 

Assuming you want to know how it could be done in code (possible if it is indeed homework), one way is to think about what constitutes an integer in terms of the string. It would most likely be:

  • an optional sign, +/-.
  • a required digit.
  • any number of optional digits (but watch out for overflow).
  • the end of string.

From that specification, you can write a function that will do the work for you.

Something like this pseudo-code would be a good start:

set sign to +1.
set gotdigit to false.
set accumulator to 0.
set index to 0.
if char[index] is '+':
    set index to index + 1.
else:
    if char[index] is '-':
        set sign to -1.
        set index to index + 1.
while char[index] not end-of-string:
    if char[index] not numeric:
        return error.
    set accumulator to accumulator * 10 + numeric value of char[index].
    catch overflow here and return error.
    set index to index + 1.
    set gotdigit to true.
if not gotdigit:
    return error.
return sign * accumulator.
paxdiablo
+1  A: 
int okay = argc>1 && *argv[1];
char* p = argv[1];
int sign = 1;
int value = 0;
if( *p=='-' ) p++, sign=-1;
else if( *p=='+' ) p++;
for( ; *p; p++ ) {
    if( *p>='0' && *p<='9' ) {
        value = 10*value + *p-'0';
    } else {
        okay = 0;
        break;
    }
}
if( okay ) {
    value *= sign;
    printf( "okay, value=%d\n", value );
}

EDIT: allow - and + characters

You may even compress this into a dense one- or two-liner. Or you may find a library function with the same functionality ;)

EDIT2: just for fun - it should now parse the number

frunsi
This will accept "123-456".
Carl Smotricz
see my answer for an `strtol` based implementation, there is no reason to reinvent the (square) wheel :)
Gregory Pakosz
Hehe I tested this code but it does not work. First off all because false is not really part of c. But I think you are in the right direction ;). It will also throw segmentation fault when no arguments are supplied.
Alfred
@Carl: d'oh, will change it..., @Alfred: Again, d'oh
frunsi
@Frunsi I guess that would be a smart thing to do because right now this code will blow up in your face (so please update code to be correct for other stackoverflow members).
Alfred
frunsi
Okay because when you compile this code and do not provide arguments this program will give a segmentation fault.
Alfred
frunsi
+1  A: 

So your best choice is strtof() http://publib.boulder.ibm.com/infocenter/zos/v1r10/topic/com.ibm.zos.r10.bpxbd00/strtof.htm

alemjerus
No, it's not. And baby makes 3.
Bklyn