hi i have to use strtol to error check that my second parameter is between 0 and 1023. if not i must print out "R".
i have this from a lecturer of which he recomended i look at, but i don't quite understand what is going on. could someone please explain it to, so i can attempt to do this range check
1 2 3 4 5 6 7
char *perror_chars ;
char **pperror_chars = &perror_chars ;
longint the_number = strtol(argv[1], pperror_chars, 0);
if ( strlen( perror_chars) != 0)
{ printf(" strtol found parameter 1 not a valid long integer: %s.\n", argv[1]) ;
printf(" invalid integer characters start at %s\n", perror_chars) ;
}
string min=0; //I think you shouldn't use strings here.
string max=1023;
char *end;
errno = 0;
strtol(argv[2], &end, 10); //you are not catching the returned value (the converted number)
if (errno != 0 && *end == '\0')
{
cout<<"R"<<endl;
}
if( between(min, number, max) ) (you need to code between)
int min=0; // define numerics as 'int' instead of 'string'
int max=1023;
char *end = NULL;
errno = 0;
long int num = strtol(argv[2], &end, 10); // returned value saved in 'num' variable
if ( strlen(end) != 0) { // 'errno' was zero even with invalid digits so don't use 'errno'
cout<<"R"<<endl;
} else if ( !((num >= min) && (num <= max)) )
cout<<"R"<<endl;
}