Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Linux
I tried this code but it is throwing the below error:
int main(int argc, char **argv)
     {
       int total=0;
        for(int i=0;i<argv;++i)>
         {
             total=total+atoi(argc[i]);
         }
         cout<<total<<endl;
       return 0;
     }
Error: Pointer type needed instead of int.
Posted 22-Feb-11 23:47pm
Edited 23-Feb-11 2:41am
CPallini313.7K
v4
Comments
CPallini at 23-Feb-11 8:40am
   
And you, please, don't bother posting such pointless comments.
CDP1802 at 23-Feb-11 6:23am
   
On the contrary. Most people don't mind helping a beginner. He did not ask us to do his homework and obviously simply got confused by the types of argc and argv. char** is something I also would not want to encounter right at the beginning
CPallini at 23-Feb-11 8:42am
   
Small sidenote: requirements are for two integers, you should check that too.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

OK: Some slight problems here:
argc contains the number of arguments, not argv. Change to:
for(int i=1;i<argc;++i)
and argv contains the arguments themselves, not argc. Change to:
total=total+atoi(argv[i]);
And all should be well.
 
[edit]And take out the > symbol on the end of your for loop! (If it is really there...) - OriginalGriff[/edit]
 
Nuri Ismail correctly suggested: the loop must start from 1 (int i = 1) and 'argc' must be at least 3, because 'argv[0]' is reserved for the process name. Example cmd line: 'my_prog.exe 5 6' -> argv[0] = my_prog.exe, argv[1] = 5 and argv[2] = 6. Thumbs Up | :thumbsup:
 
I have updated my response to illustrate that.
 
It was also (very correctly) pointed out that you should check your inputs for:
Number of arguments - argc should be equal to three or you have a problem!
Valid digts - is the argument actually a number?
 
If you need help with that, please ask again in another question!
  Permalink  
v3
Comments
CDP1802 at 23-Feb-11 6:14am
   
And perhaps he should check that argc really equals two, no more, no less. As it is now, you could also pass 200 arguments. Also, it might be worth a thought what happens when I pass %64§ and +xdfg# as arguments.
OriginalGriff at 23-Feb-11 6:15am
   
Or at least a minimum of two.
I did consider that, but as he is a complete beginner I thought "just fix the problem he has now, don't confuse him." :laugh:
Nuri Ismail at 23-Feb-11 6:40am
   
Griff, the loop must start from 1 (int i = 1) and 'argc' must be at least 3, because 'argv[0]' is reserved for the process name. Example cmd line: 'my_prog.exe 5 6' -> argv[0] = my_prog.exe, argv[1] = 5 and argv[2] = 6.
OriginalGriff at 23-Feb-11 6:43am
   
Good point :blush: My bad - it's been a while since I played with C command line arguments...
I'd 5 that if I could...
Nuri Ismail at 23-Feb-11 6:54am
   
No problem, normally I wouldn't notice such a detail, but nearly a month ago I wrote a small cmd line app which works with arguments.
 
BTW, all of the remarks from your answer are good and valid. Maybe you should update the answer in order to reflect this little detail about the argv[0], because I'm not sure if the OP will take care to read the comments. :)
 
Best Regards,
Nuri
OriginalGriff at 23-Feb-11 7:28am
   
Good idea - done.
Nuri Ismail at 23-Feb-11 8:09am
   
Thank you, Griff! 5+
SAKryukov at 23-Feb-11 22:31pm
   
Anyway, 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 518
1 OriginalGriff 459
2 ChintanShukla 305
3 Richard Deeming 250
4 RyanDev 210
0 Sergey Alexandrovich Kryukov 8,901
1 OriginalGriff 7,571
2 CPallini 2,603
3 Richard MacCutchan 2,095
4 Abhinav S 1,893


Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 23 Feb 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100