N00b needs help with structures

This is a discussion on N00b needs help with structures within the C++ Programming forums, part of the General Programming Boards category; i have recently started learning c++ and before that i did not code in about 5 years and back then ...

  1. #1
    Nothing is impossible! 74466's Avatar
    Join Date
    Jan 2006
    Location
    Bloemfontein South-Africa
    Posts
    29

    Unhappy N00b needs help with structures

    i have recently started learning c++ and before that i did not code in about 5 years and back then i used pascal, now i came across this : http://www.cprogramming.com/tutorial/lesson7.html on this site, and i have absolutely no idea what it is saying to me. would anyone be able to show me an example code using structures?

    i have looked at the tutorial, but it does not make sense to me for some reason... i need an example code where it is/can be used with explanation why u are doing what u are doing

    Thanks.
    Last edited by 74466; 01-23-2006 at 07:40 AM. Reason: refrased the q

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Code:
    struct database {
      int id_number;
      int age;
      float salary;
    };
    
    int main()
    {
      database employee;  //There is now an employee variable that has modifiable 
                          // variables inside it.
      employee.age = 22;
      employee.id_number = 1;
      employee.salary = 12000.21;
    }

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    20
    If you want to pass the structure to a function, pass it by reference, this saves memory and you don't have to return the structure.

    Code:
    struct database {
       int id;
       int age;
       float salary;
    };
    
    void fillInStructure(database& db);
    
    int main()
    {
         database employee;
         
         fillInStructure(db);
    
         return 0;
    }
    
    void fillInStructure(database& db)
    {
        db.age = 22;
        //etc...
    }

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    To be honest, C++ relies more on classes that structs. example:

    Code:
    class Example
    {
    public:
    Example();      // construcror
    ~Example()    // deconstructor
    
    private:
    int age;
    string name;
    
    };
    
    Example::Example()
    {
    
    // constructor code here
    
    }
    
    ~Example()
    {
    }
    
    int main()
    {
    
    // code here 
    
    }
    atthough saying this, structs are still usful.

  5. #5
    Nothing is impossible! 74466's Avatar
    Join Date
    Jan 2006
    Location
    Bloemfontein South-Africa
    Posts
    29
    Thank you, well as i said, i am still new, even though classes may be more usefull, i rather learn everything, as i made the mistake in learning php, wml and html by skipping some more basic functions.

    I wrote this:
    Code:
    #include<iostream>
    using namespace std;
    struct membrs
    {
    int id;
    int age;
    char name[10];
    char cmt[150];
    };
    
    int main()
    {
    membrs str;
    membrs *ptr;
    str.id=001;
    str.age=21;
    str.name=Guest; //error here 
    str.cmt=who are you; //and here
    ptr=&str;
    cout<<"welcome "<<ptr->name<<".\n";
    cout<<"You are member number "<<ptr->id<<" and you are "<<ptr->age<<" years old\n";
    cout<<"Your comment: "<<ptr->cmt<<"\n";
    cin.get();
    }
    and i marked where i get an error, can anyone please help me, tell me wat i am doing wrong... i am using the borland compiler

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    classes and structs are nearly identical in C++. The only difference is (by default) members are "public:" in struct and "private:" in class.

    str.name=Guest; //error here
    str.cmt=who are you; //and here
    You need to learn about strings, which in this case would be
    strcpy(str.name,"Guest"); //error here
    strcpy(str.cmt,"who are you"); //and here

  7. #7
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    your problem is that you want to pass literal values to a sting after it has been initialised. normally its ok to to this:

    Code:
    char astring [20] = "Hello World"; //notice the quotes
    but if you want to initiallise it later on, you'll have to do this:

    Code:
    char astring [20];
    .
    .
    .
    strcpy (astring, "Hello World");
    Here's your code with the modifications, just one point of order first, when you declare struct:

    Code:
    membrs str;
    that works on most compilers, but the ANSI standard, (to the best of my lecturers knowledge) is that you must use the struct keyword like so:

    Code:
    struct membrs str;
    its just something you might run into on different systems. here is your original code with the modifications:

    Code:
    #include<iostream>
    using namespace std;
    struct membrs
    {
    int id;
    int age;
    char name[10];
    char cmt[150];
    };
    
    int main()
    {
    membrs str;
    membrs *ptr;
    str.id=001;
    str.age=21;
    strcpy (str.name,"Guest");
    strcpy (str.cmt, "who are you");
    ptr=&str;
    cout<<"welcome "<<ptr->name<<".\n";
    cout<<"You are member number "<<ptr->id<<" and you are "<<ptr->age<<" years old\n";
    cout<<"Your comment: "<<ptr->cmt<<"\n";
    cin.get();
    }
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  8. #8
    Nothing is impossible! 74466's Avatar
    Join Date
    Jan 2006
    Location
    Bloemfontein South-Africa
    Posts
    29
    Thank you for your help, now, one final question about this.::

    My code:
    Code:
    #include<iostream>
    using namespace std;
    void cdetails();
    struct membrs
    {
    int id;
    int age;
    char name[10];
    char cmt[150];
    };
    
    int main()
    {
    int x;
    x=1;
    int y;
    int pwd;
    
    	while(x==1)
    	{
    	membrs str;
    	membrs *ptr;
    	str.id=001;
    	str.age=21;
    	strcpy (str.name,"Guest");
    	strcpy (str.cmt, "who are you");
    	ptr=&str;
    	cout<<endl;
    	cout<<"welcome "<<ptr->name<<".\n";
    	cout<<"You are member number "<<ptr->id<<" and you are "<<ptr->age<<" years old\n";
    	cout<<"Your comment: "<<ptr->cmt<<"\n";
    	cout<<"If this is correct, press 2. \n";
    	cout<<"If it is incorrect and you wish to change it, please press 1.\n";
    	cin>>y;
    		if(y==1)
    		{
    			cout<<"Please enter your admin password to continue...\n";
    			cin>>pwd;
    			cin.ignore();
    			if(pwd==1234)
    			{
    				cdetails();
    			}
    			else
    			{
    				cout<<"Sorry, incorrect password!\n";
    			}	
    			
    		}
    		cout<<"do you wish to try again?\n";
    		cout<<"Press 1 for yes or any other key for no!\n";
    			cin>>x;
    			cin.ignore();
    		}
    cin.get();
    }
    
    void cdetails()
    {
    	cout<<"Access Granted...press enter!\n";
    	cin.get();
    	cout<<"Please enter Your name (max 10 characters) here: \n";
    	cin>>name;
    	cout<<"How old are you? \n";
    	cin>>age;
    	cin.ignore();
    	cout<<"Tell us a little something about yourself:  \n";
    	cin>>cmt;
    	cin.ignore();
    }
    What i am trying to do is let the user change the details...

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    In your main function you have an instance of the membrs struct called str. Inside the cdetails function, you need to access that struct. The simple way is to pass it as a reference parameter the way QuietWhistler showed. Then you can use it inside cdetails and update its fields with the new user input.

  10. #10
    Nothing is impossible! 74466's Avatar
    Join Date
    Jan 2006
    Location
    Bloemfontein South-Africa
    Posts
    29
    Thank you all... now i have done that all, it allows me to do the inputs, but it does not display the new inputs... ill sorty it out

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> it allows me to do the inputs, but it does not display the new inputs

    Make sure you passed it by reference to the function.

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. Classes and Structures.
    By jrahhali in forum C++ Programming
    Replies: 6
    Last Post: 03-28-2004, 04:03 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

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