Thread: Simple Initialization Problem

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    7

    Question Simple Initialization Problem

    I'm a noob and i'm just coding a small program to test what i know but i've hit a problem with this piece of code:

    Code:
    char grid[3][3];
     grid[1][1]=" ";
     grid[1][2]=" ";
     grid[1][3]=" ";
     grid[2][1]=" ";
     grid[2][2]=" ";
     grid[2][3]=" ";
     grid[3][1]=" ";
     grid[3][2]=" ";
     grid[3][3]=" ";
    i know my problem is that i'm initialising this array wrong however i don't know how to fix it to do what i want. Basically each part of this array is to store either a static space ,x or o. Someone i talked to this said i should initialize this as a string rather than char but i dont know how to do this.

    Can someone help me?

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    First of all, arrays are 0 based. So if you make an array of size 3, the elements go from 0 to 2. Next, characters use single quotes; double quotes are for strings (char arrays).

    Then you can initialize your array all at once:
    Code:
    char grid[3][3] = {
      { ' ', ' ', ' ' },
      { ' ', ' ', ' ' },
      { ' ', ' ', ' ' }
    };

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    7
    sorry i obviously did a really bad job of explaining myself, i actually want the variables to store strings (its just got char infront of it atm because thats what it was like when i stopped) thats why i need to change it from initialising a char to initialising a string, however i do not know the keyword that is needed to initialize strings and i put in the code incase what i want to do is impossible and i have to try something else.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Well since this is C++ and you are new to it, I suggest you using the std::string class. This is by far the easiest way for new C++ programmers:
    Code:
    #include <string>
    using namespace std;
    
    string grid[3][3] = {
      { " ", " ", " " },
      { " ", " ", " " },
      { " ", " ", " " }
    };
    Or if you're feeling adventurous and want to do it the C way:
    Code:
    char *grid[3][3] = {
      { " ", " ", " " },
      { " ", " ", " " },
      { " ", " ", " " }
    };
    Watch out there because you need to reallocate strings when you want to put something in it and delete after. Or if you don't want to do that, you can risk buffer overflows and use fixed-length strings, where 10 is the max size of each string including the null character (so 9 real characters total). You can change that number, of course:
    Code:
    char grid[3][3][10] = {
      { " ", " ", " " },
      { " ", " ", " " },
      { " ", " ", " " }
    };
    Again, note that arrays are zero-based. The first element is not at one, its at zero. So these are the bounds: grid[0..2][0..2]

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    11
    I'm having a bit of trouble trying to get a string to store in an array ... heres what I have ... I tried following what he said above but I just got confused...

    Code:
    #include <string.h>
    
    using namespace std;
    
            char iname[8];
    
    	iname[0]="Plain Egg";
    	iname[1]="Bacon and Egg";
    	iname[2]="Muffin";
    	iname[3]="French Toast";
    	iname[4]="Fruit Basket";
    	iname[5]="Cereal";
    	iname[6]="Coffee";
    	iname[7]="Tea";

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    you're forgetting the minor dimension
    Code:
            char iname[8][15];
    
    	iname[0]="Plain Egg";
    	iname[1]="Bacon and Egg";
    	iname[2]="Muffin";
    	iname[3]="French Toast";
    	iname[4]="Fruit Basket";
    	iname[5]="Cereal";
    	iname[6]="Coffee";
    	iname[7]="Tea";
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    wierd guy bart's Avatar
    Join Date
    Aug 2001
    Posts
    87
    Code:
    #include <string.h>
    
    using namespace std;
    
            string iname[8];
    
    	iname[0]="Plain Egg";
    	iname[1]="Bacon and Egg";
    	iname[2]="Muffin";
    	iname[3]="French Toast";
    	iname[4]="Fruit Basket";
    	iname[5]="Cereal";
    	iname[6]="Coffee";
    	iname[7]="Tea";
    Whats the point of including the string library if your not going to use it

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >#include <string.h>
    #include <string>
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Simple Gets() problem
    By akira181 in forum C++ Programming
    Replies: 17
    Last Post: 05-25-2006, 03:28 AM
  3. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM
  4. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 09:10 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 05:33 PM