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?
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.
Re: How can I initialize structures AFTER declaration? (begginers question)
Originally Posted by Ulnarian
...
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.
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.
Re: How can I initialize structures AFTER declaration? (begginers question)
Originally Posted by Ulnarian
, 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.
Bookmarks