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