Thread: boolean arrays

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    29

    boolean arrays

    I'm trying to make an array that says true or false about a dining room and kitchen. I am familiar with int & char arrays, but not sure how to do a boolean one...


    Code:
    #include <iostream> 
    #include <string>
    #include <cstdlib> 
    using namespace std; 
    
    
    int main()
    
    {
    
    	bool dining, kitchen; //declaring variables to boolean type.
    
    	bool lights [2] = {dining, kitchen}; 
    	lights[0] = false;
    	lights[1] = false;
    	cout << lights[0] << lights[1] << endl;
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    I am familiar with int & char arrays, but not sure how to do a boolean one...
    Arrays of these primitive types are all declared in exactly the same way. Whats the specific problem your having?

    This is disregarding the fact that in your above code, either (one of):
    - "dining" and "kitchen" variables can be removed as they dont really do anything--just use the array indices 0 and 1 as you are now to represent these values, rather than taking up more memory and using more variables
    - dont use an array at all, and just keep your "dining" and "kitchen" variables.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    oh gotcha, I was trying to store a true/false in the variable, but also in an array. I see what you mean about the array with no variables:
    Code:
     bool lights = {};  lights[1] = true;
    Thanks!

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    When declaring the array with 2 elements, you should do it how you did in your first post, i.e.
    Code:
    bool lights[2]
    Then assign and access elements "lights[0]" and "lights[1]" normally.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Novice needs help
    By ghaasemi in forum C++ Programming
    Replies: 9
    Last Post: 05-30-2009, 08:20 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Replies: 4
    Last Post: 04-02-2004, 07:30 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM