PDA

View Full Version : my code compiles and executes but it does not show anything at display



sauerplayer
5th November 2016, 02:12
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.


//****************( "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 " )****************



//****************( " 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 " )****************



//****************( " main.cpp " )****************

#include<iostream>
#include<cstdlib>
#include "Cuenta.h"
using namespace std;

int main(){

Cuenta1 cuentaLuis();

system("PAUSE");
return 0;
}

//****************( " end of main.cpp " )****************

anda_skoa
5th November 2016, 09:19
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


Cuenta1 cuentaLuis;


Cheers,
_

jefftee
5th November 2016, 15:32
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.

sauerplayer
6th November 2016, 02:51
##########


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


Cuenta1 cuentaLuis;


Cheers,
_

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


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?

jefftee
6th November 2016, 10:45
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.

sauerplayer
7th November 2016, 20:25
thnks for your help

Added after 1 22 minutes:

thanks for your help