Noob : Creating a global array?

This is a discussion on Noob : Creating a global array? within the C++ Programming forums, part of the General Programming Boards category; Hi! I am trying to declare a global array for use throughout a program. The program is a math question ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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,475
    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.
    I'm just trying to be a better person - My Name Is Earl

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, 08:42 AM
  2. Set an array with unknown dimensions as global
    By paok in forum C Programming
    Replies: 11
    Last Post: 12-07-2007, 12:37 PM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  4. Creating array from data file...
    By Crankit211 in forum C Programming
    Replies: 2
    Last Post: 12-06-2001, 08: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21