Arrays and Line Command Help!

How do I make this add up all the grades?
For example, if you line command 2, you enter 2 grades, so I input 7 and 3, but I want to add them up to become 10, How should I do this?

#include<iostream>
2 #include<stdlib.h>
3
4 using namespace std;
5
6 int main (int argc, char *argv[]){
7 int *grades, sum;
8
9 if (argc > 1){
10
11 grades = new int[atoi(argv[1])];
12
13 for(int i=0; i < atoi(argv[1]); i++){
14 cout<<"Enter grade: ";
15 cin>> grades[i];
16
17 sum+=grades[i];
18 }
19
20 cout<<grades<<endl;
21
22 }
23 return 0;
help please!
Please always use code tags - the <> button on the right.


Proper indentation is good too.

Line 11 looks suspect - did that compile?

http://www.cplusplus.com/reference/new/operator%20new%5B%5D/


There is a lot of stuff in the reference section top left of this page - check it out.

You need a for loop to output the values in the array, and you didn't cout the sum variable.

You are leaking memory, because you didn't call delete[]

Hope all goes well.
Thanks! I got it to work. :)
Topic archived. No new replies allowed.