need help with array

This is a discussion on need help with array within the C++ Programming forums, part of the General Programming Boards category; I need to use array for a 'map' im making and is it possible to make to arrays 'overlap' so ...

  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,261
    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%.

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    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, 02:28 AM
  5. Replies: 1
    Last Post: 04-25-2006, 12:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21