strange error when iterating through a data array

hey, I'm using Dev C++ and the end of the marking periods coming up at my school, so being the 13 year old beast I am I decided to write a program that averages all my grades and gives me a total percent :)

here's my main function:
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
#include <cstdlib>
#include <iostream>
#include "variables.h"
using namespace std;

void GetNumberOfGrades()
{
 #include "variables.h"
 cout<<"How many grades do you need to enter? : ";
 cin>> NumberOfGrades;
}
 void GetGrades()
  {
   #include "variables.h"
   int grades[NumberOfGrades];
   cout<<"Enter your grades below \n\n";
   for(;i < NumberOfGrades; i++)
      {
          cin>> grade;
            for(;j < NumberOfGrades;j ++)
               {
                 grades[j] = grade;
                 cout<<grades[j];
               }
      }
  }
    void IterateAndAdd()
      {
      #include "variables.h"     
           cout<<" \n";
             for(;iter < NumberOfGrades;iter ++)
             {
              cout<<" "<<grades[iter]<<" \n";                    
             }
      }
int main(int argc, char *argv[])
{
    GetNumberOfGrades();
    GetGrades();
    IterateAndAdd();
    system("PAUSE");
    return EXIT_SUCCESS;
}


And here's my variables.h header file :

1
2
3
4
5
6
7
8
9
10
11
12

#ifndef variables_H
#define variables_H

int grades;
int NumberOfGrades;
int iterate;
int i = 0;
int grade;
int j = 0;
int iter = 0;
#endif 


and yes, I know I could declare my variables globaly but I'm new to header files and wanted to play around with them :), however when I compile it gives me this weird error:

C:\Dev-Cpp\grade avg program.cpp In function `void IterateAndAdd()':
33 C:\Dev-Cpp\grade avg program.cpp invalid types `int[int]' for array subscript
C:\Dev-Cpp\Makefile.win [Build Error] ["grade avg program.o"] Error 1

Hope you guys can help :) by the way this is my first post so I'm not sure if my brackets work right so if you see [source] [/source] its b/c I didn't use HTML right :) Thanks for reading this guys, see ya
Last edited on
Please change your [source] tags to [code] tags.
Your header file is defining global variables. Your include directive is just like cutting and pasting it into the main code.

You have defined a global int variable grades for holding your grades. It is global. Then in GetGrades() you are defining an int array called grades. It is a local variable with the same name as your global grades. The global grades is now hidden in this scope and the local array is used. You allocate memory for your array using a variable NumberOfGrades. The way you have done this is not standard C++ and is disallowed in other compilers. You should allocate memory for this type of array dynamically, using new.

Now, you are trying to IterateAndAdd() your grades, but the variable grades being accessed here is again that global int you defined in the variables header. It is not an array, which is why you are getting that subscript error.

~psault
Well your approach to header and variable inclusion is most novel and refreshing - and completely wrong of course.
Your variables are still global anyway (the #include directives on lines 8, 14 and 29 have no effect)
Last edited on
Topic archived. No new replies allowed.