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).
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;
};
Re: How can I initialize structures AFTER declaration? (begginers question)
Quote:
Originally Posted by
Ulnarian
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.
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)
If you move the assignments to within main (at the beginning) it should work.
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
Re: How can I initialize structures AFTER declaration? (begginers question)
Quote:
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. :eek:
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)
Re: How can I initialize structures AFTER declaration? (begginers question)
Thanks for the help, Nuzzle, your solution worked perfectly.
Re: How can I initialize structures AFTER declaration? (begginers question)
Quote:
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.