Thread: need help with array

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    5

    need help with array

    I need to use array for a 'map' im making and is it possible to make to arrays 'overlap' so they are linked together. and i want to name each array according to the current place he is in.

    i know this isn`t much but i try to give a rough idea of what i mean.

    also i get 'too many characters in constant' error i know what it means but is there any way i can put several characters into an array?

    Code:
    #include <iostream>
    #include <string.h>
    
    using namespace std;
    
    void main (void)
    {
    	int room [3][6];
    	char room1 [3][6];
    
    	room[0][0] = 1, room[0][1] = 2, room[0][2] = 3, room[0][3] = 4, room[0][4] = 5, room[0][5] = 6, room[0][6] = 7;
    
    	room1[0][0] = 'Northwing' , room1[0][1] = 'hallway';
    
    	cout << room[0][6];

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    A string (which is delimited by double quotes, not single quotes, in C++) is an array of char. The string "ABC" is represented as an array of four characters 'A', 'B', 'C', and '\0' (zero).

    room1[0][0] is a single char. If you try to squeeze multiple characters into a single char, then the compiler will complain. Just as, in the real world, you can't squeeze three people into the space occupied by one, as they won't survive.

    Look up the map (template) class in the C++ standard library, to do what (I guess) you are really trying to do.

    And main() returns int, not void.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Listen to grumpy. Also, your code is in the c style. Use c++ style:
    Code:
    #include <iostream>
    #include <cstring>  // not used
    
    int main()
    {
        using namespace std;
        int room [3][6];
        char room1 [3][6];
    
        room[0][0] = 1, room[0][1] = 2,
        room[0][2] = 3, room[0][3] = 4,
        room[0][4] = 5, room[0][5] = 6,
        room[0][6] = 7;
    
        // room1[0][0] = 'Northwing' , room1[0][1] = 'hallway';
        // the above line is an error
    
        cout << room[0][6];
        // . . . 
        return 0;
    }
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  2. Replies: 9
    Last Post: 04-07-2010, 10:03 PM
  3. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  4. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  5. Replies: 1
    Last Post: 04-25-2006, 12:14 AM