Best option for a list of variables

Not sure if I know how to explain this efficiently but basically I want to make a "list" of 50 objects(?), each with it's own string and two double variables, so that I can pass those three values for each into another function depending on which one is selected. Does that make sense? What is the best way for me to accomplish this?
Last edited on
It might make more sense to explain what I'm actually trying to accomplish. I'm writing a lottery program and depending on the state the person is from the program will pass in different values as far as the tax rate, lump sum, annual payments, etc. I'm wondering if it would be easier to define a whole list of each of those things for each state, which is what I'm asking how to do, or if I should just define the tax rate and calculate the payments themselves. The latter is obviously the "better" solution, but whichever is easier is fine with me. And either way my main question is what is the best way to define a "list" of variables for each state?
Last edited on
Since you have a fixed number, you could use a struct array.
1
2
3
4
5
6
7
8
struct Lottery
{ string state_name;
   double tax_rate;
   double lump_sum;
  ... etc
};

Lottery lottery[50];


You could also use a vector.
 
  vector<Lottery> lottery.




What you need (and describe precisely!) is called struct. A struct can contain several variables of different types. Check this out:

http://www.cplusplus.com/doc/tutorial/structures/
Thanks guys! So, am I doing this correctly?

struct lottery
{
string stateName;
string stateAbbrev
double taxRate;
double lumpSum;
}AL, AK, AZ, AR, CA, CO, CT, DE, FL, GA, HI, ID, IL, IN,
IA, KS, KY, LA, ME, MD, MA, MI, MN, MS, MO, MT, NE, NV,
NH, NJ, NM, NY, NC, ND, OH, OK, OR, PA, RI, SC, SD, TN,
TX, UT, VT, VA, WA, WV, WI, WY;

AL.stateName = "Alabama"
AL.stateAbbrev = "AL"
AL.taxRate = .07;

etc.
Last edited on
You got the idea right, be careful with the syntax:
1
2
3
4
5
6
7
struct lottery  
{ 
string stateName;
string stateAbbrev    // you´re missing ;
double taxRate;
double lumpSum;
} 


The initialization and the rest of story should go inside a function.
I mean, this:

1
2
3
AL.stateName = "Alabama"  // ;
AL.stateAbbrev = "AL"     // ;
AL.taxRate = .07;


goes inside some function, right?
AbstractionAnon has a very good point, you could creat an array of lottery.

1
2
3
4
5
6
7
8
9
10
11
12
struct lottery  
{ 
string stateName;
string stateAbbrev;
double taxRate;
double lumpSum;
};            // no particular lottery variable is created

int main(){
    struct lottery[50];
    //...
}


Now you have an array of 50 cells (0 through 49) each one of wich is a "thing" called lottery that contains....

This could be of great help for automation, example:

1
2
3
for (int i = 0; i<50; i++){
    cout << lottery[i].stateName << "' s tax rate is " << lottery[i].taxRate << endl;
}


If you create 50 diferent structs, with no particular order, you would have to do this manually...
Last edited on
I see, that does make much more sense. Thanks guys, really helpful.

I'm assuming there's no way to declare some sort of template(can't think of a better name) where I could just do AL = ( "Alabama" , "AL" , .07 , 10000000); for each of the states, instead of assigning every one of the four variables for each state individually?
I´m almost shure you can do that, but using brakets:

 
lottery Al = { "Alabama" , "AL" , .07 , 10000000};


As you correctly did, you must fill the fields in the same order you declared the variables on the struct declaration.
@Marcos Modenesi

lottery Al = { "Alabama" , "AL" , .07 , 10000000};

But doing that defeats the purpose of having the array.

@innominate
A better solution would be to read the values in from a file. Stick with this sort of syntax to assign values:

lottery[i].stateName = "Alabama";

Of course the code for assigning values goes into a while loop.

If you are unsure how this works, look in the documentation section.

HTH


Topic archived. No new replies allowed.