Link to home
Start Free TrialLog in
Avatar of pothios
pothios

asked on

comparing interger using atoi

hello. im trying to check a string from argv if its within 10 to 30. i tried doing it the way below and it seems dat the comparison doesnt work.

if(atoi(argv[1]) < 10) && atoi(argv[1]) > 30) {
      printf("not in range");
}
Avatar of sunnycoder
sunnycoder
Flag of India image

Hello pothios,

if(atoi(argv[1]) < 10) && atoi(argv[1]) > 30) {

if(atoi(argv[1]) < 10) || atoi(argv[1]) > 30) {

Use OR in place of AND

Regards,

sunnycoder
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think your parentheses are also unbalanced.
Avatar of pothios
pothios

ASKER

hello, i cant get it right :(

./program 24324324324324      will still not print the error.

    /* must be within 10 to 30*/
    if((atoi(argv[1]) < 10) || (atoi(argv[1]) > 30)) {
            printf("error");
    }
 
Pls post the "exact" program and output ... Note that the number you are passing is huge and might cause overflow ... From man page
If the value cannot be represented, the behavior is undefined.

The code here looks fine - including ()
Avatar of pothios

ASKER

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <ctype.h>

void error() {
    fprintf(stderr, "error\n");
    exit(2);
}

/* function to check argument type */
void validate(int argc, char **argv) {
    int i, a;

    if(argc != 4) {
        usage_error();
    }

    for(i = 1; i < argc; i++) {
        for(a = 0; argv[i][a] ; a++) {
            if(!isdigit(argv[i][a])) {
               error();
            }
        }
    }
    /* args must be within 1 to 10000 */
    if((atoi(argv[1]) < 1) || (atoi(argv[1]) > 10000)) {
            error();
    }

int main(int argc, char *argv[]) {
  validate();
}
                                                                                                                                           6,0-1         Top
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#include <malloc.h>
#include <ctype.h>

void error() {
    fprintf(stderr, "error\n");
    exit(2);
}

/* function to check argument type */
void validate(int argc, char **argv) {
    int i, a;

    if(argc != 4) {
      //       usage_error();
    }

    for(i = 1; i < argc; i++) {
        for(a = 0; argv[i][a] ; a++) {
            if(!isdigit(argv[i][a])) {
               error();
            }
        }
    }
    /* args must be within 1 to 10000 */
    if((atoi(argv[1]) < 1) || (atoi(argv[1]) > 10000)) {
            error();
    }
}
int main(int argc, char *argv[]) {
  validate(argc, argv);
}
1. you are comparing only argv[1] and not all of the arguments - is that what you expect

2. the code would not compile - closing braces of validate() ... and args to call to validate
Avatar of pothios

ASKER

>>   //       usage_error();
yea i forgot to put dat method in but then again, it doesnt affect the result. Maybe the error is caused by wat sunnycoder mentioned. i'll try setting a smaller int and see wat happe\ns

>>sunnycoder:
Pls post the "exact" program and output ... Note that the number you are passing is huge and might cause overflow ... From man page
If the value cannot be represented, the behavior is undefined.
I passed argv[1] as 0 and it duly printed error .. ofcourse after making corrections for compilation ... so condition is correct
Avatar of pothios

ASKER

>>  validate(argc, argv);
yep i did put dat in. i typed it wrongly here cause i cant paste from my vm to here :/
Avatar of pothios

ASKER

hmmm weird. i tried changing to  1 and tried ./program 0 and error is printed out. but not when i put the value to 1000.

    if((atoi(argv[1]) < 1000) || (atoi(argv[1]) > 10000)) {
            error();
    }
Avatar of pothios

ASKER

ok sry my bad it works.
atoi(argv[1]) < 1000
False ... it is == 1000 but not <1000

atoi(argv[1]) > 10000
False

False || false is false
hence error does not get printed