I want to make it so that a certain set of elements in the array on startup is true. Later, when the user takes a cell, its value is changed to false. Example, I declare an array of type bool with 3 elements that are true at the time of launch. In the future, the user is prompted to enter numbers. If the user enters 0, and then 0 again, he is given a warning.

Code:

#include <iostream> using namespace std; int main() { bool myArray[3] = {true}; int action; bool TurnOff = false; do { cout << "Action: "; cin >> action; if (myArray[action] == true) { cout << "This cell is free\n"; myArray[action] = false; } else { cout << "this cell is busy\n"; } } while (TurnOff != true); cout << "Done"; } 

Theoretically, I expected that if the user enters 0 for the first time, a message will be displayed that the cell is free, and a separate message if it is already taken. The problem is that if you enter 0, then 0 again, then everything works as expected. If you enter 1, the program will immediately say that such a cell is already occupied. Example exit program:

 Action: 0 This cell is free Action: 1 this cell is busy Action: 2 this cell is busy Action: 4 This cell is free Action: 5 This cell is free Action: 6 This cell is free Action: 7 this cell is busy Action: 8 This cell is free Action: 9 This cell is free Action: 0 this cell is busy Action: 11 this cell is busy Action: 12 This cell is free Action: 

Why does the program allow numbers to be greater than 2? After all, I indicated the size of the array is 3.

I realize that I absolutely do not understand how boolean arrays work. How to make it so that a large number of array elements are set to True at startup (available) and changed to False only if it has already been used?

    2 answers 2

    "I realize that I don’t understand how the Boolean arrays work." - just like any other type.

    This

     bool myArray[3] = {true}; 

    defines an array of three elements, in which the first (with index 0) element is true , the rest are false . All that is further - untouched memory. To which you have no right to apply, but the responsibility for checking the output beyond the bounds of the array lies only with you.

    So if you want to initialize the array as you wish :) - then

     bool myArray[3] = {true,true,true}; 

    Well, or use, say,

     vector<bool> myArray(3, true); 

    And - be sure to check the output outside the array - it is clear that if you enter an action greater than 2 (or less than 0), you will get UB.

      If you try to display your array, you will see true, false, false . If there are less values ​​in the initialization list than in the array, then the last cells of the array will be initialized with zeros.

      To fill the entire array with some value, use std::fill_n ;

       #include <algorithm> //... std::fill_n(myArray, 3, true); 

      As for going beyond the array, it is solely on your conscience. Going beyond the bounds of the array leads to undefined behavior. Anything can be indefinite behavior: normal operation, segolt, formatting a hard disk. You seem lucky this time :)