Function always evaluating as true

I'm trying to write a code so that when someone puts in their name, age, and gender, it'll read that info back to them. When I try to compile this code, it tells me that the function always evaluates as true.

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
#include <iostream>
#include <string>
using namespace std;

void age()
{
    cout << "Please enter your age: ";
    string age;
    cin >> age;
}

void gender()
{
    cout << "Please enter your gender: ";
    string gender;
    cin >> gender;
}

void name()
{
    //ask for the person's name
    cout << "Please enter your first name: ";

    //read the name
    string name; //define name
    cin >> name;
}

int main()
{
    cout << "Hello!" << endl;
    name();
    age();
    gender();
    cout << "Your name is: " << name << "." << endl;
    cout << "Your age is: " << age << "." << endl;
    cout << "Your gender is: " << gender << "." << endl;
    return 0;
}



||=== Practice, Debug ===|
E:\C++\Practice\main.cpp||In function 'int main()':|
E:\C++\Practice\main.cpp|35|warning: the address of 'void name()' will always evaluate as 'true'|
E:\C++\Practice\main.cpp|36|warning: the address of 'void age()' will always evaluate as 'true'|
E:\C++\Practice\main.cpp|37|warning: the address of 'void gender()' will always evaluate as 'true'|
||=== Build finished: 0 errors, 3 warnings ===|
the variables in your functions are local variables, and they're gone once your functions end.

also you don't have the variables name, age, and gender declared in your main function. My main guess is, your compiler thinks those are the functions you are trying to call but without the (), since your function names are exactly the same as your variable names as well.
Last edited on
This is probably better for you :

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
#include <iostream>
#include <string>
using namespace std;

//Global user input data types
string userAge, userName, userGender;

void age()
{
    cout << "Please enter your age: ";
    //string age; <---- same identifier name as function is bad
    //cin >> age;
    cin >> userAge;
}

void gender()
{
    cout << "Please enter your gender: ";
    //string gender; <---- same identifier name as function is bad
    //cin >> gender;
    cin >> userGender;
}

void name()
{
    //ask for the person's name
    cout << "Please enter your first name: ";

    //read the name
    //string user; <---- same identifier name as function is bad
    //cin >> user;
    getline (cin, userName); // If your asking for the persons full name getline is better since it reads after spaces
}

int main()
{
    cout << "Hello!" << endl;
    name();
    age();
    gender();
    cout << "Your name is: " << userName << "." << endl;
    cout << "Your age is: " << userAge << "." << endl;
    cout << "Your gender is: " << userGender << "." << endl;
    return 0;
}


The compiler is just giving you warnings because it states that the functions : name, age, and gender will always come out true. Since it is a void you can check for invalid input if a user inputs char's for the age input. And age should probably be unsigned short int instead of string.
Last edited on
Thank you very much. Now I know not to have identifiers the same as functions. I'm still pretty much a beginner when it comes to coding.
Global variables will work for you here, but when you start getting into bigger projects the global variables will make everything much more complicated. Instead of void functions and global variables, I'd recommend string functions with local variables. It is just better coding practice.
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
#include <iostream>
#include <string>
using namespace std;

string get_age() // Change function type
{
    cout << "Please enter your age: ";
    string age;
    cin >> age;
    return age;  // return the string
}

string get_gender() //function type
{
    cout << "Please enter your gender: ";
    string gender;
    cin >> gender;
    return gender; //return value
}

string get_name()
{
    //ask for the person's name
    cout << "Please enter your first name: ";

    //read the name
    string name; //define name
    cin >> name;
    return name;
}

int main()
{
    cout << "Hello!" << endl;
    string name = get_name();
    string age = get_age();
    string gender = get_gender();

    cout << "Your name is: " << name << "." << endl;
    cout << "Your age is: " << age << "." << endl;
    cout << "Your gender is: " << gender << "." << endl;
    return 0;
}


Another tip: Function names are the clearest when they have a verb associated with them. This tells us what they do.
Last edited on
also if you want to be able to use spaces you need getline
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 <cstdlib>
#include <iostream>
#include <string>

using namespace std;
string name, gender;
int age;

int main()
{
    cout << "Hello!" << endl;
    cout << "Name: "; 
    cin >> name;
    cout << "Age: ";
    cin >> age;
    cout << "Gender: ";
    cin >> gender;
    
    cout << "Your name is: " << name << endl;
    cout << "Your age is: " << age << endl;
    cout << "Your gender is: " << gender << endl;
    cin.ignore();
    
    
    cin.ignore();
    return EXIT_SUCCESS;
}


Why not do it in much less code? Unless you're trying to work with functions or you will be using the variables more then once, I guess. Just trying to simplify things for you. I also believe this allocates much less memory as opposed to calling a new function every single time.
Nathaniel Sheller you are not using function over here . but in big project there is always a use of the function. also you should use the getline() to include spaces in the string .
Well yes, but he never specified if he needed functions for it. If he needs functions then use the previous methods, but if he just wants it to be a simple program to do just this then my code works just as well. lol.

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 <cstdlib>
#include <iostream>
#include <string>

using namespace std;
string name, gender;
int age;

int main()
{
    cout << "Hello!" << endl;
    cout << "Name: "; 
    getline(cin, name);
    cout << "Age: ";
    cin >> age;
    cout << "Gender: ";
    cin >> gender;
    
    cout << "Your name is: " << name << endl;
    cout << "Your age is: " << age << endl;
    cout << "Your gender is: " << gender << endl;
    cin.ignore();
    
    
    cin.ignore();
    return EXIT_SUCCESS;
}


There. Added getline instead of what I had been doing.
Last edited on
Nathaniel Sheller you are right .
Topic archived. No new replies allowed.