Can someone clean my array code? Won't run

I made a program that reads in20 numbers, each between 10 to 100 and if a user inputs a number not in that range they are asked to re-input. After reading in all the values, I need to display all the unique vclues.
Thanks!

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
#include <iostream>
using namespace std;

int counter = 0;
int num = 0;

int main ()
{

int array = new array [20];

while(counter <= 20){
cin << num;

 if(num>=10 && num<=100){
 array[counter] = num;
 counter++;
 }


 else {
 cout << "Enter a number between 1 and 10";
}


}

for(int x=0; x<=20; x++)
{
cout << array[x];
}
return 0;
}
hi,

in this case it isn't really necessary to create a dynamic array, you should replace int array = new array[20] with int array[20].
If you still want to use dynamic arrays i sugesst to read the following tutorial: http://www.cplusplus.com/doc/tutorial/dynamic/

there is also a small mistake in the text in line 22, which should be cout << "Enter a number between 10 and 100" but this is not the reason why your program does not work of course.

In addition the condition of the for-loop is wrong, because this way it would run through 21 times instead of 20 times, so you should change it to
for(x=0;x<20;x++)
Notice that i did remove the 'int' too, because you should not create new varibles in the middle of a function. You should rather declare this variable at the beginning of the function, together with all other variables.

Same with the while-loop, change the condition to counter<20 to avoid errors.
Last edited on
line 13: cin << num;

should be, cin >> num. As mentioned above really watch your bounds on the arrays.
Topic archived. No new replies allowed.