Quick question about atoi

acidic

Gawd
Joined
Jun 25, 2005
Messages
952
Ok, so I have this array of strings that I want to compare to make sure its a number, so I been thinking atoi, but I cannot get it to compile, how would I compare the stuff inside an array of strings to an interger? The bolded and underlined part is my problem

code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>


int main(int argc, char *x[]){
if(argc !=3){
printf("Must have two arguments.\n");
exit(0);
}
int d,z,j,l,i,t;
int buffer;

for(i=1;i<argc;i++){
for(j=0;j<4;j++){

                if(x[i][j]=='\0'){
                break;
                }

                                buffer=0;

                        for(t=0;t<10;t++){
[U]                 [B]    if(atoi(x[i][j])==0){[/B][/U]
                                                buffer++;
                                                printf("%d",buffer);
                                                printf("\n%d,%d\n",i,j);}
                                        }

                        if(buffer==0){
                                printf("Arguments must be a number.\n");
                                        exit(0);}


}}
z= atoi(x[1]);
d= atoi(x[2]);

l= z * d;

printf("%d\n",l);


return 0;}
 
Just a quick glance, but...

char *argv[] (or x in your code) is not a matrix, it's an array of pointers to char strings).

Also, atoi's return value is whatever the number is contained in the string (if it is indeed a number). It returns 0 only if an error occurs.
 
yea, but I need to compare the stuff in x[row][j] to verify its an interger, using x gets the array, I need the letters that the strings is pointed by the array. That is what I don't know to do.

edit:
what would be a way to check to see if x[j] is an interger?
 
You are misunderstanding both atoi and character strings.

Code:
for(j=0;j<4;j++){ ... }

This will limit the size of the numbers to 4 digits, which you probably don't want.

Code:
for(t=0;t<10;t++){
  if(atoi(x[i][j])==0){
    buffer++;
    printf("%d",buffer);
    printf("\n%d,%d\n",i,j);
  }
}

First, this code probably doesn't need the loop. If buffer must be increased 10 times for each "0", then just use buffer += 10

Second, you are calling atoi on a character. It will not convert, for example, the character '3' into the number 3. It will, however, convert the character string "3" into 3, the string "12345" into the integer 12345, etc.
 
acidic said:
what would be a way to check to see if x[j] is an interger?


Check if its value is between 48 (0) and 57 (9). Eg, if (x[j] >= 48 && x[j] <= 57){ ... }
 
edit: beaten to it since I was busy coding up something.

atoi expects a char* argument so you have to pass it strings, not just chars.

However, using atoi might not be optimal for your program because it can return 0 if an error occurs, and this can conflict with the input number "0", so "a" and "blah" will produce the same return value as 0.

Code:
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
    while(argc-- && argc)
    {
        if(atoi(argv[argc]) >= 0)
        {
            printf("%s\n", argv[argc]);
        }
    }

    return 0;
}

Just for reference - I'm not recommending the use of this code.
 
char * x[] is an array of string arguements.

IE. If you ran your programs with the argument foo bar:
Code:
c:\> YOUR-PROGRAM foo bar

then
Code:
x[0] == "YOUR-PROGRAM"
x[1] == "foo"
x[2] == "bar"
 
Here's something that'll work with accessing each element of the argv strings. Incredibly hacky, will need to be modified to fit with your code.

Code:
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
    int i;
    char c;

    while(--argc && argc)
    {
        for(i = 0; argv[argc][i] != '\0'; ++i)
        {
            c = argv[argc][i];
            if(c < 48 || c > 57)
            {
                printf("%s is not a number!\n", argv[argc]);
                break;
            }
        }
    }

    return 0;
}

And note that this does not work for negative numbers, or numbers prefixed with the positive sign either, or hex numbers, octal...etc.
 
Back
Top