C ++ is not C and this applies to a few features:
listed
It is not necessary to use typedef
... do not mess up the code for free:
enum tElemento {Piedra,Papel,Tijera}; // C++
conversions
C ++ has 4 different types of conversions. The great advantage of these conversions is that they are much safer than C conversions and can also be easily located in the text using the search tools:
eleccion= tElemento(numero); // C
eleccion = static_cast<tElemento>(numero); // C++
Variables at the beginning
Neither in C (at least from the C99 standard (dating from 1999 EYE !!!) or in C ++ (since time immemorial) it is necessary to declare the variables at the beginning of the function.
The variables can be declared at the most convenient time, and the best thing is to shorten your life as much as possible ...
tElemento eleccionHumano()
{
// tElemento eleccion; <--- MAL
int numero;
cout <<endl<<"Elige un elemento <1-piedra, 2-papel, 3-tijera>: ";
cin >> numero;
// vvvvvvvvvvv Esto ya lo hemos explicado en el punto anterior
tElemento eleccion= static_cast<tElemento>(numero);
// ^^^^^^ Mejor aqui
}
And now other details applicable to both C and C ++:
Returns
If a function has a return type you have to use necessarily at least a return
. Think that the function is going to return a value yes or yes and if you do not choose it, the program will do it for you ... Do you think that it will succeed and will return what you expect? Bets are accepted ...
tElemento eleccionHumano()
{
int numero;
cout <<endl<<"Elige un elemento <1-piedra, 2-papel, 3-tijera>: ";
cin >> numero;
tElemento eleccion = static_cast<tElemento>(numero);
return eleccion; // <<--- FALTABA return
}
string elementoAstring(tElemento elemento)
{
string ElementoHumano;
if (elemento == 1)
{
ElementoHumano = "Piedra";
}
else if (elemento == 2)
{
ElementoHumano = "Papel";
}else if (elemento ==3)
{
ElementoHumano = "Tijera";
}
cout << "Ha elegido " << ElementoHumano;
return ElementoHumano; // <<--- FALTABA return
}
Be careful with the statements
int main()
{
tElemento eleccionHumano();
string elementoAstring(tElemento elemento);
}
In those few lines of the main ...
-
tElemento eleccionHumano();
is a declaration of a function called eleccionHumano
that does not receive parameters and that returns an object of type tElemento
.
-
string elementoAstring(tElemento elemento);
is another declaration of another function ...
try this:
int main()
{
tElemento elemento = eleccionHumano();
elementoAstring(elemento);
}