-
August 4th, 2012, 08:03 PM
#1
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).
-
August 5th, 2012, 01:08 AM
#2
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;
};
-
August 5th, 2012, 02:34 AM
#3
Re: How can I initialize structures AFTER declaration? (begginers question)
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.
Victor Nijegorodov
-
August 5th, 2012, 03:41 AM
#4
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.
-
August 5th, 2012, 04:05 AM
#5
Re: How can I initialize structures AFTER declaration? (begginers question)
If you move the assignments to within main (at the beginning) it should work.
-
August 5th, 2012, 04:27 AM
#6
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
-
August 5th, 2012, 05:18 AM
#7
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.
Victor Nijegorodov
-
August 5th, 2012, 12:37 PM
#8
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.
-
August 5th, 2012, 01:18 PM
#9
Re: How can I initialize structures AFTER declaration? (begginers question)
Victor Nijegorodov
-
August 5th, 2012, 06:57 PM
#10
Re: How can I initialize structures AFTER declaration? (begginers question)
Thanks for the help, Nuzzle, your solution worked perfectly.
-
August 6th, 2012, 12:35 AM
#11
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|