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");
}
C

Avatar of undefined
Last Comment
sunnycoder

8/22/2022 - Mon
sunnycoder

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
ozo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ozo

I think your parentheses are also unbalanced.
ASKER
pothios

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");
    }
 
Your help has saved me hundreds of hours of internet surfing.
fblack61
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.

The code here looks fine - including ()
ASKER
pothios

#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
ozo

#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);
}
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
sunnycoder

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
ASKER
pothios

>>   //       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.
sunnycoder

I passed argv[1] as 0 and it duly printed error .. ofcourse after making corrections for compilation ... so condition is correct
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER
pothios

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

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();
    }
ASKER
pothios

ok sry my bad it works.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
sunnycoder

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