Filling an Array with a function

Hi All,

My English is not te best, so I try t wright it in understandable language

I have to declare an Array, and after that fill that array with numbers using a function.
The code that I wrote is below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <iomanip>

using namespace std;

void invoer(const int a[], int lengte);

int main(){
	
int AANTAL;
cout << "How many numbers you want to add:"; 
cin >> AANTAL;

int cijfers1[AANTAL];


invoer(cijfers1, AANTAL);

}

void invoer(const int a[], int const lengte){
	for (int i=0; i<lengte; i++){
	cout << i+1 << "st number is :";	
	cin = a[i];	
			}	
	cin.get();
}


if you want to help me, can you write it in simple English and use the terms that are close to my C++ syntax. I have only see the basics of C++ till pointers ans arrays. But I think that the idea is do this without using pointers.

thanks and greetz

Thanks for the help.
Last edited on
line 24: change = to >>

Also though line 14 will probably work, it still non-standard and should be avoided. Use vectors or if you absolutely need to use arrays, use int* cijfers1 = new int[AANTAL]; and add delete cijfers1; at the end of main()
If the array is constant, then you cannot modify it. Change your function to:

void invoer(int [], int const );
Last edited on
@fspererg i don't -exactly understand,
what you need to do
but, i hope this example helps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//fillArray.cpp
//#
#include <iostream>
using std::cout;
using std::cin;
using std::endl;


void printArray(const int a[],int size);
void fillArray(int a[],int size);

int main(){

int numbers;

cout<<"How many numbers you want to add?: ";
cin>>numbers;

int array[numbers];

cout<<"Enter "<<numbers<<" numbers: ";
fillArray(array,numbers);
printArray(array,numbers);



return 0; //indicates success
}//end main

void printArray(const int a[],int size){
        for(int index=0;index<size;index++)
                cout<<a[index]<<' ';

cout<<endl; //new line
}//end function printArray

void fillArray(int array[],int size){
        for(int index=0;index<size;index++)
                cin>>array[index];
}//end function fillArray 



Eyenrique-MacBook-Pro:Help Eyenrique$ ./fillArray 
How many numbers you want to add?: 10
Enter 10 numbers: 1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10 
Hi All,

Thanks foy your reply.

The aim of the code was to fill the array with numbers through a function.

Smac89, you where right. I made from the Array an Const int.. Thats why I couldn't store numbers in it. Thanks a lot all, for helping me solve this little problem.

You will see me soon again :).
Topic archived. No new replies allowed.