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?