Thread: Noob : Creating a global array?

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Noob : Creating a global array?

    Hi!

    I am trying to declare a global array for use throughout a program. The program is a math question generator (sets).

    My 'main' program is as follows:-

    Code:
    #include <cstdlib> 
    #include <iostream>
    
    #include <ctime> 
    
    using namespace std;
    
    int arrayname[9];
    
    int main() 
    { 
    	srand((unsigned)time(0)); 
    
    	int random_integer; 
    	cout << "Given the sets A and B, where:-" << endl << endl << endl << "A = {";
    	for(int index=0; index<5; index++)
    	{ 
    		random_integer = (rand()%9)+1; 
    		arrayname[index] = random_integer;
    		cout << arrayname[index] << ", ";
    
    	} 
    	cout << "}" << endl << endl << "and" << endl << endl << "B = {";
    
    	for(int index=6; index<10; index++)
    	{ 
    		random_integer = (rand()%9)+1; 
    		arrayname[index] = random_integer;
    		cout << arrayname[index] << ", ";
    
    	} 
    	cout << "}";
    	cout << arrayname[index] << "test";
    	cout << endl << endl << endl << endl;
    
    	system("pause");
    }
    I have never created another file for use during a program, I found a tutorial saying I should declare the global array in a separate header file, I've made this:-

    Code:
    #include <iostream>
    
    using namespace std;
    
    int index = 0;
    extern int arrayname[index];
    The header file is reporting that "'index' : undeclared identifier".

    I understand global arrays are baaddd, but I'm wanting to use one at present, so in summary I'm looking for some advice on how to setup the global file correctly.

    Many thanks for any advice!

    Swerve.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It would be
    extern int arrayname[9];

    Also, putting "using namespace std" in a header file is very poor style.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Thanks Salem, will try it now.

    Yeah i wondered about the namespace line, and the includes. First time created a second file, will delete the namespace.



    EDIT - hmm, trying a few ways to write the file but am still getting:

    'index' : undeclared identifier

    I currently have the file as follows:-

    Code:
    #include <iostream>
    
    int main()
    {
    int index = 0;
    extern int arrayname[9];
    }
    Last edited by Swerve; 06-07-2008 at 10:34 AM.

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    I would guess you need to #include your header file into the one containing main.

    Code:
    #include <iostream>
    #include "yourheaderhere.h"
    
    int main()
    {
       int index = 0;
       extern int arrayname[ 9 ];
    
       return 0;
    }
    The error is saying index is not declared because you need to include the header file you have made. I ma guessing you are using a .h extention.
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamically creating an array of pointers
    By cs32 in forum C Programming
    Replies: 4
    Last Post: 02-06-2008, 09:42 AM
  2. Set an array with unknown dimensions as global
    By paok in forum C Programming
    Replies: 11
    Last Post: 12-07-2007, 01:37 PM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Creating array from data file...
    By Crankit211 in forum C Programming
    Replies: 2
    Last Post: 12-06-2001, 09:45 PM
  5. Creating an array of object pointers
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 10:01 PM

Tags for this Thread