Already got down the 'keep adding to the array' until array is 'full'.
But how do I get the program to stop adding into the array when the user inputs 0?
#define MAX_ENTRIES 10U
int numbers[MAX_ENTRIES];
// store user-input values into the array
size_t count = 0U;
int temp;
for(size_t i = 0; i < MAX_ENTRIES; ++i)
{
std::cout << "Input an integer: ";
std::cin >> temp;
if (temp != 0)
{
numbers[count++] = temp;
}
else
{
std::cout << "Ignored zero input!" << std:endl;
// could also add a 'break' statement here to exit the loop on first zero input
}
}
// display array elements
for (size_t j = 0U; j < count; ++j)
{
cout << numbers[j];
}
Note: Using a separate count variable to record the actual number of inputs!
However in C++ this would be coded as a const and not as a #define (which is the c way).
Note that in the OP, L9 is not standard C++ as size is not const. This is accepted by some compilers as a language extension to match C. As C++ consider:
Specifically, we define the macro MAX_ENTRIES as 10U. Simply put, it means that any occurrence of the token "MAX_ENTRIES" in your source code will be expanded to "10U", by the pre-processor, before the code is passed on to the compiler. The suffix "U" simply means unsigned. It is not strictly required here, but the correct type for the size of an array is size_t, and that is an unsigned type, so I make the constant unsigned too :-)
Note: In C, the size of an array must be a constant expression, so even a const (read-only) variable is not allowed. That is why we use a pre-processor macro for such things. I think this is relaxed in C++.