CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Join Date
    May 2009
    Posts
    16

    Defining outside a structure

    Code:
    struct items {
          char name;
          int quantity;
    }item[10];
    Sorry if this is a stupid question, but how do I define name from outside the structure?

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Defining outside a structure

    Code:
    char name;
    BTW, most names consist of more than one character.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  3. #3
    Join Date
    May 2009
    Posts
    16

    Re: Defining outside a structure

    I've tried. I don't remember the error I get, I'm not at home, but it wasn't working. And I actually have it as

    Code:
    char name[10];
    in the structure.

  4. #4
    Join Date
    Aug 2007
    Posts
    858

    Re: Defining outside a structure

    What exactly do you mean by "define name from outside the structure"?

  5. #5
    Join Date
    May 2009
    Posts
    16

    Re: Defining outside a structure

    Sorry for not being clear. I want to give a value to item[x].name. It seems like I should be easy but I can't do it.

  6. #6
    Join Date
    Aug 2007
    Posts
    858

    Re: Defining outside a structure

    I assume you mean initializing since you seem to know how to do assignment. For structs you do it like so:

    Code:
    struct items 
    {
      char name[10];
      int quantity;
    };
    
    items item[10] = {
      { "Apple", 10 }, // item[0]
      { "Pear", 20 }, // item[1]
      { "Peach", 5 } // item[2]
      // and so on...
    };

  7. #7
    Join Date
    May 2009
    Posts
    16

    Re: Defining outside a structure

    Ah. That's pretty simple. So I can seperate the members with a comma and initialize the structure all at once? And what would be the difference between define and initialize? I'm still new at this.

  8. #8
    Join Date
    Aug 2007
    Posts
    858

    Re: Defining outside a structure

    Initialization is giving a variable an initial value when it's first created.

    Code:
    string s = "Hello World!"; // s will be initialized to contain "Hello World!"
    // could also be written:
    string s("Hello World!");
    Definition has to do with giving the compiler information - it goes with declaration.

    When you declare a class/struct or function, you tell the compiler it's going to exist and some the basic information about it, but don't give the full details.
    When you define something, you give the compiler the complete information about the object.
    It's also possible to combine declaration and definition. The ability to seperate the declaration of something from its definition will become more important later on, because it's what allows you to split a program up into multiple files.

    Code:
    // declare Foo
    void Foo( );
    
    //declare and define Bar
    void Bar( )
    {
      cout << "Bar" << endl;
    }
    
    int main( )
    {
      Foo( );
      Bar( );
      return 0;
    }
    
    // define Foo
    void Foo( )
    {
      cout << "Foo" << endl;
    }

  9. #9
    Join Date
    May 2009
    Posts
    16

    Re: Defining outside a structure

    That cleared some stuff up. Sometimes tutorials leave this stuff unclear, like how to initialize a structure. Thank you very much.

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Defining outside a structure

    You can, of course, also define a constructor for the struct. Assuming you're in C++ rather than C.

  11. #11
    Join Date
    May 2009
    Posts
    16

    Re: Defining outside a structure

    I had tried reading about constructors earlier but didn't really understand them. How could I use them here?

  12. #12
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Defining outside a structure

    Code:
    struct items 
    {
      char name[10];
      int quantity;
      items(char *name_in, int quantity_in): quantity(quantity_in)
      {
          strncpy(name, name_in, 9);
          name[9] = 0;
      }
    };
    
    items myitem("Bob",5);
    
    
    items itemarray[5] = {
      items("Apple",10), // itemarray[0]
      items("Pear", 20), // itemarray[1]
      items("Peach", 5) // itemarray[2]
      // and so on...
    };
    
    myitem = items("Joe",10); // Not possible using {} syntax above---that's for initialization only
    Of course, that puts significant restrictions on what names you can use (<=9 characters); and the need to use strcpy means you can't just throw name into the initializer list like quantity. These restrictions would be lifted if you did:

    Code:
    struct items 
    {
      std::string name;
      int quantity;
      items(const std::string &name_in, int quantity_in): name(name_in), quantity(quantity_in)
      {}
    };
    If you prefer, the constructor may take a const char * instead of a const string&. Makes no difference in this case.

  13. #13
    Join Date
    May 2009
    Posts
    16

    Re: Defining outside a structure

    That's starting to use concepts I don't understand. Pointers have thus far eluded me, and I know arrays are like constant pointer, but I just don't get all that.

  14. #14
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Defining outside a structure

    Pointers aren't the point, if you'll pardon the phrase. You could make the argument be a const char[10] instead of a const char* if you prefer. Makes no difference in this context.

    Quite literally *no* difference----the compiler will ignore the [10] and just read the function argument as const char [], which is equivalent to const char *.....

  15. #15
    Join Date
    May 2009
    Posts
    16

    Re: Defining outside a structure

    So this is my full program. It's basically an engine for a text based RPG and It's the first I've ever written, so I'm sure there might be a lot of bad practices in here. I have just one error left.

    error: expected `)' before ‘]’ token|

    on line 84.

    Code:
    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    using namespace std;
    
    char user_in[10];
    int current_room = 0;
    int main();
    void get_item();
    
    //Dungeon Room Variables
    struct rooms
    {
        int north_room;
        int east_room;
        int south_room;
        int west_room;
        int room_item[3];
        string description;
    };
    
    //Dungeon Map and Room Descriptions
    rooms room[5] = {   
        // N   E   S   W   Item #s      Room Descriptions
        { -1, -1, -1,  1, {0, 0, 0}, "You are in room 0. You may go west."},  // Room 0
        {  3,  0,  2, -1, {1, 0, 0}, "You are in room 1. You may go north, east, or south."},  // Room 1
        {  1, -1, -1, -1, {0, 0, 0}, "You are in room 2. You may go north."},   // Room 2
        {  4, -1,  1, -1, {0, 0, 0}, "You are in room 3. You may go north or south."},   // Room 3
        { -1, -1,  3, -1, {0, 0, 0}, "You are in room 4. You may go south."},    // Room 4
    };
    
    //Item Variables
    struct items
    {
        char name[10];
        int quantity;
    };
    
    // Item Details
    items item[10] = {
        { "", 0 },      // Item[0]
        { "Key", 0 },   // Item[1]
        { "", 0 },      // Item[2]
        { "", 0 },      // Item[3]
        { "", 0 },      // Item[4]
        { "", 0 },      // Item[5]
        { "", 0 },      // Item[6]
        { "", 0 },      // Item[7]
        { "", 0 },      // Item[8]
        { "", 0 },      // Item[9]
    };
    
    //Find what the user typed
    int user_input()
    {
        int previous_room = current_room;
        if (strcmp(user_in, "north") == 0)
           {
            current_room = room[current_room].north_room;   //Equals value of north room and goes there if possible
           }
        else if (strcmp(user_in, "south") == 0)
           {
            current_room = room[current_room].south_room;   //Equals value of south room and goes there if possible
           }
        else if (strcmp(user_in, "west") == 0)
           {
            current_room = room[current_room].west_room;    //Equals value of west room and goes there if possible
           }
        else if (strcmp(user_in, "east") == 0)
           {
            current_room = room[current_room].east_room;    //Equals value of east room and goes there if possible
           }
        else if (strcmp(user_in, "get") == 0)
           {
                get_item();
           }
        else
           {
            cout << "I don't understand." << endl;
           }
        if (current_room == -1)
           {
            cout << "You can't go that way." << endl;
            current_room = previous_room;
           }
    return (current_room);
    }
    
    //Finds item to get
    void get_item ()
    {
        int x;
        cout << "Get what item?" << endl;
        cin >> user_in;
    
        for (x=0; x<3; x++)
        {
            if (strcmp(user_in, item[room[current_room].room_item[x]].name]) == 0)  //User Input = Item Name
            {
                item[room[current_room].room_item[x]].quantity++;   //Adds one to item quantity
                room[current_room].room_item[x] = 0;    //Item is no longer in room
            }
        }
    }
    
    int main()
    {
        int y = 0;
            do {
            cout << room[current_room].description << endl;
            cin >> user_in;
            system("clear");
            current_room = user_input();
        } while (y == 0);
    return 0;
    }
    Last edited by Laus; May 21st, 2009 at 07:04 PM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured