int function return int and print to screen?

Hello c++.com. I am very new to programming and c++.

As an exercise I am trying to write a simple program that asks the user for their birth year then calculates their age. However I am running into an issue with my code structure. My idea is I declare an int function, and then print that out. I know there are many alternatives to this approach but I am curious what is wrong with this method. When I cout my int birthyear, I get a random long number. Thanks in advance!

#include <iostream>
using namespace std;

int birthyear(); //declaring int function

int main()
{
int birthyear();
cout << birthyear
return 0;
}

int birthyear(); // function definition
{
int year;
cout << "What Year where you born" << endl;
cin >> year;
return year;
}


To call a function you use parenthesis
cout << birthyear();
Thanks so much!
Hello,

I am back again.

I have written out the rest of the code, however there is one problem I am struggling with. When I cout my int, it seems to re-run thats integers entire code and not the number I am expecting. Thanks so much for your help!

Here is my code below.

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
//Use this application to determine how old the user is, based on what year they where born


#include <iostream>
#include <string>


using namespace std;


void intro();
int UserBirthYear();
int UserAge();


string name = "";


int main()
{
  intro();
  UserBirthYear();
  UserAge();
  cout << UserAge(); // Seems to re-run UserBirthYear() and not print out the sum of CurrentYear - UserBirthYear()
  return 0;
}


void intro()
{
  cout << "Hello, please tell me your name" << endl;
  cin >> name;
  cout << "Hello " << name << endl;
  return;
}


int UserBirthYear()
{
  int birthyear;
  cout << name << ", Please tell me what year you wear Born." << endl;
  cin >> birthyear;
  return birthyear;
}


int UserAge()
{
  int CurrentYear = 2016;
  return CurrentYear - UserBirthYear();  
}

Line 22: You call UserBirthYear(), but you don't store the result anywhere.

Line 23: You call UserAge(), which calls UserBirthYear() a second time. Again, you don't store the result anywhere.

Line 24: You call UserAge() again, which calls UserBirthyear a third time.

You main should look more like this:
1
2
3
4
5
6
7
8
9
int main()
{
	int year, age;
	intro();
	year = UserBirthYear();
	age = UserAge(year);
	cout << age << endl; 
	return 0;
}

Last edited on
> When I cout my int function, it seems to re-run thats integers function's entire code
yeah, that's supposed to happen.
You issue an order, the order is carry out.
If you do
1
2
cout << 42 << ' ';
cout << 42 << ' ';
the expected output will be
42 42 
¿right?


You may capture the return value of a function, and then use it later
1
2
3
4
5
6
int age = UserAge();
cout << "The age is " << age << '\n';
if(age>124)
	cout << "Congratulations\n";
else
	cout << "There's still a long way to go\n";
Topic archived. No new replies allowed.