Problem with passing values. Program ends unexpectedly

Alright, so for school I have this program where my instructor wants us to use two functions.
The first one is to ask how old your dad is, and the second one is to display that age.
For some reason, when I compile and run it, it asks you to input your father's age, but then, without doing anything else, unexpectedly terminates.

Also, in the function DisplayFatherAge() when I try to output the age to the screen, I type

std::cout << "Your father's age is " << FatherAge(age);

This, for some reason, asks for the age twice, but never displays it. Why?

I don't want an answer to my assignment as I would like to figure this out on my own, but the way our textbook explains passing values, this should work correctly.

So, any help would be appreciated!

Here's my code:

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
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <conio.h>
#include <math.h>

const int NotUsed = system( "color 0a" );  /*change text color to light green
                                             and BG to black*/

int FatherAge(int age);           //int function to get father's age & return it
void DisplayFatherAge(int age2);  //function to display father's age


int main()
{
    int age, age3;  //declare variables to pass
    
    FatherAge(age);   //call function asking father's age

	std::cout << '\n';  //create a blank to make output look nicer

    DisplayFatherAge(age3);  //call function that prints father's age to screen

	return 0;
}

/*-------------------------------------------------------*/
/* Function to ask for father's age, and store it in 
   variable age3. Then return the variable to main().    */
/*-------------------------------------------------------*/

int FatherAge(int age3)
{
        std::cout << "What is your father's age? ";
	std::cin >> age3;

	return(age3);
}

/*-------------------------------------------------------*/
/* Function to pass value of age3 to age2, and print 
   father's age to screen.*/
/*-------------------------------------------------------*/

void DisplayFatherAge(int age2)
{
	std::cout << "Your father's age is: " 
                  << age2 << '\n';
}
Last edited on
Thank you helios, but that tutorial does not help me in understanding how to pass variables.
It tells about the scope, which is just whether or not a variable is global or local, but I need to know how to pass the values from one function to the next without error.
Or, more precisely, without unexpected termination when the compiler says there are no errors or warnings.

Thanks
Well you are never using the value that is returned from FatherAge(). When a function returns a value, think of it like the function becomes that value in the original call statement. So in order to get your variable age to become the value returned by your function, you need to use the assignment operator =. So it would look like

age = FatherAge();

So now, FatherAge() doesn't need a parameter, you can create a variable to hold the age locally in the function and return that. Of course you could always pass the variable by reference to avoid having to write the assignment statement, but with an int there isn't much point since the overhead required in copying it will be negligible.
Now, your program is failing because you are trying to print an uninitialized variable to cout, that variable being age3. This variable is completely unnecessary, because the variable age is the one that is holding your data that you want printed. So you should be getting rid of age3 and instead be calling

DisplayFatherAge( age );
Last edited on
Hi spoodlz!

Your main problem is how you name those variables.
For example:
int age, age3; int FatherAge(int age3) void DisplayFatherAge(int age2)
Why is the name "age3" used both as a variable name and as a input variable name in the function?
It's confusing.And why does the secont function, DisplayFatherAge, have an input variable named age2?Why not age3 just like the first function??
The way you named them really creates confusion.
Where you afraid that name clashes would occur??They never do if they are names for parameters of that function.Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

bool is_it_minor(int age)
{
   if ( age<18)
      return 1;
   else return 0;
}

int main()
{
   int age=15;
   
   if ( is_it_minor(age) )
      cout<<"Is minor";
   else cout<<"Is major";

   return 0;
}

The variable int age has nothing to do with the parameter int age from the function.Even if they are named the same, the name only matters outside functions, in case of variables.The name of function parameters is only so you know wich parameter you are accesing inside the function.
For example:
1
2
3
4
5
6
int max_number( int, int )
{
   if (int < int )
      return int;
   else return int;
}

The code above obviously is stupid and doesn't even compile, but the idea is that there is no way of knowing wich parameter is wich, because they have no manes.But if you do:
1
2
3
4
5
6
int max_number( int number1, int number2)
{
   if (number1 < number2 )
      return number2;
   else return number1;
}

Now you know exactly which parameter you are using.

Anyway, the names of variables and the names of parameters do not clash with each other, so you can use the same name if you want.Even for more functions.
Here's an example of your program:
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
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <conio.h>
#include <math.h>

const int NotUsed = system( "color 0a" );  /*change text color to light green
                                             and BG to black*/
/*-------------------------------------------------------*/
/* Function to ask for father's age, and store it in 
   variable age3. Then return the variable to main().    */
/*-------------------------------------------------------*/

int FatherAge(int age)
{
        std::cout << "What is your father's age? ";
	std::cin >> age;

	return(age);
}

/*-------------------------------------------------------*/
/* Function to pass value of age3 to age2, and print 
   father's age to screen.*/
/*-------------------------------------------------------*/

void DisplayFatherAge(int age)
{
	std::cout << "Your father's age is: " 
                  << age << '\n';
}


int main()
{
    int age, age2;  //declare variables to pass
    
    FatherAge(age);   //call function asking father's age

	std::cout << '\n';  //create a blank to make output look nicer

    DisplayFatherAge(age);  //call function that prints father's age to screen

	return 0;
}

Of course the program still haves the issues reported in the previous answers , but as you can see there is no problem in using the same name.

What about global variables???
There you might have a problem.For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int age = 15; // a global variable

bool can_vote(int age) // a function
{
   if (age >= 18) // oops!!! wich age?? age the global variable or age the parameter ??
      return 1;
   else return 0;
}

int main()
{
   if( can_vote(age) )  // no problem here
      cout<<"The person can vote";
   else cout<<"The person cannot vote";

   return 0;
}

That's why a lot of people say "don't use global variables".
Anyway, try to give clear names to both variables and functions, and yes, "don't use global variables" :) unless you are completely shore there name doesn't conflict whit parameter's names, for the functions that you use.
Good luck!
Last edited on
The function that you use to get the age returns an int but that value doesn't get received by an int instead its just left hanging in memory.

To fix this is very easy just add;
 
age = functionName(variable);
Topic archived. No new replies allowed.