using a function to print basic student information in c++
Because 0 at the beginning of an integer literal means that it should be interpreted as an octal value. Now you don't want that. So convert phone
and age
to std::string
.
Also, Use a struct
to make the work easier (instead of many many parameters):
#include <iostream>
#include <string>
struct Generalities
{
std::string fname { std::string( "Unknown" ) }; // use in-class initializers
std::string lname { std::string( "Unknown" ) }; // to prevent any problems
std::string email { std::string( "Unknown" ) }; // in case default initialization
std::string phone { std::string( "Unknown" ) }; // happens
std::string age { std::string( "Unknown" ) };
};
void printGeneralities( const Generalities& generalities )
{
std::cout << "First Name: " << generalities.fname << " --- Last Name: " << generalities.lname
<< " --- Email: " << generalities.email << " --- Phone: " << generalities.phone
<< " --- Age: " << generalities.age << '\n';
}
int main( )
{
std::cout << "Team generalities\n";
printGeneralities( { "Ledjet", "Lushka", "[email protected]", "0694339874", "20" } );
printGeneralities( { "Enio", "Sefa", "[email protected]", "0699274494", "20" } );
Generalities generalities; // empty, i.e. non of the members have real values
printGeneralities( generalities );
}
Output:
Team generalities
First Name: Ledjet --- Last Name: Lushka --- Email: [email protected] --- Phone: 0694339874 --- Age: 20
First Name: Enio --- Last Name: Sefa --- Email: [email protected] --- Phone: 0699274494 --- Age: 20
First Name: Unknown --- Last Name: Unknown --- Email: Unknown --- Phone: Unknown --- Age: Unknown
The answer @digito_evo gave was a nice improvement. I would go further and overload the operator<< for the struct.
#include <iostream>
#include <string>
struct Generalities
{
std::string fname;
std::string lname;
std::string email;
std::string phone;
std::string age;
friend std::ostream& operator<<(std::ostream& os, const Generalities& g);
};
std::ostream& operator<<(std::ostream& os, const Generalities& g)
{
os << g.fname << ' ' << g.lname << " email: " << g.email << " Phone: "<< g.phone << " Age: " << g.age << '\n';
return os;
}
int main( )
{
std::cout << "Team generalities\n";
std::cout << Generalities{ "Ledjet", "Lushka", "[email protected]", "0694339874", "20" };
return 0;
}