my code compiles and executes but it does not show anything at display
This code I wrote it myself but I do not get the expected output, just a message saying press enter to continue, the code is about emulating parts of an ATM.
what I expect the program to do is the constructor of the class "Cuenta1()" to automatically call and execute the class function "establecerValores()", so that establecerValores() can automatically call and execute pedirContrasenia(), so that pedirContrasenia() can call and execute mostrarMensaje() just by declaring
"Cuenta1 CuentaLuis();" within main.
Code:
//****************( "Cuenta.h " )****************
#include<string>
using namespace std;
class Cuenta1{
public:
Cuenta1();
void establecerValores( string, int, int );
string obtenerUsuario( );
int obtenerEdad();
int obtenerMonto();
void pedirContrasenia();
void mostrarMensaje();
private:
string nombreUsuario;
int edadUsuario;
int montoUsuario;
int contraseniaUsuario;
};
//****************( " end of Cuenta.h " )****************
Code:
//****************( " Cuenta.cpp " )****************
#include<iostream>
#include"Cuenta.h"
using namespace std;
Cuenta1::Cuenta1(){
establecerValores( "Luis Alberto Sánchez M", 28, 3600 );
}
void Cuenta1::establecerValores( string nombre, int edad, int monto ){
nombreUsuario = nombre;
edadUsuario = edad;
montoUsuario = monto;
pedirContrasenia();
}
string Cuenta1::obtenerUsuario(){
return nombreUsuario;
}
int Cuenta1::obtenerEdad(){
return edadUsuario;
}
int Cuenta1::obtenerMonto(){
return montoUsuario;
}
void Cuenta1::pedirContrasenia(){
int contraseniaActual = 1988;
cout << "Por favor introduzca su contraseña : ";
cin >> contraseniaUsuario;
cout << "\n\n";
if( contraseniaUsuario == contraseniaActual )
mostrarMensaje();
if( contraseniaUsuario != contraseniaActual )
cout << "\n\n";
cout << "Usted ingreso una contraseña incorrecta se se reinicializara el sistema\n"
<< "por favor introduzca la contraseña correcta la proxima vez" << "\n\n" ;
}
void Cuenta1::mostrarMensaje(){
cout << "Bienvenido a cajeros Nature : " << obtenerUsuario() << " Edad " << obtenerEdad() << "usted tiene $ " << obtenerMonto() << "\n\n";
}
//****************( " end of cuenta.cpp " )****************
Code:
//****************( " main.cpp " )****************
#include<iostream>
#include<cstdlib>
#include "Cuenta.h"
using namespace std;
int main(){
Cuenta1 cuentaLuis();
system("PAUSE");
return 0;
}
//****************( " end of main.cpp " )****************
Re: my code compiles and executes but it does not show anything at display
Your main function declares a function named cuentaLuis() that returns an object of Cuenta1: https://en.wikipedia.org/wiki/Most_vexing_parse
You probably wanted to create an instance of Cuenta1
Code:
Cuenta1 cuentaLuis;
Cheers,
_
Re: my code compiles and executes but it does not show anything at display
I don't want to start a war, but I don't think you should really do any "processing" in a C++ constructor. The purpose of the constructor is to initialize the object instance to a specific known state.
Re: my code compiles and executes but it does not show anything at display
##########
Quote:
Originally Posted by
anda_skoa
It works now silly me hehe. I am not yet into creating an instance out of a function that takes no parameters and returns a type of the constructor part of the ebook yet, so thanks very much for clarify this issue for me I guess Im just a newbie
Quote:
Originally Posted by
jefftee
I don't want to start a war, but I don't think you should really do any "processing" in a C++ constructor. The purpose of the constructor is to initialize the object instance to a specific known state.
I am a newbie into programming so I shoud ask, why not to do what you just stated?
Re: my code compiles and executes but it does not show anything at display
Quote:
Originally Posted by
sauerplayer
I am a newbie into programming so I shoud ask, why not to do what you just stated?
There are many subtle reasons why this is the case, google around for "what you should not do in a C++ constructor" to read some of the challenges with constructors. For me, the big reason is that constructors don't have return values, so it complicates error handling when your constructor has a lot of logic.
Re: my code compiles and executes but it does not show anything at display
thnks for your help
Added after 1 22 minutes:
thanks for your help