problems with argc, *argv[]

Here's my code:

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const unsigned short NB_TOTAL_ELEMENTS = 81;

int main(int argc, char *argv[])
{
    unsigned short tableau[NB_TOTAL_ELEMENTS];
    
    for(unsigned short i = 1; i < argc; i++)
    {
        if(argv[i] == "0")
        {
            tableau[i-1] = 0;
        }
        else if(argv[i] == "1")
        {
            tableau[i-1] = 1;
        }
        else if(argv[i] == "2")
        {
            tableau[i-1] = 2;
        }
        else if(argv[i] == "3")
        {
            tableau[i-1] = 3;
        }
        else if(argv[i] == "4")
        {
            tableau[i-1] = 4;
        }
        else if(argv[i] == "5")
        {
            tableau[i-1] = 5;
        }
        else if(argv[i] == "6")
        {
            tableau[i-1] = 6;
        }
        else if(argv[i] == "7")
        {
            tableau[i-1] = 7;
        }
        else if(argv[i] == "8")
        {
            tableau[i-1] = 8;
        }
        else
        {
            tableau[i-1] = 9;
        }
    }
    
    cout << argc << endl << endl;
    
    for(int i = 0; i < NB_TOTAL_ELEMENTS; i++)
    {
        cout << tableau[i] << endl;
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


my problem is that for some reason my array (tableau[]) gets filled with 9 (in other words, every argument fall into the "else" section of my if..else if structure), instead of the following arguments (81 in total)

5 0 6 4 0 0 0 2 0 2 0 0 0 3 1 7 0 4 0 7 0 2 0 0 0 0 0 8 0 5 7 0 0 2 0 9 0 4 0 0 8 0 0 3 0 9 0 3 0 0 6 1 0 8 0 0 0 0 0 9 0 6 0 6 0 7 8 2 0 0 0 3 0 1 0 0 0 5 9 0 7

I can't find the mistake can you help me out? thanks a lot!
Last edited on
ur subtracting ints from chars
I'm not sure that this is the problem, because the purpose of having an if...else if nest is to not do that... you see what I mean? I say if argument == (string) "8" (or "6" or whatever), assign a certain value to an array... but that doesn't seem to work, that's what I don't understand... I see what you mean but I thought I corrected this mistake, when I figured out I was passing chars to int, by nesting the thing with if..else if...
what?? that is the problem. "9" is a char so "9" - 1 is the same as "a" - 1
well I'm not doing that, I say if argv[i] == "4" (a string), assign an int to an array... I don't see what's wrong with that.... the problem is that my array gets filled with 9s instead of the parameters I give... but let's say you're right How should I do it then?
Last edited on
You can't compare C-strings using ==. Use strcmp.
it's working thanks a lot!
Topic archived. No new replies allowed.