I would like to know how to declare and call this function from within the main:
void Part1()
{
int array1[10];
int n;
int i;
for (i=1; i<=10; i++)
{
cout<<"Please enter an entry for position "<< i<<": "<<endl;
cin>>n;
array1[i] = n;
}
cout<<endl;
i = 0;
for (int i=0; i<10; i++)
{
cout<<array1[i]<<endl;
}
return 0;
}
When I try to run my int main() I get nothing. I am aware that void does not return anything, but I thought simply calling the function (ie “Part1”) would work. What am I doing wrong?
EDIT: This is how I have been calling it:
int main (){
Part1;
system("PAUSE");
return 0;
}
To call a function, you need to use parentheses:
Part1();. The parentheses contain the arguments that will be passed to the function, but in your case there are none so the parentheses are empty.Also, the index of your first
forloop is incorrect. Your array’s indices start at 0 and end at 9. You seemed to get this correct in the secondforloop but not in the first. It should befor (int i=0; i<10; i++).