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