Thread: Structures help

  1. #1
    ------------
    Join Date
    Jun 2005
    Posts
    79

    Structures help

    Ok, i tried learning structures but i keep getting this error:
    syntax error before '.' token

    this is my code:
    Code:
    #include <iostream>
    
    using namespace std;
    
    	struct company {
    		char name[50];
    		int sales;
    		int price;
    		char owner[50];
    	};
    
    int main()
    {
    	cout<<"Enter the company name: \n";
    	cin>>company.name; //get error here
    	cin.ignore();
    	cout<<"How many sales did it make? \n";
    	cin>>company.sales; //and here
    	cin.ignore();
    	cout<<"How much did they sell it for? \n";
    	cin>>company.price; //not to mention here
    	cin.ignore();
    	cout<<"Who is the company owner? \n";
    	cin>>company.owner; //oh yea, and here too...
    	cin.ignore();
    
    	cout<<"Company: " << company.name << endl; //and here
    	cout<<"Sales: " << company.sales << endl; //and here
    	cout<<"Price: " << company.price << endl; //and here
    	cout<<"Owner: " << company.owner << endl; //and here
    }
    anyone know what the problem is?
    Last edited by Goosie; 06-22-2005 at 08:10 PM. Reason: adding comments
    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. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    32
    You need to create an object out of the struct.

    i.e.

    Code:
    company companyObject;
    	cout<<"Enter the company name: \n";
    	cin>>companyObject.name;
    Otherwise you don't have any space allocated for the data, the struct definition just tells the compiler of a new type of data holder that you have made. but you need to give it some memory first

  3. #3
    ------------
    Join Date
    Jun 2005
    Posts
    79
    hmm... now im getting the same error but just on one line:
    Code:
    company company.name;
    ...
    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

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    I suggest something closer to what Charmy posted, try to stay away from creating an instance of an object with the same name as the object... its just confusing.

    Code:
    company acompany;       // acompany is an instance of company
    acompany.sales = 1200;  // Example setting 'sales' element
    "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

  5. #5
    ------------
    Join Date
    Jun 2005
    Posts
    79
    Ok, i tried doing what you said, but its still giving me that same error lol... structures are hard...
    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

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    32
    Your trying to access a member before its defined.

    Code:
    company company.name;
    this won't work since your trying to access the name variable before the structure is even allocated. i needs to be like this

    Code:
    company companyO;
    companyO.name = "Bobs Fish Shop";
    companyO.sales = "2000.52";
    //etc....
    So you define the object. then once its defined, goto a new line and do the operations on all the variables.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Think of 'company' as a template. You have to create something from the template (namely, an object with the given data fields / available operations).

    Code:
    struct foo {
       int a;
       char b;
    }; // 'foo' is now a template for a type of object, but no memory has been allocated yet
    
    int main( ) {
       foo bar; // now memory has been allocated for an object variable 'bar' based off the template 'foo'
       bar.a = 5;
       bar.b = 'f';
    
       cout << bar.a << bar.b << endl;
    /// etc...
    }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    ------------
    Join Date
    Jun 2005
    Posts
    79
    Ok i dont quite get what your saying, but in ^^ that ^^ example you are defining the variables yourself, but i want then to be defined by when the user inputs something... im not quite sure if its the same and im just not thinking right... and why did you put
    Code:
    company company0;
    ??? what does that do exactly?
    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. #9
    myNegReal
    Join Date
    Jun 2005
    Posts
    100
    Based on your above code, are you writing company acompany.name?
    Once you instantiate it: company acompany;
    you only need the instantiated object name (acompany) before the '.' operator.
    Code:
    company acompany;
    acompany.name = "a name";
    acompany.sales = 10;
    /* Not:
        company acompany;
        company acompany.name = "a name"; // error: company acompany is initialization
    */
    Also, when you initialize it, you only put the type name, you can't set a variable in the struct and initialize it in the same statement.

  10. #10
    ------------
    Join Date
    Jun 2005
    Posts
    79
    Ok, i got it to work, but sadly still dont understand it 1 bit... why did i have to add:
    Code:
    company acompany;
    ?? couldnt i have just used company? and what exactly does that do anyways? is it making some kind of pointer from company to acompany? im so confused...
    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

  11. #11
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Quote Originally Posted by Goosie
    Ok i dont quite get what your saying, but in ^^ that ^^ example you are defining the variables yourself, but i want then to be defined by when the user inputs something... im not quite sure if its the same and im just not thinking right... and why did you put
    Code:
    company company0;
    ??? what does that do exactly?
    In my example, the following would have worked as well:
    Code:
    cin >> bar.a >> bar.b;
    I just left that bit out for simplicity.

    At any rate, read over my post again. Defining the structure 'company' does not allocate memory. It just makes a template. 'company company0' allocates memory for a variable named 'company0' based on the template, 'company'.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  12. #12
    ------------
    Join Date
    Jun 2005
    Posts
    79
    BTW here is the final code that i got:

    Code:
    #include <iostream>
    
    using namespace std;
    
    struct company {
    	char name[50];
    	int sales;
    	int price;
    	char owner[50];
    };
    
    int main()
    {
    	company acompany;
    
    	cout<<"Enter the company name: ";
    	cin>>acompany.name;
    	cin.ignore();
    	cout<<"How many sales did it make? ";
    	cin>>acompany.sales;
    	cin.ignore();
    	cout<<"How much did they sell it for? ";
    	cin>>acompany.price;
    	cin.ignore();
    	cout<<"Who is the company owner? ";
    	cin>>acompany.owner;
    	cin.ignore();
    
    	cout<<"Company: " << acompany.name << endl;
    	cout<<"Sales: " << acompany.sales << endl;
    	cout<<"Price: " << acompany.price << endl;
    	cout<<"Owner: " << acompany.owner << 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

  13. #13
    ------------
    Join Date
    Jun 2005
    Posts
    79
    OOOHHHH... so typing:
    Code:
    company acompany;
    is actualy giving the variables in "company" memory so it can actualy be used... and i make it acompany so that i dont get it confused with company? or so i can use the same "template" for diffrent things i would want to use it for? i think im starting to get it...
    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

  14. #14
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Sort of. You are on the right track. I briefly looked over your code, and it looks good.

    A high level way of think about this (i.e. without thinking of memory layouts) is that 'company' is simply a type, like int, char, etc. So, the statements 'int x' and 'company acompany' are analogous: you create a named variable (x / acompany) of a given type (int / company). So, just like you would never say 'int = 3', you wouldn't say 'company.name = "something corp." '.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  15. #15
    ------------
    Join Date
    Jun 2005
    Posts
    79
    ^ok that is making sense... i started writing a new code (i keep using what i learn to try and make it easier) and i'm getting some error saying incompatable types in assignment of 'const char[7] to char[50]' ...c++ is hard lol
    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

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