Find errors of argv[1] and argv[2]

Hi

So I'm doing some error checking of parameters that are entered. This is what I've done for argv[1].The parameters are entered in the format of 25 2, and if it were to be just 25 (without the 2 it is assumed to argv[2] = 1). This is my problem how would I do the checking for argv[2]? since if I have no argv[2] it will out put something else.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    int ilen = 0;

    while(argv[1][ilen] != '\0')
    {
        ilen++;
    }

    for(int i = 0; i<ilen; i++)
    {
        if(argv[1][i] == '0' || argv[1][i] == '1' || argv[1][i] == '2' || argv[1][i] == '3' || argv[1][i] == '4' || argv[1][i] == '5' || argv[1][i] == '6' || argv[1][i] == '7' || argv[1][i] == '8' || argv[1][i] == '9')
        continue;

            else
            {
                cout << "X" << endl;
                return 0;
            }

    }


        // Range check
        if(atoi(argv[1]) > 255 || atoi(argv[1]) < -255)
        {
            cout << "R1" << endl;
            return 0;
        }

        /*if(atoi(argv[2]) > 65536 || atoi(argv[2]) < 1)
        {
            cout << "R2" << endl;
            return 0;
        }*/


Thanks
Last edited on
Rather than doing the check for individual values in
 
if(argv[1][i] == '0' || argv[1][i] == '1' ...
you can use
 
isdigit(argv[1][i])

http://pubs.opengroup.org/onlinepubs/007908799/xsh/isdigit.html

Your validation seems a little odd if all you're doing is checking that your input numerator/denominator pair are valid.
Your validation seems a little odd if all you're doing is checking that your input numerator/denominator pair are valid.


This is only a segment of my program, I'm using a IO USB board.

But I might have poorly worded my problem. So here is another go;
I need to check values of argv[1] and argv[2], how would I do this when I don't input a value for argv[2] (argv[2] should equal 1 if this is the case)?

Last edited on
Use argc to check how many args you actually have. e.g.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main(int argc, char* argv[])
{
    int num = 0, den = 1;

    for (int i = 1; i < argc; ++i)
        switch (i)
        {
        case 1:
            num = atoi(argv[i]);
            break;
        case 2:
             if (atoi(argv[i])
                 den = atoi(argv[i]);
             break;
        }

    // ...
    return 0;
}
> I need to check values of argv[1] and argv[2]

Checking if a string contains the representation of an integer is not as trivial as you imagine it to be. Merely checking that every character is a digit is nowhere near enough.

Every one of these is valid: 8743 +8743 -8743
These are invalid: +8743+ +87-43 -+8743

The C++ input stream knows how to convert sequences of characters to integers; let it do the heavy lifting.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <sstream>

// if cstr contains the representation of an integer in [min_value,max_value]
// assign that integer to value and return true
// otherwise leave value unchanged and return false  
bool to_int( const char* cstr, int& value, int min_value, int max_value )
{
    std::istringstream stm(cstr) ;
    int temp ;

    if( stm >> temp && stm.eof() && temp >= min_value && temp <= max_value )
    {
        value = temp ;
        return true ;
    }

    else  return false ;
}

int main( int argc, char* argv[] )
{
    enum { ARG1_MIN = -255, ARG1_MAX = 255,
            ARG2_MIN = 1, ARG2_MAX = 65536, ARG2_DEFAULT = 1 } ;

    if( argc < 2 ) { /* error */ }

    else
    {
        int first ;
        if( !to_int( argv[1], first, ARG1_MIN, ARG1_MAX ) ) { /* error */ }

        int second = ARG2_DEFAULT ;
        if( argc == 3 )
           if( !to_int( argv[2], second, ARG2_MIN, ARG2_MAX ) ) { /* error */ }

        // use first, second
    }
}
Topic archived. No new replies allowed.