Hi - I'm having a problem converting a couple of things that work fine spelled out in function main, but not when I try to define them as functions. One is a process of filling in a one-dimensional array. I'd like to just be able to say "here's the array; fill it in with fillArray()", but can't find the right syntax. Here's what works and what doesn't so far:

What works:

1 // Rich Mansfield 0457321 11/11/09
 2 // CIS180 fillArrays.cpp
 3 // This asks the user for a number of scores to be entered,
 4 // then it reads the scores into a one-dimensional array;
 5 // but when I try to make a function out of it, it won't compile.
 6 #include <iostream>
 7 using namespace std;
 8 
 9 
10 
11 
12 int main()
13 {
14    const int SIZE = 3;
15    int arrayName[SIZE];
16 
17    cout  << "Enter " << SIZE  << " elements: ";
18    for (int i = 0; i < SIZE; i++)
19       cin   >> arrayName[i];
20    
21    cout  << "Here's the array: ";
22    for (int i = 0; i < SIZE; i++)
23       cout  << arrayName[i]   << " ";
24    cout  << endl;
25    
26    return 0;
27 }  //End main fn

And here's what does NOT work:

1 // Rich Mansfield 0457321 11/6/09
 2 // CIS180 createArraysFn.cpp
 3 // This is an attempt to make a fn that asks a user for a no. of scores 
 4 // to be entered, then input the scores into a one-dimensional array;
 5 // but it won't compile.
 6 
 7 #include <iostream>
 8 using namespace std;
 9 
10 //Function prototype
11 int   fillArray(int);//Trying to make a fn that fills an array
12 
13 int main()
14 {
15    const int SIZE = 3;
16    int arrayName[SIZE];
17 
18    fillArray(arrayName[SIZE]);
19 
20    return 0;
21 }  //End main fn
22 
23 //****************************************************
24 //Function definition
25 int   fillArray(int num)
26 {
27    const int SIZE = 3;
28    for (int i=0; i < SIZE; i++)
29       cin   >> num[i];
30 }  //End fillArray fn.

What am I doing wrong or not doing right?

Rich Mansfield

Recommended Answers

All 4 Replies

The parameter in the function is different from the parameter in the function call. To be more specific, the call has an array as the parameter, while the definition has an int value that is not an array. They must be the same data type.
But if I may offer my personal take on the matter:
I would make the function a void function with no parameters. Your current function is also wrong because it is declared as int but does not return an int value. Void solves this problem because a void function does not return any values.
Here is what I had in mind.

#include <iostream>
using namespace std;
 //Function prototype
void  fillArray();
int main()
{
    fillArray();
    return 0;

 }  //End main fn
 
 //****************************************************
 //Function definition
void fillArray()
{
     const int SIZE = 3;
     int num[SIZE];
     for (int i=0; i < SIZE; i++)
         cin   >> num[i];
     for (int i=0; i < SIZE; i++)
         cout   << num[i]; 
 }  //End fillArray fn.

The function creates the array, fills it, and outputs its contents. Let me know if you need any clarification.

try this:

#include <iostream>

using namespace std;

void  fillArray(int[], const int);

int main()
{
    const int SIZE = 3;
    int num[SIZE];
    fillArray(num, SIZE);
    for (int i=0; i < SIZE; i++)
         cout<<num[i]<<endl;
    return 0;

 }

void fillArray(int arr[], const int sz)
{
     for (int i=0; i < sz; i++)
         cin>>arr[i];
}

And next time use google (c++ arrays as parameters).

commented: Not only solves the problem, but also provides another place to look for more solutions. +1

They have given you the answer, but let me expand on that :

Your prototype for fillArray is this :

int   fillArray(int);//Trying to make a fn that fills an array

What does that mean ? It says that fillArray takes an int variable and
returns an int variable. What you want it to say is that fillArray
takes a int array, and a int variable and returns nothing.

So it would be something like this :

void  fillArray(int anArray[], int itsSize);

Again, that means that fillArray takes an int array and its size as a
variable. Then all you have to do is make sure its defined.

Very elegant, just what I wanted - a one-liner in main. Thanks.

Rich
============

The parameter in the function is different from the parameter in the function call. To be more specific, the call has an array as the parameter, while the definition has an int value that is not an array. They must be the same data type.
But if I may offer my personal take on the matter:
I would make the function a void function with no parameters. Your current function is also wrong because it is declared as int but does not return an int value. Void solves this problem because a void function does not return any values.
Here is what I had in mind.

#include <iostream>
using namespace std;
 //Function prototype
void  fillArray();
int main()
{
    fillArray();
    return 0;

 }  //End main fn
 
 //****************************************************
 //Function definition
void fillArray()
{
     const int SIZE = 3;
     int num[SIZE];
     for (int i=0; i < SIZE; i++)
         cin   >> num[i];
     for (int i=0; i < SIZE; i++)
         cout   << num[i]; 
 }  //End fillArray fn.

The function creates the array, fills it, and outputs its contents. Let me know if you need any clarification.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.