Results 1 to 6 of 6

Thread: Im not getting the expected result

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

    Default Im not getting the expected result

    in the part of this code I expect the variable precioFinal ( " precioFinal += costoFinal; " ) to acumulate all the costs of all the buyed items but instead of that it only returns the price of the last buyed item. what am I doing wrong?

    Qt Code:
    1. //****************( Factura.h )****************
    2.  
    3. #include<string>
    4. using namespace std;
    5.  
    6. class Factura{
    7.  
    8. public:
    9.  
    10. Factura();
    11.  
    12. void establecerDatos( string, string, int, int );
    13.  
    14. void mostrarMensaje();
    15.  
    16. void procesar( int, int );
    17.  
    18. string obtenerNumeroDePieza();
    19. string obtenerDescripcionDePieza();
    20. int obtenerCantidadDeArticulos();
    21. int obtenerPrecioDelArticulo();
    22. int obtenerPrecioFinal();
    23. int obtenerCostoFinal();
    24. int obtenerTotal();
    25.  
    26. private:
    27.  
    28. string numeroDePieza;
    29. string descripcionDePieza;
    30. int cantidadDeArticulos;
    31. int precioDeArticulo;
    32. int precioFinal;
    33. int costoFinal;
    34. };
    35.  
    36. //****************( end of Factura.h )****************
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //****************( Factura.cpp )****************
    2.  
    3. #include<iostream>
    4. #include"Factura.h"
    5. using namespace std;
    6.  
    7. Factura::Factura(){
    8.  
    9. establecerDatos( numeroDePieza, descripcionDePieza, cantidadDeArticulos, precioDeArticulo );
    10. }
    11.  
    12. void Factura::establecerDatos( string numeroPieza, string descripcionPieza, int cantidad, int precio ){
    13.  
    14. numeroDePieza = numeroPieza;
    15. descripcionDePieza = descripcionPieza;
    16. cantidadDeArticulos = cantidad;
    17. precioDeArticulo = precio;
    18. }
    19.  
    20. void Factura::mostrarMensaje(){
    21.  
    22. char decidir;
    23. int cantidadAcomprar;
    24.  
    25. cout << "Bienvenidos a Ferreteria Don Juan : que es lo que necesita? : \n\n"
    26. << "Contamos con a)Foco, b)cable, c)pintura, d)martillo, e)cierra, f)tijeras\n"
    27. << "g)lija, h)solvente, i)desarmador, y j)fusibles\n\nintroduzca una opcion\n\n";
    28.  
    29. cin >> decidir;
    30.  
    31. switch( decidir ){
    32.  
    33. case 'a':
    34.  
    35. cout << "Cuantos Focos desea comprar ? : ";
    36.  
    37. cin >> cantidadAcomprar;
    38.  
    39. establecerDatos( "14268596", "Foco ahorrador luz Calida", cantidadAcomprar, 127 );
    40.  
    41. procesar( cantidadAcomprar, 127 );
    42.  
    43. cout << "Usted compro codigo : " << obtenerNumeroDePieza() << " " << obtenerDescripcionDePieza() << " cantidad " << obtenerCantidadDeArticulos() << " precio " << obtenerCostoFinal() << "\n\n";
    44.  
    45. break;
    46.  
    47. case 'b':
    48.  
    49. cout << "Cuantos cables desea comprar ? : ";
    50.  
    51. cin >> cantidadAcomprar;
    52.  
    53. establecerDatos( "16589742", "CableCoaxial blanco 3 metros", cantidadAcomprar, 65 );
    54.  
    55. procesar( cantidadAcomprar, 65 );
    56.  
    57. cout << "Usted compro codigo : " << obtenerNumeroDePieza() << " " << obtenerDescripcionDePieza() << " cantidad " << obtenerCantidadDeArticulos() << " precio " << obtenerCostoFinal() << "\n\n";
    58.  
    59. break;
    60.  
    61. case 'c':
    62.  
    63. cout << "Cuantos pinturas desea comprar ? : ";
    64.  
    65. cin >> cantidadAcomprar;
    66.  
    67. establecerDatos( "42698816", "Pintura acrilica blanca 1 litro", cantidadAcomprar, 74 );
    68.  
    69. procesar( cantidadAcomprar, 74 );
    70.  
    71. cout << "Usted compro codigo : " << obtenerNumeroDePieza() << " " << obtenerDescripcionDePieza() << " cantidad " << obtenerCantidadDeArticulos() << " precio " << obtenerCostoFinal() << "\n\n";
    72.  
    73. break;
    74.  
    75. case 'd':
    76.  
    77. cout << "Cuantos martillos desea comprar ? : ";
    78.  
    79. cin >> cantidadAcomprar;
    80.  
    81. establecerDatos( "72698756", "Martillo metalico chico", cantidadAcomprar, 50 );
    82.  
    83. procesar( cantidadAcomprar, 50 );
    84.  
    85. cout << "Usted compro codigo : " << obtenerNumeroDePieza() << " " << obtenerDescripcionDePieza() << " cantidad " << obtenerCantidadDeArticulos() << " precio " << obtenerCostoFinal() << "\n\n";
    86.  
    87. break;
    88.  
    89. case 'e':
    90.  
    91. cout << "Cuantas cierras desea comprar ? : ";
    92.  
    93. cin >> cantidadAcomprar;
    94.  
    95. establecerDatos( "26978543", "Cierra metalica con cegueta", cantidadAcomprar, 83 );
    96.  
    97. procesar( cantidadAcomprar, 83 );
    98.  
    99. cout << "Usted compro codigo : " << obtenerNumeroDePieza() << " " << obtenerDescripcionDePieza() << " cantidad " << obtenerCantidadDeArticulos() << " precio " << obtenerCostoFinal() << "\n\n";
    100.  
    101. break;
    102.  
    103. case 'f':
    104.  
    105. cout << "Cuantas tijeras desea comprar ? : ";
    106.  
    107. cin >> cantidadAcomprar;
    108.  
    109. establecerDatos( "98542689", "Tijeras metalicas para electricista", cantidadAcomprar, 59 );
    110.  
    111. procesar( cantidadAcomprar, 59 );
    112.  
    113. cout << "Usted compro codigo : " << obtenerNumeroDePieza() << " " << obtenerDescripcionDePieza() << " cantidad " << obtenerCantidadDeArticulos() << " precio " << obtenerCostoFinal() << "\n\n";
    114.  
    115. break;
    116.  
    117. case 'g':
    118.  
    119. cout << "Cuantas lijas desea comprar ? : ";
    120.  
    121. cin >> cantidadAcomprar;
    122.  
    123. establecerDatos( "15829527", "Lija de agua numero 1", cantidadAcomprar, 12 );
    124.  
    125. procesar( cantidadAcomprar, 12 );
    126.  
    127. cout << "Usted compro codigo : " << obtenerNumeroDePieza() << " " << obtenerDescripcionDePieza() << " cantidad " << obtenerCantidadDeArticulos() << " precio " << obtenerCostoFinal() << "\n\n";
    128.  
    129. break;
    130.  
    131. case 'h':
    132.  
    133. cout << "Cuantos solventes desea comprar ? : ";
    134.  
    135. cin >> cantidadAcomprar;
    136.  
    137. establecerDatos( "17983366", "Solvente thinner para pintura 1 litro", cantidadAcomprar, 18 );
    138.  
    139. procesar( cantidadAcomprar, 18 );
    140.  
    141. cout << "Usted compro codigo : " << obtenerNumeroDePieza() << " " << obtenerDescripcionDePieza() << " cantidad " << obtenerCantidadDeArticulos() << " precio " << obtenerCostoFinal() << "\n\n";
    142.  
    143. break;
    144.  
    145. case 'i':
    146.  
    147. cout << "Cuantos desarmadores desea comprar ? : ";
    148.  
    149. cin >> cantidadAcomprar;
    150.  
    151. establecerDatos( "13263345", "Desarmador de cruz con mango de goma", cantidadAcomprar, 30 );
    152.  
    153. procesar( cantidadAcomprar, 30 );
    154.  
    155. cout << "Usted compro codigo : " << obtenerNumeroDePieza() << " " << obtenerDescripcionDePieza() << " cantidad " << obtenerCantidadDeArticulos() << " precio " << obtenerCostoFinal() << "\n\n";
    156.  
    157. break;
    158.  
    159. case 'j':
    160.  
    161. cout << "Cuantos fusibles desea comprar ? : ";
    162.  
    163. cin >> cantidadAcomprar;
    164.  
    165. establecerDatos( "32269564", "Fusible para caja electrica residencial", cantidadAcomprar, 8 );
    166.  
    167. procesar( cantidadAcomprar, 8 );
    168.  
    169. cout << "Usted compro codigo : " << obtenerNumeroDePieza() << " " << obtenerDescripcionDePieza() << " cantidad " << obtenerCantidadDeArticulos() << " precio " << obtenerCostoFinal() << "\n\n";
    170.  
    171. break;
    172. }
    173. }
    174.  
    175. void Factura::procesar( int cantidad, int precio ){
    176.  
    177. precioFinal = 0;
    178.  
    179. costoFinal = cantidad * precio;
    180.  
    181. precioFinal += costoFinal;
    182.  
    183. }
    184.  
    185. string Factura::obtenerNumeroDePieza(){
    186.  
    187. return numeroDePieza;
    188. }
    189.  
    190. string Factura::obtenerDescripcionDePieza(){
    191.  
    192. return descripcionDePieza;
    193. }
    194.  
    195. int Factura::obtenerCantidadDeArticulos(){
    196.  
    197. return cantidadDeArticulos;
    198. }
    199.  
    200. int Factura::obtenerPrecioDelArticulo(){
    201.  
    202. return precioDeArticulo;
    203. }
    204.  
    205. int Factura::obtenerPrecioFinal(){
    206.  
    207. return precioFinal;
    208. }
    209.  
    210. int Factura::obtenerCostoFinal(){
    211.  
    212. return costoFinal;
    213. }
    214.  
    215. //****************( end of Factura.cpp )****************
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //****************( main.cpp )****************
    2.  
    3. using namespace std;
    4.  
    5. int main(){
    6.  
    7. int seleccionar;
    8. Factura factura1;
    9. factura1.mostrarMensaje();
    10. cout << "\n\nDesea comprar mas cosas? : 1)Si 2)No : ";
    11. cin >> seleccionar;
    12.  
    13. while( seleccionar == 1 ){
    14.  
    15. factura1.mostrarMensaje();
    16. cout << "\n\nDesea comprar mas cosas? : 1)Si 2)No : ";
    17. cin >> seleccionar;
    18. }
    19.  
    20. if( seleccionar == 2 ){
    21.  
    22. cout << "Gracias por su compra\n\nEl total a pagar es : " << factura1.obtenerPrecioFinal();
    23. }
    24.  
    25. system("PAUSE");
    26. return 0;
    27. }
    28.  
    29. //****************( end of main.cpp )****************
    To copy to clipboard, switch view to plain text mode 

  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: Im not getting the expected result

    Quote Originally Posted by sauerplayer View Post
    in the part of this code I expect the variable precioFinal ( " precioFinal += costoFinal; " ) to acumulate all the costs of all the buyed items
    Are you talking about the code in Factura:rocesar()?
    Why would you expect that to hold a sum?
    You are setting the variable to 0 on every call.

    Cheers,
    _

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

    Default Re: Im not getting the expected result

    Quote Originally Posted by anda_skoa View Post
    Are you talking about the code in Factura:rocesar()?
    Why would you expect that to hold a sum?
    You are setting the variable to 0 on every call.

    Cheers,
    _
    so If I modify this code will it work?

    Qt Code:
    1. void Factura::procesar( int cantidad, int precio ){
    2.  
    3. costoFinal = cantidad * precio;
    4.  
    5. precioFinal += costoFinal;
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    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: Im not getting the expected result

    Quote Originally Posted by sauerplayer View Post
    so If I modify this code will it work?
    You are not serioulsy asking that instead of trying it, right?

    Cheers,
    _

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

    Default Re: Im not getting the expected result

    Quote Originally Posted by anda_skoa View Post
    You are not serioulsy asking that instead of trying it, right?

    Cheers,
    _
    I found the and fixed the problem, now it works correctly, the problem was that I did not initialize PrecioFinal before using it, but it had to be done inside the constructor...

    Factura::Factura(){

    precioFinal = 0;

    establecerDatos( numeroDePieza = "", descripcionDePieza = "", cantidadDeArticulos = 0, precioDeArticulo = 0 );
    }

    still I do not undesrtand why, if I try to initialize precioFinal to 0 inside Factura.h it fails to do so...

    the error message I get for this is : C:\Users\eyel\Desktop\CBlocks\Clase9\Factura.h|30|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11|

  6. #6
    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: Im not getting the expected result

    Quote Originally Posted by sauerplayer View Post
    still I do not undesrtand why, if I try to initialize precioFinal to 0 inside Factura.h it fails to do so...

    the error message I get for this is : C:\Users\eyel\Desktop\CBlocks\Clase9\Factura.h|30|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11|
    As the compiler hints that is available from the C++11 standard onwards.
    If you are building for an older standard then you need to use assignment in code or the constructor's initialization list.

    Cheers,
    _

Similar Threads

  1. SQL result problem
    By unix7777 in forum Newbie
    Replies: 7
    Last Post: 14th August 2012, 00:31
  2. How i can plot result in my GUI ?
    By 21did21 in forum Qwt
    Replies: 58
    Last Post: 18th June 2011, 01:41
  3. Replies: 10
    Last Post: 22nd May 2011, 16:58
  4. QRegExp - get only last result
    By kabanek in forum Newbie
    Replies: 2
    Last Post: 3rd November 2010, 23:17
  5. How to display result
    By sksingh73 in forum Newbie
    Replies: 1
    Last Post: 7th June 2010, 09:39

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.