Super simple functions question
Jul 13, 2013 at 1:32am UTC
I'm new to using functions and all I'm trying to do is get the user age using a function called getAge. The problem is that it keeps saying "error the identifier userAge is undefined"
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
#include<iostream>
#include<iomanip>
using namespace std;
int getAge();
int main()
{
int userAge;
userAge = getAge();
cout << userAge << endl;
}
// Function definition
int getAge()
{
do
{
cout << "Enter age:" ;
cin >> userAge;
} while (userAge <= 0);
return userAge;
}
Jul 13, 2013 at 1:38am UTC
You don't have a variable called userAge inside your function. Variables do not transfer across functions unless you specifically pass them.
Jul 13, 2013 at 1:50am UTC
It is because you are trying to pass a function with no arguments in it. Say if you did
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
#include<iostream>
#include<iomanip>
using namespace std;
int getAge(int userAge); //<--- note the argument pass
int main()
{
int userAge = 0;
userAge = getAge(userage); //<--- pass the int argument
cout << userAge << endl;
}
// Function definition
int getAge(int userage)
{
do
{
cout << "Enter age:" ;
cin >> userAge;
} while (userAge <= 0);
return userAge;
}
Last edited on Jul 13, 2013 at 1:51am UTC
Jul 13, 2013 at 2:14am UTC
That was it, thank you =)
Topic archived. No new replies allowed.