Results 1 to 6 of 6

Thread: my code compiles and executes but it does not show anything at display

  1. #1
    Join Date
    Oct 2016
    Posts
    10
    Platforms
    Windows

    Default 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.

    Qt Code:
    1. //****************( "Cuenta.h " )****************
    2.  
    3. #include<string>
    4. using namespace std;
    5.  
    6. class Cuenta1{
    7.  
    8. public:
    9.  
    10. Cuenta1();
    11.  
    12. void establecerValores( string, int, int );
    13.  
    14. string obtenerUsuario( );
    15.  
    16. int obtenerEdad();
    17.  
    18. int obtenerMonto();
    19.  
    20. void pedirContrasenia();
    21.  
    22. void mostrarMensaje();
    23.  
    24. private:
    25.  
    26. string nombreUsuario;
    27. int edadUsuario;
    28. int montoUsuario;
    29. int contraseniaUsuario;
    30. };
    31.  
    32. //****************( " end of Cuenta.h " )****************
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. //****************( " Cuenta.cpp " )****************
    2.  
    3. #include<iostream>
    4. #include"Cuenta.h"
    5. using namespace std;
    6.  
    7. Cuenta1::Cuenta1(){
    8.  
    9. establecerValores( "Luis Alberto Sánchez M", 28, 3600 );
    10. }
    11.  
    12. void Cuenta1::establecerValores( string nombre, int edad, int monto ){
    13.  
    14. nombreUsuario = nombre;
    15. edadUsuario = edad;
    16. montoUsuario = monto;
    17. pedirContrasenia();
    18. }
    19.  
    20. string Cuenta1::obtenerUsuario(){
    21.  
    22. return nombreUsuario;
    23. }
    24.  
    25. int Cuenta1::obtenerEdad(){
    26.  
    27. return edadUsuario;
    28. }
    29.  
    30. int Cuenta1::obtenerMonto(){
    31.  
    32. return montoUsuario;
    33. }
    34.  
    35. void Cuenta1::pedirContrasenia(){
    36.  
    37. int contraseniaActual = 1988;
    38. cout << "Por favor introduzca su contraseña : ";
    39. cin >> contraseniaUsuario;
    40. cout << "\n\n";
    41.  
    42. if( contraseniaUsuario == contraseniaActual )
    43.  
    44. mostrarMensaje();
    45.  
    46. if( contraseniaUsuario != contraseniaActual )
    47.  
    48. cout << "\n\n";
    49. cout << "Usted ingreso una contraseña incorrecta se se reinicializara el sistema\n"
    50. << "por favor introduzca la contraseña correcta la proxima vez" << "\n\n" ;
    51.  
    52. }
    53.  
    54. void Cuenta1::mostrarMensaje(){
    55.  
    56. cout << "Bienvenido a cajeros Nature : " << obtenerUsuario() << " Edad " << obtenerEdad() << "usted tiene $ " << obtenerMonto() << "\n\n";
    57. }
    58.  
    59. //****************( " end of cuenta.cpp " )****************
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. //****************( " main.cpp " )****************
    2.  
    3. #include<iostream>
    4. #include<cstdlib>
    5. #include "Cuenta.h"
    6. using namespace std;
    7.  
    8. int main(){
    9.  
    10. Cuenta1 cuentaLuis();
    11.  
    12. system("PAUSE");
    13. return 0;
    14. }
    15.  
    16. //****************( " end of main.cpp " )****************
    To copy to clipboard, switch view to plain text mode 
    Last edited by sauerplayer; 5th November 2016 at 08:13.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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
    Qt Code:
    1. Cuenta1 cuentaLuis;
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. #3
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default 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.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  4. #4
    Join Date
    Oct 2016
    Posts
    10
    Platforms
    Windows

    Default Re: my code compiles and executes but it does not show anything at display

    ##########

    Quote Originally Posted by anda_skoa View Post
    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
    Qt Code:
    1. Cuenta1 cuentaLuis;
    To copy to clipboard, switch view to plain text mode 

    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

    Quote Originally Posted by jefftee View Post
    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?
    Last edited by sauerplayer; 6th November 2016 at 03:57.

  5. #5
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: my code compiles and executes but it does not show anything at display

    Quote Originally Posted by sauerplayer View Post
    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.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  6. #6
    Join Date
    Oct 2016
    Posts
    10
    Platforms
    Windows

    Default 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
    Last edited by sauerplayer; 7th November 2016 at 21:25.

Similar Threads

  1. JS not executes in QTWebkit
    By ericzhang in forum Qt Programming
    Replies: 3
    Last Post: 25th March 2014, 12:31
  2. Create And Display QWidget in code
    By ShapeShiftme in forum Qt Programming
    Replies: 4
    Last Post: 19th May 2012, 08:04
  3. qt I want my widget show security code from web
    By insert in forum Qt Programming
    Replies: 7
    Last Post: 1st December 2010, 19:58
  4. Dialog executes in debug build but not release
    By awhite1159 in forum Qt Programming
    Replies: 5
    Last Post: 24th June 2008, 18:51
  5. Plugin ctor executes, but not dtor ?
    By scollyer in forum Qt Programming
    Replies: 1
    Last Post: 12th January 2008, 15:18

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.