I am required to write a code for getting 5 positive numebrs from the user.
Here is my code:
cout << "Write 5 positive numbers:" << endl;
int input;
int num[5];
for(int i = 0; i <= 4; ++i){
cin >> input;
if(input < 0){
cout << "Error" << endl;
}else{
num[i] = input;
}
}
for(int i = 0; i <= 4; ++i){
cout << "Number " << i << " = " << num[i] << endl;
}
The problem is that array should store only positive numbers. When I enter negative num, it stores it too and then prints the garbage value. For example inputs are: 3 -2 1 6 8
The output is:
Number 0 = 3
Number 1 = -1608404014
Number 2 = 1
Number 3 = 6
Number 4 = 8
The array should ask the user enter the input until all 5 buckets in array will be filled only by positive numbers