Thread: Structures help

  1. #16
    ------------
    Join Date
    Jun 2005
    Posts
    79
    OMG can someone please help me in why im getting that error?! lol

    Code:
    #include <iostream>
    
    using namespace std;
    
    struct people {
    	char name[50];
    	int age;
    	char city[50];
    };
    
    int main()
    {
    	people person1;
    	people person2;
    	people person3;
    
    	person1.name = "person1";
    	person1.age = 18;
    	person1.city = "city1";
    
    	person2.name = "person2";
    	person2.age = 19;
    	person2.city = "city2";
    
    	person3.name = "person3";
    	person3.age = 20;
    	person3.city = "city3";
    
    
    	cout<<person1.name<<" is "<<person1.age<<" years old, and lives in "<<person1.city<<endl;
    	cout<<person2.name<< " is " <<person2.age<<" years old, and lives in "<<person2.city<<endl;
    	cout<<person3.name<<" is "<<person3.age<<" years old, and lives in "<<person3.city<<endl;
    }
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  2. #17
    ------------
    Join Date
    Jun 2005
    Posts
    79
    ok i fixed it, but it raises another question... i think i have the structures (to a decent level at least...)

    Code:
    #include <iostream>
    
    using namespace std;
    
    struct people {
    	string name;
    	int age;
    	string city;
    };
    
    int main()
    {
    	people person1;
    	people person2;
    	people person3;
    
    	person1.name = "person1";
    	person1.age = 18;
    	person1.city = "place1";
    
    	person2.name = "person2";
    	person2.age = 16;
    	person2.city = "place2";
    
    	person3.name = "person3";
    	person3.age = 9;
    	person3.city = "place3";
    
    
    	cout<<person1.name<<" is "<<person1.age<<" years old, and lives in "<<person1.city<<endl;
    	cout<<person2.name<< " is " <<person2.age<<" years old, and lives in "<<person2.city<<endl;
    	cout<<person3.name<<" is "<<person3.age<<" years old, and lives in "<<person3.city<<endl;
    }
    but what is the diffrence between
    Code:
    char (name)[50];
    and:
    Code:
    string (name);
    ?? can i just use string instead of char? since it seems string can support spaces, and char just gives errors...
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  3. #18
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Goosie
    but what is the diffrence between
    Code:
    char (name)[50];
    and:
    Code:
    string (name);
    ?? can i just use string instead of char? since it seems string can support spaces, and char just gives errors...
    The difference is char (name)[50] is what you had to use in C programming, as well as a lot of other methods like strcpy and stuff to be able to get a text string correctly.. and in C++ its all done for you using a string.

    char (name)[50] allocates 50 spaces for characters (which you have to copy using some str thing), but a string done it all for you, its the easist thing since slicing bread

    And of course this tutorial is the ownage: http://www.cprogramming.com/tutorial/string.html
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  4. #19
    ------------
    Join Date
    Jun 2005
    Posts
    79
    so i dont even need the char (name) [size] in c++? because every time i try to use it i get that same error message and it drives me crazy... thats beutiful to know! lol...

    edit:

    Oh yea... thanks everyone for helping me out i understand structures... entering day 4 of c++ and i can write programs that do absolutely nothing useful!!! lol... i was thinking of starting a text based game, but i dont know how to handle character names yet or functions... i'm hoping to be able to start on it within the next week or so

    edit again:

    Im tired and about to leave (just incase someone responds to this, i wount be here until the morning or later)
    Last edited by Goosie; 06-22-2005 at 10:37 PM. Reason: adding stuff...
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  5. #20
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    some clarification

    string is a standard c++ library class which internally manipulates a array of chars, just like the one you declared "char name[]".
    using directly an char array brings lots of constraints. And to manipulate it you have to use functions that iterate through out the array and apply the needed changes.
    string is a class that internally does all that for you, and provides a simpler interface, so that you can manipulate it like any primitive variable.

    About the struct - here's a clear explanation

    Whenever you want to use a int what do you do ? what is a int ?
    Code:
    int y = 0;
    y = y+2;
    ...
    in that declaration you mention that y will be some variable that you'll manipulate. That variable is stored somewhere in memory. It doesn't mind.
    int is the type of that variable. You're telling that y will be a integer. I could be a char, a float, some other type. You just have to choose.

    Code:
    struct company{
     ... //contents - don't really matter
    };
    in that declaration you're stating that company is the name of a structure. company will then be associated with a type of structures which was defined together with de declaration of struct company{}... In other words company will be a user defined type, as a short hand to the contents of the struct.
    Then, like the instantiatoin of int y. you'll want to create an instance of that structure to manipulate it.
    Code:
    struct company x;
    int            y;
    now you're saying that x will be a variable to manipulate, just like y. The type of x is that of your defined structure, just like the type of y is a int.
    so now you have an instance of your struct
    you can create multiple instances of the same struct, each being independant one from another
    Code:
    struct company x, y;
    struct company w;
    now you want to fill the struct with something
    Code:
    struct company{
        char name[50];
        int sales;
    };
    
    struct company x;
    if x is a instance of the structure, you will have instances of the structure variables within it. to acess each of the variables use the '.' operator
    Code:
    struct company x;
    strcpy(x.name, "big deal" );
    s.sales = 50;
    when acessing a variable with the '.' operator just treat it normally as you would treat any variable of that type, like not being inside a struct

    to end, you may want to create a instance of a struct with already defined values for each contained variable, or in other words, initialization values for each variable.
    considering your struct
    Code:
    struct company {
        char name[50];
        int sales;
        int price;
        char owner[50];
    };
    
    struct company x = {
        "big deal" , //field name, which is char[]
        1000, //field sales which is a int
        50000, //field price which is a int
        "mister lazy" //field owner which is char[]
    };
    after that declaration, each of the structs contained variables will have those assigned values. And note that the order of the arguments for the initialization, is the same of the declaration of the struct



    a structure simply holds a defined amount of variables to the programmers liking.
    Last edited by xErath; 06-23-2005 at 12:32 AM.

  6. #21
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    #include<xErath.h>
    You're out of date xErath, and in the near future your deprecated version may be removed from the standard or compilers, GL.
    Last edited by Dae; 06-23-2005 at 12:14 AM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  7. #22
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    it's ansi C... deprecate yourself

  8. #23
    ------------
    Join Date
    Jun 2005
    Posts
    79
    sorry, but im not getting the char and string variables still... every time i want to use char (with a structure at leat) i end up with an error, but string works perfectly...
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  9. #24
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    What's your current code?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #25
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by xErath
    it's ansi C... deprecate yourself
    Oh.. I'm sorry what forum are you posting in again?
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  11. #26
    ------------
    Join Date
    Jun 2005
    Posts
    79
    currently im just making random programs to learn this stuff... i just started on a game that i plan on turning into a MUD codebase someday soon... right now im trying to get it to take a name, password, and race into a txt file... maybe im jumping a little ahead of myself, but im using it so i actualy have a set code that im working on instead of just random codes here and there...
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  12. #27
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    playing around with char[] implies knowledge of c-style string manipulation which you'll learn. meanwhile std::string eases up things. yet you'll have to learn c-style strig manipulation, or you'll miss an important part of c/c++ programming.
    a char[] is an array, therefore you can simply use it as you'd use a int or a float. but std::string overloads those operators.

  13. #28
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by xErath
    playing around with char[] implies knowledge of c-style string manipulation which you'll learn. meanwhile std::string eases up things. yet you'll have to learn c-style strig manipulation, or you'll miss an important part of c/c++ programming.
    a char[] is an array, therefore you can simply use it as you'd use a int or a float. but std::string overloads those operators.
    I agree with xErath. I plan on going back to a C tutorial or two to practice char strings. I wouldnt use the char[] strings in my programs but I think learning it gives good base, especially for arrays. Plus one day you'll run across them and you'll want to know what does what so you can help convert it to C++ if thats the objective. Also alternatively I'm convinced char arrays may be the only way I'm going to get that text-data working with bitfield compression (std::string aint havin' :-/).. so thats an example, they may come in use where std::string doesnt.

    Of course stick with std::string until checking out char[] strings feels comfortable, since std::string is a hell of a lot more convenient and easy.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  14. #29
    Registered User
    Join Date
    Jun 2005
    Posts
    32
    Of course stick with std::string until checking out char[] strings feels comfortable, since std::string is a hell of a lot more convenient and easy.
    Convenient if you know all the internal function calls, and easier only if you need the features it comes with.

    Any formal Programming training you get on C/C++ almost never introduces std::string until you are exteremly well versed in arrays, besides there are alot of instances when when using char arrays or char pointers is much more convenient than using std::string.

    I would Suggest just playing around with arrays first you will quickly find their limitations, and how they work. I would actually advise that you don't use std::string until you understand the basics of how strings are stored in memory (i.e. the String is stored in the array with the null term char '\0' indicating its actually a string), and how to access each element of the string seperatly, becuase you use this same method to access them in the std::string class.

    Trust me, you will be better off.

  15. #30
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    I can see that logic. I did read some C before skipping to C++ and I covered the concept of char arrays so maybe that made it easier. I just found, especially reading the basic std::string tutorial on this site (covering some of the function calls), that strings were much easier to use for basic console output (like his MUD game thing), but I didnt mean go any further than that without learning char strings.

    And its not like you wouldnt have to learn function calls when learning char strings but they tell you to learn it really early because thats a C tutorial where its necessary to know them to make strings, but in C++ it isnt so why not give it some time is all I meant.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  3. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM