### Indicating end of an array I'm doing this program that has to read an array of digits of 20, when the user enter -1, thats the end of the array

i.e

54251801 -1(end of array).

also the last number(9) has to be ignore for future operations.

Cheers guys.
Last edited on You can try using a for loop
 ``123456789101112131415`` ``````int main() { const int size = 20; int array[size]; int number; for(int i = 0; i < 20; i++){ std::cin >> array[i]; if(array[i] == -1) break; } for(int i = 0; i < size; i++){ std::cout << array[i] << " "; } }``````

The code assumes you're always having 20 number. If you need to ignore the last digit, you can lower the range from 20 to 19. track the size as you add to it. you can do that in the first or last element (you will need to allocate an extra slot for this), or you can add it onto the array via a struct (struct mything{int arr[somesize]; int size;};, or you can use a vector (preferred approach, vectors can grow to fit and track their own size for you). I'm only allow to use array, and the break is not working, what else can i do guys?
Cheers The break is working. The problem here:
 5425180123456789 -1(end of array).
is that `-1` isn't a digit. It is certainly an integer. So is `5425180123456789`. You have two integers (one of them very large).

Now if you do the input like this, it becomes more manageable:
 `5 4 2 5 1 8 0 1 2 3 4 5 6 7 8 9 -1`

A modified version of the code posted previously by Hengry
 ``1234567891011121314151617181920212223242526`` ``````#include #include using namespace std; int main() { const int size = 20; int array[size]; int count; for (count = 0; count < size; count++) { std::cin >> array[count]; if (array[count] == -1) break; } for (int i = 0; i < count; i++) { std::cout << setw(3) << (i+1) << setw(2) << array[i] << "\n"; } }``````

 also the last number(9) has to be ignore for future operations.
Is it to be ignored because it is a '9', or simply because it is the last? If the latter, just subtract 1 from count after the end of the first loop.
Last edited on Hey thanks heaps, i realised i was skipping something.
SOLVED!!
Topic archived. No new replies allowed.