Cant get the function to work right

Hey, what's up fellas?
I'm new to the C++ or programming in general. I'm trying to get a separate function to calculate and then go back to the first one and read out the output for every year that was entered. It's still in a rough state and I know somethings aren't that great.
Anywho here's the code. The first one is the one I need to get the function right and the second one is what I want the first one to do, but the calculations in another function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
double new_pop(double, double, double, double, double, double, double, int);

int main()
{

	double start_pop = 0, annual_birth_rate = 0, annual_death_rate = 0, number_of_years = 0,
		new_population = 0, current_pop = 0, annual_birth_rate2 = 0,
		annual_death_rate2, annual_birth_rate3, population_growth, population;
	int A;


	cout << "What's the starting size of the population? (Minimum of 2) " << endl;
	cin >> start_pop;


	if (start_pop >= 2 && start_pop <= 10000);
	else if (cout << "Are you some type of self reproducing creature? If not, try again." << endl)
		return 0;


	cout << "What's the annual birth rate? (Enter 1-1000)" << endl;
	cin >> annual_birth_rate;


	if (annual_birth_rate >= 1 && annual_birth_rate <= 1000);
	else if (cout << "Isn't anyone procreating?? If so, try again." << endl)
		return 0;


	annual_birth_rate2 = annual_birth_rate / 1000;


	cout << "What's the annual death rate? (Enter 1-1000)" << endl;
	cin >> annual_death_rate;


	if (annual_death_rate >= 1 && start_pop <= 1000);
	else if (cout << "Is your society immortal?? If not, try again." << endl)
		return 0;


	annual_death_rate2 = annual_death_rate / 1000;

	current_pop = annual_birth_rate - annual_death_rate + start_pop;

	cout << "Your current population is " << current_pop << " in our society." << endl;

	cout << "Enter the amount of years you'd like to see the population growth. " << endl;
	cin >> number_of_years;



	cout << "The estimated growth in " << A << " years is " << annual_birth_rate3 << "." << endl;

	return 0;
}
double new_pop(double population_growth, double annual_birth_rate, double annual_death_rate,double population, double current_pop, double number_of_years, double annual_birth_rate3, int A)
{
	population_growth = annual_birth_rate - annual_death_rate;
	population = current_pop + population_growth;

	for (A = 1; A <= number_of_years; ++A)

	{
		annual_birth_rate3 = A * population_growth + current_pop;

	}

	return main();
}

Second
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;

int main()
{

	double start_pop = 0, annual_birth_rate = 0, annual_death_rate = 0, number_of_years = 0,
		new_population = 0, current_pop = 0, annual_birth_rate2 = 0,
		annual_death_rate2, annual_birth_rate3;
	int A;

	cout << "What's the starting size of the population? (Minimum of 2) " << endl;
	cin >> start_pop;

	if (start_pop >= 2 && start_pop <= 10000);
	else if (cout << "Are you some type of self reproducing creature? If not, try again." << endl)
		return 0;

	cout << "What's the annual birth rate? (Enter 1-1000)" << endl;
	cin >> annual_birth_rate;

	if (annual_birth_rate >= 1 && annual_birth_rate <= 1000);
	else if (cout << "Isn't anyone procreating?? If so, try again." << endl)
		return 0;

	annual_birth_rate2 = annual_birth_rate / 1000;

	cout << "What's the annual death rate? (Enter 1-1000)" << endl;
	cin >> annual_death_rate;

	if (annual_death_rate >= 1 && start_pop <= 1000);
	else if (cout << "Is your society immortal?? If not, try again." << endl)
		return 0;

	annual_death_rate2 = annual_death_rate / 1000;

	current_pop = annual_birth_rate - annual_death_rate + start_pop;

	cout << "Your current population is " << current_pop << " in our society." << endl;

	cout << "Enter the amount of years you'd like to see the population growth. " << endl;
	cin >> number_of_years;



	double population_growth, population;

	population_growth = annual_birth_rate - annual_death_rate;
	population = current_pop + population_growth;

	for (A = 1; A <= number_of_years; ++A)

	{
		annual_birth_rate3 = A * population_growth + current_pop;

		cout << "The estimated growth in " << A << " years is " << annual_birth_rate3 << "." << endl;
	}


	return 0;
}






dcchris311 wrote:
71
72
73
74
	}

	return main();
}
You have a misconception about return statements. A return statement takes the value to be returned to the calling function, it does not take the function you want to return to - the code already knows what function it need to return to. In this case, you are calling main again (which is invalid to do anyway, since main is a special function), and thus you are getting infinite recursion.

EDIT: I don't even see where you are calling new_pop in main?
Last edited on
closed account (iAk3T05o)
You are putting 'cout in an else if statement.
Oh, that made me notice that you have semicolons after your if statements on lines 17, 24, and 33. Maybe you should reread the tutorial on control structures?
http://www.cplusplus.com/doc/tutorial/control/
closed account (j3Rz8vqX)
Call the function on line 55 since you've exported the data into the function.

It would look like the function's header, without the return and removing the data types before each variable.
 
double new_pop(double population_growth, double annual_birth_rate, double annual_death_rate, double population, double current_pop, double number_of_years, double annual_birth_rate3, int A)

Also..
 
return main();

Is an incorrect way to use returned data - it isn't the same as jumping in assembly. An appropriate return function may look like the below:
 
return A;//returns the value of variable A to the calling procedure. 

Since no real data is necessarily return, you should have a void function and not return anything.

If anything, the function should return A as integer, since you're using it on line 57 - otherwise pass it as reference, so its data is maintained.

When the function completes it process, it will return to where the function was called - where it last was in the main procedure.

Information on functions
http://www.cplusplus.com/doc/tutorial/functions/

Information on pass_by_reference: (optional - if you're VERY new, just return int, since you've only got 1 significant data in the function)
http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/

Although it may sound criticizing, no negative intentions were meant from my post.

Have fun.
Thanks for the help and the links to the if else statements! Hopefully i get better at it. I think I fixed my statements, but I haven't gotten the calling to the functions yet. How does it look so far???
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;

double new_pop(double, double, double, double, double, double, int);

int main()
{

	double start_pop = 0, annual_birth_rate = 0, annual_death_rate = 0, number_of_years = 0,
		new_population = 0, current_pop = 0, annual_birth_rate2 = 0, annual_death_rate2;

	cout << "What's the starting size of the population? (Minimum of 2) " << endl;
	cin >> start_pop;

	if (start_pop >= 2)
	
		cout << "You entered " << start_pop << " for the size of the population" << endl;
	
	else if(start_pop <= 1)
	
		cout << "Are you some type of self reproducing creature? If not, try again." << endl;
	

	cout << "What's the annual birth rate? (Enter 1-1000)" << endl;
	cin >> annual_birth_rate;

	if (annual_birth_rate >= 1 && annual_birth_rate <= 1000)
	
		cout << "You entered " << annual_birth_rate << " as the birth rate." << endl;
	
	else if (annual_birth_rate <= 1 && annual_birth_rate >= 1001)
	
		cout << "Isn't anyone procreating?? If so, try again." << endl;
	
	annual_birth_rate2 = annual_birth_rate / 1000;

	cout << "What's the annual death rate? (Enter 1-1000)" << endl;
	cin >> annual_death_rate;

	if (annual_death_rate >= 1 && start_pop <= 1000)
	
		cout << "You've entered " << annual_death_rate << " as the death rate." << endl;
	
	else if (annual_death_rate <= 1 && annual_death_rate >= 1001)
	
		cout << "Is your society immortal?? If not, try again." << endl;
	
	annual_death_rate2 = annual_death_rate / 1000;

	current_pop = annual_birth_rate - annual_death_rate + start_pop;

	cout << "Your current population is " << current_pop << " in our society." << endl;

	cout << "Enter the amount of years you'd like to see the population growth. " << endl;
	cin >> number_of_years;



	//cout << "The estimated growth in " << A << " years is " << new_pop << "." << endl;


	return 0;
}
Last edited on
How do I get it to stop running if its wrong can I do this???
1
2
3
4
5
6
7
8
9
	if (start_pop >= 2)
	
		cout << "You entered " << start_pop << " for the size of the population" << endl;
	
	else if (start_pop <= 1)
	{
		cout << "Are you some type of self reproducing creature? If not, try again." << endl;
		return 0;
	}
Last edited on
"else if" is invalid in my option, this only works in Java or a few programming languages.
closed account (j3Rz8vqX)
Else followed by if, is legal.
However:

If you want else-if to have brackets, your if must also have brackets.
1
2
3
4
5
6
7
8
9
    if (start_pop >= 2)
    {
        cout << "You entered " << start_pop << " for the size of the population" << endl;
    }
    else if(start_pop <= 1)
    {
        cout << "Are you some type of self reproducing creature? If not, try again." << endl;
        return 0;
    }

Or you'd want them on separate lines; if having one statement and else having one statement - an if.
1
2
3
4
5
6
    else
        if(start_pop <= 1)
        {
            cout << "Are you some type of self reproducing creature? If not, try again." << endl;
            return 0;
        }


Thought I'd give my input since it's taking a bit to resolve this.
Have fun.
Topic archived. No new replies allowed.