PDA

View Full Version : Problem In C++



Vipul Motiwala
01-11-2007, 08:57 PM
:D Hello Everybody... I'm a student and i have recently started learning C++.
We are learning functions right now.. somebody please tell me how to use arrays given in the main function in some other user defined function...

like in the prog:
void main()
{
void array();
int n[*0]={*,2,*,4,5,6,7,8,*,*0}
getch();
array()
}

void array()
{
int var[*0];
cout<<var;
}

u see i want to use the value given in the main function i.e. value stored in array n in my function 'array'... how can i do that.. plz tell... if an example can be given i'll be thankful

Moonbat
01-12-2007, 09:17 AM
You'd have to modify your function to get user input for the array values, instead of using ones entered in the main function.

Vipul Motiwala
01-12-2007, 10:18 AM
You'd have to modify your function to get user input for the array values, instead of using ones entered in the main function.

Can u plz explain????

Mortal_Kill
01-12-2007, 10:50 AM
lol , im the Beast from the EAst in the C ++
so u said u want array ... um.. i dont know whats array suppose 2 mean we never learn that . but i do know - that we use - getchar.. then we user putchar --> its like printf(" bla bla bla \n")
if u want more explane for putchar just ask ;)

Ezekiel
01-12-2007, 01:10 PM
:D Hello Everybody... I'm a student and i have recently started learning C++.
We are learning functions right now.. somebody please tell me how to use arrays given in the main function in some other user defined function...

like in the prog:
void main()
{
void array();
int n[*0]={*,2,*,4,5,6,7,8,*,*0}
getch();
array()
}

void array()
{
int var[*0];
cout<<var;
}

u see i want to use the value given in the main function i.e. value stored in array n in my function 'array'... how can i do that.. plz tell... if an example can be given i'll be thankful

There are many ways, but here are a few:




void array(int var[*0]);

int main()
{
int n[*0] = {*,2,*,4,5,6,7,8,*,*0}
getch();
array(n)
}

void array(int var[*0]);
{
cout << var;
}




void array();
int n[*0] = {*,2,*,4,5,6,7,8,*,*0}

int main()
{
int n[*0] = {*,2,*,4,5,6,7,8,*,*0}
getch();
array(n)
}

void array();
{
cout << n;
}

There's probably mistakes in there, but you get the idea. You can pass the array in through the function parameters, or you can declare the data globally.

One problem with your code was that you declared the array function inside main(), but it should be declared before any of that. I fixed it in both those pieces of code.

Vipul Motiwala
01-13-2007, 09:52 AM
There's probably mistakes in there, but you get the idea. You can pass the array in through the function parameters, or you can declare the data globally.



thanx mike... but can u explain a little more about declaring variables gloabally.

Ezekiel
01-13-2007, 10:16 AM
thanx mike... but can u explain a little more about declaring variables gloabally.

All C++ tutorials explain global variables -- try searching google for one.

Vipul Motiwala
01-15-2007, 11:14 AM
All C++ tutorials explain global variables -- try searching google for one.

OK mike...Thank you for your guidance