pass values between functions?

The Partridge Family were neither partridges nor a family. Discuss.
Damus
Posts: 7
Joined: January 19th, 2013, 6:43 pm

pass values between functions?

Post by Damus » January 20th, 2013, 1:14 pm

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.

Clodi
Posts: 175
Joined: November 18th, 2012, 7:47 pm

Re: pass values between functions?

Post by Clodi » January 20th, 2013, 2:54 pm

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

Code: Select all

struct Planet
{
	int mass;
	int number;
	int O2level;
	string name;
};
2) then create an instance

Code: Select all

Planet Charlie;
3) make your function

Code: Select all

Planet UpdatePlanet(....)
{
	.....
	.....
	.....
	return Charlie;
}

Damus
Posts: 7
Joined: January 19th, 2013, 6:43 pm

Re: pass values between functions?

Post by Damus » January 20th, 2013, 5:25 pm

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.

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;	
}		
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

Code: Select all

class planet
{
	int distance;
	int size;
	int type;
	bool hZone;

	void atmosphere();
}

Thanks

Clodi
Posts: 175
Joined: November 18th, 2012, 7:47 pm

Re: pass values between functions?

Post by Clodi » January 20th, 2013, 7:21 pm

I am sorry, I am not sure I understand you logic.

you say
to get the values for habMin, habMax, and sType passed to a planetGen() function
and that's fine. Your planetGen() functon is currently taking those parameters as input

then you say
function, in which I want to use an array and a for loop to instantiate the necessary number of objects of the planet type
i don't see any of these features in your function right now.
Also,

Code: Select all

if (starType == 0)
      {
         sType = 'A';
         habMin = 16;
         habMax = 20;
      }
      else
      if (starType == 1)
      {
         sType = 'F';   
         habMin = 12;
         habMax = 15;
      }
      else ....
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 :)

Clodi
Posts: 175
Joined: November 18th, 2012, 7:47 pm

Re: pass values between functions?

Post by Clodi » January 20th, 2013, 7:25 pm

I think I understand what you want to do, :D wait a sec

Clodi
Posts: 175
Joined: November 18th, 2012, 7:47 pm

Re: pass values between functions?

Post by Clodi » January 20th, 2013, 7:52 pm

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

Code: Select all

//"stats." structure stores 2 values
struct stats
{
	int sType;
	int habMin;
	int habMax;
};
make an instance

Code: Select all

//one "stats." created
stats Star;
design your function
//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;
}
then, do what you want with your values :)

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;
}

Damus
Posts: 7
Joined: January 19th, 2013, 6:43 pm

Re: pass values between functions?

Post by Damus » January 21st, 2013, 3:09 am

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

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;
}		
Finally the printStar, works like a champ, the console displays all the values

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.

indus
Posts: 35
Joined: November 7th, 2012, 12:35 am

Re: pass values between functions?

Post by indus » January 21st, 2013, 5:54 am

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.

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; }
};
Tham do the random stuff in the main method

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());
Ofcourse as written above you'll need to define and implement a constructor that takes those 3 parameters in the Planet class.

Musi
Posts: 106
Joined: November 25th, 2012, 1:06 am

Re: pass values between functions?

Post by Musi » January 21st, 2013, 4:51 pm

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.
Musi

There are 10 types of people that understand binary.
Those that do, and those that don't.

Damus
Posts: 7
Joined: January 19th, 2013, 6:43 pm

Re: pass values between functions?

Post by Damus » January 22nd, 2013, 5:55 am

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.

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; }
};
Tham do the random stuff in the main method

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());
Ofcourse as written above you'll need to define and implement a constructor that takes those 3 parameters in the Planet class.

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 */
		}
Gotta read more about constructors I'll be doing that now.

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.

Post Reply