0

I am currently working on a program that sets the index's of the "used" array to true or false depending on if there are characters in the "word" array.

#include <iostream>

int main(){
  const int SIZE = 5;
  char word[SIZE] = {'H', 'e', 'l', '\0', '\0'};

  bool used[SIZE];

  cout << "word: ";
  for (int i = 0; i < SIZE; i++)
  {
     cout << " " << word[i];
  }

  cout << endl << endl;
  for (int i = 0; i < SIZE; i++)
  {
      if (word[i] != '\0')
         used[i] = true;
      else
         used[i] = false;
   }

   cout << "used: ";
   for (int i = 0; i < SIZE; i++)
   {
      cout << " " << used[i];
   }

   return 0;
}

Output

word: H e l
used: 1 1 1 0 0

My question is, can you store 'true' or 'false' in an array opposed to 1's and 0's?

1 Answers1

0

How do i set a boolean to true or false

You can initialise for example:

bool variable = true;

You may be interested in how to output "true" or "false" when inserting a boolean into a output stream. The std::boolalpha I/O manipulator is for that purpose.

eerorika
  • 232,697
  • 12
  • 197
  • 326