-
Problem In C++
: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 [B]main[/B] 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
-
You'd have to modify your function to get user input for the array values, instead of using ones entered in the main function.
-
[QUOTE=Moonbat]You'd have to modify your function to get user input for the array values, instead of using ones entered in the main function.[/QUOTE]
Can u plz explain????
-
lol
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 ;)
-
[QUOTE=Vipul Motiwala]: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 [B]main[/B] 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[/QUOTE]
There are many ways, but here are a few:
[CODE]
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;
}[/CODE]
[CODE]
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;
}[/CODE]
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.
-
[QUOTE=mike*0*]
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.
[/QUOTE]
thanx mike... but can u explain a little more about declaring variables gloabally.
-
[QUOTE=Vipul Motiwala]thanx mike... but can u explain a little more about declaring variables gloabally.[/QUOTE]
All C++ tutorials explain global variables -- try searching google for one.
-
[QUOTE=mike*0*]All C++ tutorials explain global variables -- try searching google for one.[/QUOTE]
OK mike...Thank you for your guidance