12345678910111213141516171819202122232425262728293031
#include <iostream> std::size_t read_positive( int* array, std::size_t n ) { std::size_t pos = 0 ; int value ; while( pos < n ) { if( std::cout << "value? " && std::cin >> value ) // if a number was read { if( value >= 0 ) array[pos++] = value ; // if not negative else std::cerr << "negative value was ignored\n" ; } else break ; // input failed } return pos ; // number of values entered } int main() { constexpr std::size_t N = 100 ; int array[N] = {0} ; const std::size_t nvalues = read_positive( array, N ) ; for( std::size_t i = 0 ; i < nvalues ; ++i ) std::cout << array[i] << ' ' ; std::cout << '\n' ; }