Passing arrays.

Hello, everyone.

I am having an error at line 21. Specifically the code where it says "list[num]=x"

How am I able to fix this code to do what I want? The purpose of my program is to enter in positive numbers in an array (and having it end when 0 is typed) My program also accepts negative values but will ignore them when it is outputted. I believe I have all the code right except for line 21. Any help would be great.

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
#include<iostream>
using namespace std;
const int ARRAY_SIZE(25);
void read_list(const int list[],const int  ARRAY_SIZE);
int main()
{
  int list[ARRAY_SIZE];

    read_list(list, ARRAY_SIZE);

}
void read_list(const int list[],const int  ARRAY_SIZE)
{
    int x(0);
    int num(0);

    cout << "Enter positive numbers (ints) terminated by a 0: " << endl;
    cin >> x;
    while(x != 0 && num < ARRAY_SIZE)
    {
      list[num]=x;
      num++;
      cin >> x;
   
    }
    for(int i = 0; i < num; i++)
    {
     
      if(list[i] > 0)
      {
        cout << list[i] << " ";
      }
    }
    
}
Your first parameter is declared as an array of constant integers, meaning they cannot be changed. This is almost certainly not wha you wanted, just remove the 'const'. The second parameter is fine though.
Thank you very much!
Topic archived. No new replies allowed.