CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Hybrid View

  1. #1
    Join Date
    May 2006
    Posts
    102

    How can I initialize structures AFTER declaration? (begginers question)

    I would like to write a program that prompts the user to enter a series of client names which the program will then store in an array of structures. I'm thinking something along the lines of:


    struct sInfo

    {
    vector<string> vName;
    float fClientHoldings;
    };


    sInfo sClientData[100]; //create an array of structures for up to 100 client's

    sClientData[0].vName="Acme"; //these will be inputted by the user in my program, but I've attempted to initialize them here for simplification
    sClientData[1].vName="Enron";


    Everything I've read on the net so far seems to suggest that I can't declare a structure array after I declare it. Is this true? If so, is there an alternate approach that I should be using?

    Thanks (sorry for the newb questions).

  2. #2
    Join Date
    May 2009
    Posts
    2,413

    Re: How can I initialize structures AFTER declaration? (begginers question)

    I don't quite get what's the problem but judging from how you use sInfo I guess it should look like this,

    Code:
    struct sInfo
     {
     string vName;
     float fClientHoldings;
     };

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How can I initialize structures AFTER declaration? (begginers question)

    Quote Originally Posted by Ulnarian View Post
    Code:
    struct sInfo
    {
        vector<string> vName;
        float fClientHoldings;
    };
    
    sInfo sClientData[100]; //create an array of structures for up to 100 client's
    You may want to implement the constructor(s) in your struct sInfo to initialize its members.
    Victor Nijegorodov

  4. #4
    Join Date
    May 2006
    Posts
    102

    Re: How can I initialize structures AFTER declaration? (begginers question)

    Thanks for the help, here's a more simple program I've written just to show the problem I'm running into. I've also listed the error messages that I'm getting from my compiler as comments next to the offending code:


    #include <iostream>
    #include <string>

    using namespace std;

    struct clientdata
    {
    string vClientName;
    };

    clientdata sClientInfo[3];

    sClientInfo[0].vClientName="PROTEUS Inc."; //expected constructor, destructor or type conversion before '.' token ALSO expected ',' or ';' before '.' token
    sClientInfo[1].vClientName="Melkor Holdings"; //expected constructor, destructor or type conversion before '.' token ALSO expected ',' or ';' before '.' token
    sClientInfo[2].vClientName="Initech"; //expected constructor, destructor or type conversion before '.' token ALSO expected ',' or ';' before '.' token

    int main()
    {
    cout << sClientInfo[1].vClientName;
    int wait; //stops program from closing before I can see the output
    cin >> wait;
    return 0;
    }

    Again, as I'm pretty new at this, I assume this is a simple error, but I can't seem to find whats causing the compile error.

  5. #5
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: How can I initialize structures AFTER declaration? (begginers question)

    Is this what you try to do ?
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    struct clientdata
    {
        string vClientName;
        float fClientHoldings;
    };
    
    // declare and initialize a global array of clientdata
    clientdata sClientInfo[3] = { { "PROTEUS Inc.", 0.0f },  { "Melkor Holdings", 1.1f } , {  "Initech", 2.2f  } } ;
    
    int main() {
    Kurt

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How can I initialize structures AFTER declaration? (begginers question)

    Quote Originally Posted by Ulnarian View Post
    ...
    Again, as I'm pretty new at this, I assume this is a simple error, but I can't seem to find whats causing the compile error.
    But you are NOT new at CG (you are here since 2006!)
    So you must know how and why you have to use Code tags! So your avoiding these tags only shows your disrespect to all of us who read your post and try to help you.
    Last edited by VictorN; August 5th, 2012 at 01:16 PM.
    Victor Nijegorodov

  7. #7
    Join Date
    May 2009
    Posts
    2,413

    Re: How can I initialize structures AFTER declaration? (begginers question)

    If you move the assignments to within main (at the beginning) it should work.

  8. #8
    Join Date
    May 2006
    Posts
    102

    Re: How can I initialize structures AFTER declaration? (begginers question)

    While I once understood the "how" of using code tags, I was unable to remember it (16 years is a long time). And, while I did look for a tag button, I was unable to find it and ultimately decided that the "why" of using the tag probably didnt apply here given the simplicity of the code. I apologize for the disrespect.

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How can I initialize structures AFTER declaration? (begginers question)

    Victor Nijegorodov

  10. #10
    Join Date
    May 2006
    Posts
    102

    Re: How can I initialize structures AFTER declaration? (begginers question)

    Thanks for the help, Nuzzle, your solution worked perfectly.

  11. #11
    Join Date
    May 2009
    Posts
    2,413

    Re: How can I initialize structures AFTER declaration? (begginers question)

    Quote Originally Posted by Ulnarian View Post
    , your solution worked perfectly.
    Another option would be to put those assignments in their own function, say initialize(), which is then called from the beginning of main. Initialize can be called from anywhere whenever you want to reset all client data to the initial state.

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