Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Updated 23-Feb-11 2:41am
v4
Comments
CPallini 23-Feb-11 8:40am    
And you, please, don't bother posting such pointless comments.
[no name] 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 23-Feb-11 8:42am    
Small sidenote: requirements are for two integers, you should check that too.

1 solution

OK: Some slight problems here:
argc contains the number of arguments, not argv. Change to:
C#
for(int i=1;i<argc;++i)

and argv contains the arguments themselves, not argc. Change to:
C#
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. :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!
 
Share this answer
 
v3
Comments
[no name] 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 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 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 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 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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900