-2

I have been coding another programming challenge from a book. It is about asking a user to input n numbers inside an array. Every time the user inputs, the current numbers entered should show up. Numbers less than or equal to zero should not be accepted. I have managed to do the first condition. However, it even shows the "empty" slots. I have tMy code will explain it. Here:

#include <iostream>
using namespace std;
int main() {
    int size = 0, input_num [100], i, j;
    
    cout << "Enter an array size: ";
    cin >> size;
        if (size <= 0) {
            while (size <= 0) {
               cout << "Enter an array size again: ";
               cin >> size;
            } 
        }
       
    cout << "\n\nYou may now enter " << size << " numbers ";
    cout << "\n------ ------ ------ ------\n";
    
    for (i = 0; i < size; i++) {
        cout << "Enter a number: ";
        cin >> input_num [i];
       
         cout << "\nEntered Numbers: ";
            for (j = 0; j < size; j++) {
                cout << input_num [j] << "  " ;
            }
        cout << "\n";
        
    }
 
}

1 Answers1

1

In your output section, it should be j <= i, not j < size.

cout << "\nEntered Numbers: ";
for (j = 0; j <= i; j++) {
    cout << input_num [j] << "  " ;
}
cout << "\n";
Feng BLn
  • 11
  • 1