Is there some way to stop initializing a character array using an appropriate boolean condition inside of an If statement in C++?

  arrays, c++

I am trying to get a user to populate a character array up to 10 letters, but they should (in theory) be able to stop entering letters at any time. Said differently, if a user wants to only enter 5 letters, they should be able to.

My question is how can I use some sort of expression structure like if-else branches to exit the for loop shown below that initializes and populates the array?

#include <iostream>

const int DECLARED_SIZE = 10; //max size of an array in the main

void fillUpArray(char array[], int size, int& actual_size);
void reverseArray(char array[], int actual_size);

int main()
{
  char letters[DECLARED_SIZE];
  int actualSize;

  fillUpArray(letters, DECLARED_SIZE, actualSize);
  reverseArray(letters, actualSize);

}

void fillUpArray(char array[], int size, int& actual_size) //here is where my question is!
{
  int index;
  char letter;
    std::cout << "Enter up to " << size << " letters. Enter something the loop to stopn";
    for(index = 0; index < size; index++)
    {
    std::cin >> letter; 
    if(letter == something that stops this loop)
      break;
    else
          array[index] = letter;
    }
    
    actual_size = index;
}

void reverseArray(char array[], int actual_size)
{
  int index;
  for(index = actual_size; index >= 0; index--)
  {
    std::cout << array[index] << "t";
  }
  std::cout << std::endl;
}

Source: Windows Questions C++

LEAVE A COMMENT