argv and argc ?

Oct 12, 2009 at 7:03pm
I'm creating a program that reads from the command line two integer vectors, and transforms them into polinoms, allowing us to sum, multiply, and subtract them. The idea would be to call this in the console

&> ./polinom 2 1 1 3 1 1 1
(polinom is the name of the program.2 indicates the length of the first integer vector. 1 1 are the coefficients of the monoms of the first polinom. 3 indicates the length of the second vector, and 1 1 1 its coefficients)

However, i get a lot of errors with this implementation of main(void), specially on the declaration of a_length, and b_length. I don't know what I'm doing wrong, and would like some guidance in what i should do.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
   int main (int argc, char* argv[])
   {      
      unsigned a_length = unsigned(argv[1]), b_length = unsigned (argv[2+argv[1]]);
      int* ap, bp;
      ap = new int[a_length];
      bp = new int[b_length];
      cout << "longitudes : " << a_length << "  " << b_length << endl;
      for (unsigned i = 0; i< a_length ; i++){
         *(ap+i) = argv[i+2];
      }
      for (unsigned i = 0; i< b_length ; i++){
         *(bp+i) = argv[argv[1]+3+i];
      }
      Polinom a(ap,a_length), b(bp, b_length); 
      return 0;
   }


Thanks in advance
Last edited on Oct 12, 2009 at 7:06pm
Oct 12, 2009 at 7:10pm
argv stores the arguments as C strings -character arrays-, to convert them to numbers see http://www.cplusplus.com/articles/numb_to_text/#s2n ( the example uses std::string but it should work also with C strings )
Oct 12, 2009 at 7:39pm
yeah, i was smelling like i was trying to interpret strings as unsigned. But i thought g++ wouldn't emmit error.

I made the conversion, but im still getting error
In function `int main(int, char**)': invalid types `char**[char*]' for array subscript here, in the declaration of a_length and b_length:
 
      unsigned a_length = convierte(argv[1]), b_length = convierte(argv[2+argv[1]]);


this is the code of convierte(string)

1
2
3
4
5
6
7
8
   unsigned convierte (string a){
      unsigned result;
      istringstream convert(a); 
      if ( !(convert >> result) ){ 
         result = 0;
      }            
      return result;
   }
Last edited on Oct 12, 2009 at 7:39pm
Oct 12, 2009 at 7:45pm
You are still using argv[1] as number: b_length = convierte(argv[2+argv[1]]);
Oct 12, 2009 at 7:54pm
Fixed.
Why do i receive invalid conversion from `int*' to `int' here: ? (second line)

1
2
      unsigned a_length = convierte(argv[1]), b_length = convierte(argv[2+convierte(argv[1])]);
      int* ap = new int[a_length] , bp= new int[b_length];


EDIT: Self answer: It was missing a * . I should've written
int* ap = new int[a_length] , *bp= new int[b_length];

Thanks for the help, Bazzy. Im marking this as solved
Last edited on Oct 12, 2009 at 8:03pm
Oct 12, 2009 at 8:03pm
* works for just one variable on declaration, add another star before bp:
int *ap = new int[a_length] , *bp= new int[b_length];
( I like more the int *p; notation than the int* p; one for this reason. )
Oct 12, 2009 at 8:07pm
yeah, as you said, i thought of int* as a type. That would imply repetition of * is unnecessary. However that's wrong.

Once again, Thanks a lot!.
Topic archived. No new replies allowed.