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;
}
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!
//Use this application to determine how old the user is, based on what year they where born
#include <iostream>
#include <string>
usingnamespace 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();
}
> 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";