Thread: Is there a reason that I cannot access structures properly when creating a function?

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Tamworth, NSW, Australia
    Posts
    16

    Is there a reason that I cannot access structures properly when creating a function?

    Hey guys, sorry, another probably no brainer that's sending me through the wall.

    I can't seem to access the data in a structure when I create a function.. which is really annoying me!

    Code:
    #include <iostream>
    
    using namespace std;
    
    struct persons
    {
        string name;
        string Brief_comments;
        int age;
        int Client_number;
    };
    
    int first_section
    {
        persons structure;
        cout << "Please enter the clients name to store: ";
        cin >> structure.name;
        cin.ignore();
    };
    persons structure gives me an error (Expected primary expression before ect ect ect) basically trying to tell me YOU CAN'T DO THIS!

    Is there a rule in C++ (Or all languages for that matter) that prevents me accessing the structures information through a separate function?

    Thanks in advance all!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There are a few problems with your code:
    • You did not #include <string>
    • Your first_section function definition is missing the parameter list.
    • Although you declared first_section as returning an int, you did not actually return an int.
    • The semi-colon right after the definition of first_section is unnecessary (though not an error).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Tamworth, NSW, Australia
    Posts
    16
    I haven't ever been declaring the header string.. but I guess that would explain the next question I was just about to ask about why it was cutting my strings off when I used space, you're all over my questions! :P :P

    The Parameter list.. I will have to look that up. Is that a major player in the reason that I have the above troubles?

    Got it, return 0 added xD
    Thanks for your quick response!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by xScen3xHdstyl3x
    The Parameter list.. I will have to look that up. Is that a major player in the reason that I have the above troubles?
    Yes, because until you add it (even if it is only an empty matching pair of parentheses), your function definition does not look like a function definition at all.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 09-23-2008, 03:32 AM
  2. Using pointers to access variables in structures
    By Desolation in forum C++ Programming
    Replies: 5
    Last Post: 07-27-2006, 12:10 PM
  3. Random access files and structures
    By DLR in forum C Programming
    Replies: 8
    Last Post: 04-21-2006, 03:24 PM
  4. how to access structures of...
    By threahdead in forum Linux Programming
    Replies: 1
    Last Post: 03-12-2003, 03:01 PM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM