how do I declare and array and make it global

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 751 Views - Last Post: 01 May 2016 - 05:52 PM Rate Topic: -----

#1 castro  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 82
  • Joined: 12-November 05

how do I declare and array and make it global

Posted 27 April 2016 - 07:37 PM

hello

i want to declare a global array to accept first name and course name for a number of entries say 5.

I want to call this array in another method.

I know how to declare an array:
so this is what my code will be.

/
/ i want array here a string array
int arry_size=5;
string name_course[arry_size];

before the int main()
{
}
void display ()
{
cout << " enter name:"<<endl;
cin>> name_course[0];

cout << name_course[0];
}

:code:

I want something like that..

please help i am stack with this the whole day it makes me fill dump..

if i get this then i have to implement it in a class

This post has been edited by modi123_1: 27 April 2016 - 07:40 PM
Reason for edit:: please use the code tag button in the editor


Is This A Good Question/Topic? 0
  • +

Replies To: how do I declare and array and make it global

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13554
  • View blog
  • Posts: 54,090
  • Joined: 12-June 08

Re: how do I declare and array and make it global

Posted 27 April 2016 - 07:41 PM

... and what's wrong with what you did?
Was This Post Helpful? 0
  • +
  • -

#3 kaa  Icon User is offline

  • D.I.C Addict

Reputation: 224
  • View blog
  • Posts: 805
  • Joined: 15-April 11

Re: how do I declare and array and make it global

Posted 27 April 2016 - 07:41 PM

You have to make the size a constant:
const int arry_size = 5;
string name_course[arry_size];

int main()
{
  // other code ...


Was This Post Helpful? 0
  • +
  • -

#4 castro  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 82
  • Joined: 12-November 05

Re: how do I declare and array and make it global

Posted 27 April 2016 - 07:50 PM

View Postmodi123_1, on 27 April 2016 - 07:41 PM, said:

... and what's wrong with what you did?



There is nothing wrong right but it does not work when i implement it this way

#include <iostream>
#include <string>

using namespace std;

void  setup( int cnt );
int cnt ;
string Name[cnt];

void  setup( int cnt ){
	
cout <<" how many entries do you want? "<<endl;
cin  >> cnt;

	int count=0;
	do
	{
	
		cout <<"	Enter Professor FirstName & LastName: " <<endl;
		cin  >> Name[count];
 		count++;
	} while (count < cnt);
 		return;
 	
 }
int main ()  { 


return 0;

}


This post has been edited by castro: 27 April 2016 - 07:51 PM

Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13554
  • View blog
  • Posts: 54,090
  • Joined: 12-June 08

Re: how do I declare and array and make it global

Posted 27 April 2016 - 07:58 PM

Your main is suspiciously empty. As in you don't call any methods or what not inside of it. Why?
Was This Post Helpful? 0
  • +
  • -

#6 castro  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 82
  • Joined: 12-November 05

Re: how do I declare and array and make it global

Posted 27 April 2016 - 07:59 PM

Can anyone help pls
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13554
  • View blog
  • Posts: 54,090
  • Joined: 12-June 08

Re: how do I declare and array and make it global

Posted 27 April 2016 - 08:02 PM

Again - nothing in your code executes since your main is empty.
Was This Post Helpful? 0
  • +
  • -

#8 castro  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 82
  • Joined: 12-November 05

Re: how do I declare and array and make it global

Posted 27 April 2016 - 08:03 PM

View Postmodi123_1, on 27 April 2016 - 07:58 PM, said:

Your main is suspiciously empty. As in you don't call any methods or what not inside of it. Why?


there is a method in the main which calls the setup;

so in the main i have a method called start();

in start i call
setup();
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13554
  • View blog
  • Posts: 54,090
  • Joined: 12-June 08

Re: how do I declare and array and make it global

Posted 27 April 2016 - 08:07 PM

... and that was left out of your example of your current code above?
Was This Post Helpful? 0
  • +
  • -

#10 castro  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 82
  • Joined: 12-November 05

Re: how do I declare and array and make it global

Posted 27 April 2016 - 08:31 PM

I edit the code

#include <iostream>
#include <string>
using namespace std;

void  setup (int cnt);
//const int counter =0;
 int counter ;
string Name[counter];

void  setup( int cnt){
   
    
    int count=0;
    do
    {
      cout <<"  Enter Professor FirstName & LastName: " <<endl;
      cin  >> Name[count];
      count++;
    } while (count < cnt);
    return;
 }
 
 int main (){
    int num;
    cout <<" how many entries do you want? "<<endl;
    cin  >> num;
  setup(num);
  return 0;
}     


when i run the above with this << int counter >> i get the error below

error: array bound is not an integer constant before ']'
token In function 'void setup(int)': 17:15: error: 'Name' was not declared in this scope

to correct i uncomment the code below and it works but the problem is when i enter the first value the program stops
please help!!!!
//const int counter =0;
Was This Post Helpful? 0
  • +
  • -

#11 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13554
  • View blog
  • Posts: 54,090
  • Joined: 12-June 08

Re: how do I declare and array and make it global

Posted 27 April 2016 - 08:36 PM

Well, yeah. Your 'counter' has no value. Get rid of it in line 8.. and instantiate your darn array in your main. voila.

07	 int counter ;
08	string Name[counter];

Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is offline

  • member icon

Reputation: 5344
  • View blog
  • Posts: 16,676
  • Joined: 25-December 09

Re: how do I declare and array and make it global

Posted 27 April 2016 - 08:39 PM

And remember in C++ array sizes must be compile time constants.

You should also avoid global variables, make the variables local to a function and learn to pass them to and from your functions as required.

Jim

This post has been edited by jimblumberg: 27 April 2016 - 08:40 PM

Was This Post Helpful? 0
  • +
  • -

#13 castro  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 82
  • Joined: 12-November 05

Re: how do I declare and array and make it global

Posted 27 April 2016 - 08:43 PM

View Postmodi123_1, on 27 April 2016 - 08:36 PM, said:

Well, yeah. Your 'counter' has no value. Get rid of it in line 8.. and instantiate your darn array in your main. voila.

07	 int counter ;
08	string Name[counter];


how do you do it.?
also if i understand you if you instantiate how will the array be global if i want to access it in other methods which are not in the main?

thank you with the example of instantiating pls help with that
Was This Post Helpful? 0
  • +
  • -

#14 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13554
  • View blog
  • Posts: 54,090
  • Joined: 12-June 08

Re: how do I declare and array and make it global

Posted 27 April 2016 - 08:47 PM

It's about scope. You declare your variable in global scope (line 7).. but instantiate it in the main.. it still makes it global. If you declared it inside of the main.. well that has only scope for the main.

Come on.. you indicate you have been programming for 9 years... this shouldn't be the first time you have dealt with scope.
Was This Post Helpful? 0
  • +
  • -

#15 castro  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 82
  • Joined: 12-November 05

Re: how do I declare and array and make it global

Posted 27 April 2016 - 09:15 PM

View Postjimblumberg, on 27 April 2016 - 08:39 PM, said:

And remember in C++ array sizes must be compile time constants.

You should also avoid global variables, make the variables local to a function and learn to pass them to and from your functions as required.

Jim

Thanks Jim you are right

but that means i am making the array constant which i don't want to

View Postmodi123_1, on 27 April 2016 - 08:47 PM, said:

It's about scope. You declare your variable in global scope (line 7).. but instantiate it in the main.. it still makes it global. If you declared it inside of the main.. well that has only scope for the main.

Come on.. you indicate you have been programming for 9 years... this shouldn't be the first time you have dealt with scope.

it sounds crazy right a programmer for 9 years however i stopped coding for some 5 years and was into systems and database..


I am just trying to recall some basics
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2