MHB Trouble Printing Out Array Elements with a For Loop

Click For Summary
The discussion centers on issues with printing array elements using a for loop in C++. The user is attempting to read non-negative integers into an array, terminating input with -99, but encounters incorrect output, including unexpected values like 4197088. The provided code contains a logical error in the read_list function, where the while loop is improperly structured, leading to incorrect indexing and counting of elements. A revised version of the read_list function is suggested, which correctly breaks the loop upon encountering -99 and increments the num_elements counter accurately. The print_array function is also adjusted to ensure proper formatting of the output.
osu3124
Messages
3
Reaction score
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:
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 << ".";
}
 
Technology news on Phys.org
Code:
void read_list(int array[], int & num_elements, const int array_size)
{
  cout << "Enter non-negative numbers (ints) terminated by -99" << endl;

  for(int i = 0; i < array_size - 1; i++)
  {
    cin >> array[i];
    if (array[i] == -99)
      break;
    else
      num_elements++;
  }
}

void print_array(const int array[], const int num_elements)
{
  cout << endl << "Original list (" << num_elements << " values):" << endl << array[0];
  
  for(int i = 1; i < num_elements; i++)
  {
    cout << ", " << array[i];
  }
  cout << "." << endl;
}
 
Anthropic announced that an inflection point has been reached where the LLM tools are good enough to help or hinder cybersecurity folks. In the most recent case in September 2025, state hackers used Claude in Agentic mode to break into 30+ high-profile companies, of which 17 or so were actually breached before Anthropic shut it down. They mentioned that Clause hallucinated and told the hackers it was more successful than it was...

Similar threads

  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 15 ·
Replies
15
Views
4K
Replies
12
Views
2K
Replies
12
Views
3K
Replies
20
Views
2K
  • · Replies 66 ·
3
Replies
66
Views
5K
  • · Replies 12 ·
Replies
12
Views
2K
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 22 ·
Replies
22
Views
3K