Add user-input to array until user inputs 0

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?

Any help would be greatly appreciated.

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
 #include <iostream>

using namespace std;

int main()
{

    int size = 10;
    int numbers[size];

    //store user-input values into the array
    for(int i=0; i<size; ++i)
    {
        cout << "Input an integer: ";
        cin >> numbers[i];
    }


    cout << "The numbers are: ";

    //display array elements
    for (int j=0; j<size; ++j)
    {
        cout << numbers[j];
    }


    return 0;
}
Last edited on
Read number into a temporary variable, check if zero, and only if not zero store in array.

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
#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!
Last edited on
Thank you! I have one more question though, on line 1, #define MAX_ENTRIES 10U , what does 10U mean?
Last edited on
U means the number is of type unsigned.

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int main() {
	constexpr size_t MAX_ENTRIES {10};
	int numbers[MAX_ENTRIES] {};
	size_t count {};

	for (int temp {}; count < MAX_ENTRIES; numbers[count++] = temp) {
		std::cout << "Input an integer (0 to terminate): ";
		std::cin >> temp;

		if (temp == 0)
			break;
	}

	for (size_t j {}; j < count; ++j)
		std::cout << numbers[j] << ' ';

	std::cout << '\n';
}



That is a pre-processor macro:
https://en.wikipedia.org/wiki/C_preprocessor

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++.
Last edited on
Thanks to the both of you -- I'm learning a lot! :)

I'll mark this thread as solved now.
Topic archived. No new replies allowed.