pass values between functions?
pass values between functions?
Hi, somewhat new to C++ but I've been trying to learn for a bit now. I had a quick question...
First, it should be known that this is not quite directX yet, I am doing this all in console first, THEN making it so its a visual directX program, at least that's what my plan is.
I am working on a what should be simple program that randomly generates a star, and a random number of planets. very simple, I got it so it makes a random type of star and between 0 and 14 planets.
The next part however is making it so I can make an object for each planet, and have each object get stats from the star. Basically, the planet objects need to know what type of star was generated so it can know a few values from the star generating function like the distance from the star it is, the type of star it is, ect.
I am having trouble in that I cannot get any values from my star generating function. I have searched online for how to pass values from one function to another, have looked for a way to return more then one value, but to no avail. I thought I struck gold when I found out about global variables but apparently that is a big no no since everything I read about them was that they are evil.
I know there has to be a better way but I just cant figure it out. Any help would be appreciated.
First, it should be known that this is not quite directX yet, I am doing this all in console first, THEN making it so its a visual directX program, at least that's what my plan is.
I am working on a what should be simple program that randomly generates a star, and a random number of planets. very simple, I got it so it makes a random type of star and between 0 and 14 planets.
The next part however is making it so I can make an object for each planet, and have each object get stats from the star. Basically, the planet objects need to know what type of star was generated so it can know a few values from the star generating function like the distance from the star it is, the type of star it is, ect.
I am having trouble in that I cannot get any values from my star generating function. I have searched online for how to pass values from one function to another, have looked for a way to return more then one value, but to no avail. I thought I struck gold when I found out about global variables but apparently that is a big no no since everything I read about them was that they are evil.
I know there has to be a better way but I just cant figure it out. Any help would be appreciated.
Re: pass values between functions?
mate, if you don't post your code it is difficult to help you. However, a very quick way to make a function pass a whole set of values, is to define a structure first, the structure will enclose the parameters that you need the function to return. Your function will have to return a structure instead of an int or float etc.
1) make a structure
2) then create an instance
3) make your function
1) make a structure
Code: Select all
struct Planet
{
int mass;
int number;
int O2level;
string name;
};
Code: Select all
Planet Charlie;
Code: Select all
Planet UpdatePlanet(....)
{
.....
.....
.....
return Charlie;
}
Re: pass values between functions?
I've never heard of structs before, gonna have to check that out, looks like it might be able to do what I want it to do.
This is the function I have for generating the star. There are probably many things I can do to make it better so suggestions are welcome.
Basically what I want is to get the values for habMin, habMax, and sType passed to a planetGen() function, in which I want to use an array and a for loop to instantiate the necessary number of objects of the planet type (correct wording? Remember I'm fairly new to this) habMax and habMin are used to find if the planet is in a certain range, if it is in the range, hZone = true
Thanks
This is the function I have for generating the star. There are probably many things I can do to make it better so suggestions are welcome.
Code: Select all
void starGen( int habMin, int habMax, char sType )
{
// Random number
int randNum;
srand ( time(NULL) );
randNum = rand();
int starType = randNum % 5;
// Check for the type of star that was randomly picked
if (starType == 0)
{
sType = 'A';
habMin = 16;
habMax = 20;
}
else
if (starType == 1)
{
sType = 'F';
habMin = 12;
habMax = 15;
}
else
if (starType == 2)
{
sType = 'G';
habMin = 7;
habMax = 12;
}
else
if (starType == 3)
{
sType = 'K';
habMin = 5;
habMax = 6;
}
else
if (starType == 4)
{
sType = 'M';
habMin = 2;
habMax = 4;
}
cout << "Star type is: " << sType <<endl;
}
Code: Select all
class planet
{
int distance;
int size;
int type;
bool hZone;
void atmosphere();
}
Thanks
Re: pass values between functions?
I am sorry, I am not sure I understand you logic.
you say
then you say
Also,
what are you doing there?
you take "habMin" and "habMax" as parameters for your function and then reset them? so why would you need them in the first place if the only check appears to be for "starType" only?
If you lay out the bigger picture of what you are trying to do I might be able to understand your code better
you say
and that's fine. Your planetGen() functon is currently taking those parameters as inputto get the values for habMin, habMax, and sType passed to a planetGen() function
then you say
i don't see any of these features in your function right now.function, in which I want to use an array and a for loop to instantiate the necessary number of objects of the planet type
Also,
Code: Select all
if (starType == 0)
{
sType = 'A';
habMin = 16;
habMax = 20;
}
else
if (starType == 1)
{
sType = 'F';
habMin = 12;
habMax = 15;
}
else ....
you take "habMin" and "habMax" as parameters for your function and then reset them? so why would you need them in the first place if the only check appears to be for "starType" only?
If you lay out the bigger picture of what you are trying to do I might be able to understand your code better
Re: pass values between functions?
I think I understand what you want to do, wait a sec
Re: pass values between functions?
If I understand properly you want to have a function that passes several values in order for you to be able to access these values from else where e.g. other functions, right?
define a struct
make an instance
design your function
define a struct
Code: Select all
//"stats." structure stores 2 values
struct stats
{
int sType;
int habMin;
int habMax;
};
Code: Select all
//one "stats." created
stats Star;
then, do what you want with your values//it generates a star ( returning type, habMin, habMax )
stats starGeneration()
{
//initialise some values
int sType = 0;
int habMin = 0;
int habMax = 0;
// generate a random number
srand ( time(NULL) );
int randNum = rand();
int starType = randNum % 5;
// according to type, define star stats.
if (starType == 0)
{ sType = 'A'; habMin = 16; habMax = 20; }
else if (starType == 1)
{ sType = 'F'; habMin = 12; habMax = 15; }
else if (starType == 2)
{ sType = 'G'; habMin = 7; habMax = 12; }
else if (starType == 3)
{ sType = 'K'; habMin = 5; habMax = 6; }
else if (starType == 4)
{ sType = 'M'; habMin = 2; habMax = 4; }
//pass our happy values to "Star"
Star = { sType,habMin,habMax };
//"Star" is what we return
return Star;
}
Code: Select all
//it prints off the values of the parameters that define our "Star"
void starPrint( Star )
{
//get info from "Star" structure ( accessing it using "StructureName.StructureParameter" )
cout << "Star type is: " << Star.sType <<endl;
cout << "Star habMin is: " << Star.habMin <<endl;
cout << "Star habMax is: " << Star.habMax <<endl;
}
Re: pass values between functions?
I got it, I think. It seems though now that my random number generator makes the same number multiple times, I ran it like 10 times and it came up with the same number multiple times in a row. Might be coincidence but idk.
struct defined, and instance created, no prob
starGen function. I couldn't pass the happy values the same way you did. Did some looking around and had found out I had to do it with a .(period) with each value.
Finally the printStar, works like a champ, the console displays all the values
One more thing to note, I was unable to have the variables in the struct be named the same as those in the starGen function. Maybe I could have and overlooked something, not sure, but either way, you definitely helped me big time I appreciate it.
Just got so I can get stats between multiple functions, woot, now I can continue along with this, hopefully soon I will have a fully working star system generator.
struct defined, and instance created, no prob
Code: Select all
struct stats
{
int min;
int max;
char type;
};
stats Star;
starGen function. I couldn't pass the happy values the same way you did. Did some looking around and had found out I had to do it with a .(period) with each value.
Code: Select all
stats starGen()
{
int randNum;
srand ( time(NULL) );
randNum = rand();
int starType = randNum % 5;
char sType;
int habMin;
int habMax;
// Check for the type of star that was randomely picked
if (starType == 0)
{ sType = 'A'; habMin = 16; habMax = 20; }
else if (starType == 1)
{ sType = 'F'; habMin = 12; habMax = 15; }
else if (starType == 2)
{ sType = 'G'; habMin = 7; habMax = 12; }
else if (starType == 3)
{ sType = 'K'; habMin = 5; habMax = 6; }
else if (starType == 4)
{ sType = 'M'; habMin = 2; habMax = 4; }
Star.min = habMin;
Star.max = habMax;
Star.type = sType;
return Star;
}
Code: Select all
void printStar( stats )
{
cout << "\nStar type is " << Star.type << endl;
cout << "habMin is " << Star.min << endl;
cout << "havMax is " << Star.max << endl;
}
One more thing to note, I was unable to have the variables in the struct be named the same as those in the starGen function. Maybe I could have and overlooked something, not sure, but either way, you definitely helped me big time I appreciate it.
Just got so I can get stats between multiple functions, woot, now I can continue along with this, hopefully soon I will have a fully working star system generator.
Re: pass values between functions?
Thats actually easily solvable with oop. You have two classes. Star and Planet. In the Planet class create a constructor taking those 3 parameters from star that you need. In the main method You create an Object of type star and after that using this object you can access its private components by using accessor and pass them to the constructor of the Planet.
Tham do the random stuff in the main method
Ofcourse as written above you'll need to define and implement a constructor that takes those 3 parameters in the Planet class.
Code: Select all
class Star
{
char sType;
int habMin;
int habMax;
Star( int type)
{
switch(type)
case 0: sType = 'A'; habMin = 16; habMax = 20; break;
case 1: sType = 'F'; habMin = 12; habMax = 15; break;
.
.
.
}
~Star(){}
char getSType(){ return sType; }
int getHabMin(){ return habMin; }
int getHabMax(){ return habMax; }
};
Code: Select all
int randNum;
srand ( time(NULL) );
randNum = rand();
int starType = randNum % 5;
Star s = new Star (starType);
Planet p = new Planet (s.getHabMin(), s.getHabMax(), s.getgetSType());
Re: pass values between functions?
Just to note. You only need to seed the random number generator once at the start of the program, not before every rand() call. This would be why you got the same number ten times.
I mean, you can seed more than once if you want but if the srand() calls are done at the same time almost instantaneously, then they will receive the same time and rand() will generate the same number.
I mean, you can seed more than once if you want but if the srand() calls are done at the same time almost instantaneously, then they will receive the same time and rand() will generate the same number.
Musi
There are 10 types of people that understand binary.
Those that do, and those that don't.
There are 10 types of people that understand binary.
Those that do, and those that don't.
Re: pass values between functions?
indus wrote:Thats actually easily solvable with oop. You have two classes. Star and Planet. In the Planet class create a constructor taking those 3 parameters from star that you need. In the main method You create an Object of type star and after that using this object you can access its private components by using accessor and pass them to the constructor of the Planet.Tham do the random stuff in the main methodCode: Select all
class Star { char sType; int habMin; int habMax; Star( int type) { switch(type) case 0: sType = 'A'; habMin = 16; habMax = 20; break; case 1: sType = 'F'; habMin = 12; habMax = 15; break; . . . } ~Star(){} char getSType(){ return sType; } int getHabMin(){ return habMin; } int getHabMax(){ return habMax; } };
Ofcourse as written above you'll need to define and implement a constructor that takes those 3 parameters in the Planet class.Code: Select all
int randNum; srand ( time(NULL) ); randNum = rand(); int starType = randNum % 5; Star s = new Star (starType); Planet p = new Planet (s.getHabMin(), s.getHabMax(), s.getgetSType());
But how do I go about creating a number of objects of the planet class equal to a random number (the random number of planets)
I tried a for loop but get "expression must have a constant value" for planArray
Code: Select all
for ( int i = 0; i < planetNum - 1; i++ )
{
planet planArray[i];
/* insert code to add stats to each instantiation
of the planet object along with values from star
generator function */
}
And thanks Musi I didn't think of that. Originally I had a ranNum() function but it did the same thing with giving the same values so I just put it all in each other function. I'll go ahead seed it just once at the beginning.