- #1
osu3124
- 3
- 0
I am having problems with printing out each elements of the array using a for loop
It's suppose to look like this:
Enter non-negative numbers (ints) terminated by -99
1 2 3 4 5 6 -99
Original list (6 values):
1, 2, 3, 4, 5, 6.
the code's output looks like:
Enter non-negative numbers (ints) terminated by -99
1 2 3 4 5 6 -99
Original list (6 values):
-99, 0, 4197088, 0, 0, 0, .
This is my code:
It's suppose to look like this:
Enter non-negative numbers (ints) terminated by -99
1 2 3 4 5 6 -99
Original list (6 values):
1, 2, 3, 4, 5, 6.
the code's output looks like:
Enter non-negative numbers (ints) terminated by -99
1 2 3 4 5 6 -99
Original list (6 values):
-99, 0, 4197088, 0, 0, 0, .
This is my code:
Code:
#include <iostream>
#include <cmath>
using namespace std;
void read_list(int array[], int & num_elements, const int array_size);
void print_array(const int array[], const int num_elements);
int main()
{
const int array_size(20);
int array[array_size];
int num_elements(0);
read_list(array, num_elements, array_size);
print_array(array, num_elements);
return 0;
}
void read_list(int array[], int & num_elements, const int array_size)
{
cout << "Enter non-negative numbers (ints) terminated by -99" << endl;
cin >> array[0];
for(int i = 0; i <array_size-1; i++)
{
while(array[i] != -99)
{
cin >> array[i];
num_elements = num_elements + 1;
}
break;
}
}
void print_array(const int array[], const int num_elements)
{
cout << endl << "Original list (" << num_elements << " values):" << endl;
for(int i=0; i<num_elements; i++)
{
cout << array[i] << ", ";
}
cout << ".";
}