Error Messages

Hey guys, I've got this code I'm working on for homework and it's pretty complete I just can't seem to figure out how to fix these error messages.

Here's the 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
61
62
63
#include <iostream>
#include <stdlib.h>
using namespace std;


int main ()

{
    int n=0,n_initial; // n will be the number of assignments entered in the system.
                     // since n will be counting down, I want to save n_initial as well.
    
// # OF EXERCISES
cout << "How many exercises to input?  >  ";
cin >> n;
n_initial = n;

int grades [2][n]; // 2 rows for possible and actual, n columns because you will have a column for each item.



//SCORE RECIEVED COUNTDOWN
while (n>0)

{
      int x,y;
cout << endl << endl << "Score recieved for exercise " << n << ":   ";
cin >> x;
grades [0][n] = x;
cout << endl << "Points possible for exercise " << n << ":  ";
cin >> y;
grades [1][n] = y;

n = n-1;
}

//CALCULATION OF TOTAL AND PERCENT
double score_total=0,pointspossible_total=0,rows_1,rows_2;

//SCORE TOTAL
int xy=0;
n = n_initial;

for (int xy=0; xy < n_initial; xy++)
{
    score_total += grades [0][n];
    n--;
}
    cout << endl << "Score Total = "<< score_total << endl;

//POINTS POSSIBLE TOTAL
xy=0;
n = n_initial;

for (int xy=0; xy < n_initial; xy++)
{
    pointspossible_total += grades [1][n];
    n--;
}
    cout << "Possible points Total = " << pointspossible_total << endl;
    cout << "Your total is " << score_total << " out of " << pointspossible_total << ", and your percentage is " << (score_total/pointspossible_total)*100 << "%" <<endl;
    system ("pause");
return (0);
}



And I'm getting these errors.

(17): error C2057: expected constant expression
(17): error C2466: cannot allocate an array of constant size 0
(17): error C2133: 'grades' : unknown size

Any help at all on how to fix this would be much appreciated!

Last edited on
Use a vector or dynamically allocated memory for the array. It is not legal C++ to declare an array whose size cannot be determined at compile time.
Yeah I've been reading this, but I'm not quite sure how to implement the array while still getting the desired results.

I know that its something like

vector<int> grades (0,n);

Would this work the same as an array?
It just doesn't seem to work because once I create the vector, every time I try to use the vector later in the code it doesn't seem to accept it.
I would suggest doing something like the following:

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
#include <iostream>
#include <vector>
using namespace std;

struct score_info
{
    int earned ;
    int max ;
};

int main ()

{
    int n=0; 
    cout << "How many exercises to input?  >  ";
    cin >> n ;

    vector<score_info> grades ;

    //SCORE RECIEVED COUNTDOWN
    while (grades.size() < n)
    {
        score_info score ;
  
        cout << "\n\nScore recieved for exercise " << n << ":   ";
        cin >> score.earned ;
 
        cout << "\nPoints possible for exercise " << n << ":  ";
        cin >> score.max ;

        grades.push_back(score) ;
    }

    // ...

    // Now you may access the vector like so:
    // grades[index].earned or grades[index].max to get at the members of the struct.
Last edited on
Topic archived. No new replies allowed.