Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: JohnAMH en 13 Octubre 2018, 22:47 pm



Título: Ayuda urgente con un problema de C++
Publicado por: JohnAMH en 13 Octubre 2018, 22:47 pm
Buenos días, tengo un problema urgente con un ejercicio de c++ que me cuesta solucionar. el ejercicio es el siguiente:
Escribir un programa que ofrezca las siguientes opciones:
1. Client data
2. Sale data
3. Invoice
4. Totals
5. Restart
0. Exit
El programa mostrará el menú hasta que el usuario elija la opción.. Por ahora lo que llevó es esto. Seguramente haya mucha cosa innecesaria. Si es así, me lo podéis decir. Espero respuesta.
Muchas gracias de antemano.

Código
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. #include <cctype>
  5. #include <iomanip>
  6.  
  7. int menu();
  8. double cost(int descPercentage, double total, double final, double amountDisc, double amountVAT);
  9.  
  10.        int main() {
  11. int option;
  12. int units;
  13. int readCost;
  14. int readMenu;
  15. int descPercentage;
  16. const int VAT = 21;
  17. const int DISCOUNT = 15;
  18. double price;
  19. double total;
  20. double readcost;
  21. double final;
  22. double amountDisc;
  23. double amountVAT;
  24.        bool sale = false;
  25. bool client = false;
  26. char discount;
  27. string product;
  28. string name, nif, address;
  29.  
  30. cout << "1. Client Data" << endl;
  31. cout << "2. Sale Data" << endl;
  32. cout << "3. Invoice" << endl;
  33. cout << "4. Totals" << endl;
  34. cout << "5. Restart" << endl;
  35. cout << "0. Exit" << endl;
  36. cout << "Option: ";
  37. cin >> option;
  38. cout << endl;
  39. switch (option) {
  40. case 1:
  41. if (client == false) {
  42. cout << "Client Name: ";
  43. cin.sync();
  44. getline (cin, name);
  45. cout << "Client NIF: ";
  46. getline (cin, nif);
  47. cout << "Client Address: ";
  48. getline (cin, address);
  49. cout << client;
  50. cout << endl;
  51. }
  52. case 2:
  53. if (sale == true) {
  54. readMenu = menu();
  55. cout << "Product Name: ";
  56. cin.sync();
  57. getline (cin, product);
  58. cout << "Product Price: ";
  59. cin >> price;
  60. cout << "Discount (y/n): ";
  61. cin >> discount;
  62. discount = toupper(discount);
  63. if (discount == 'Y') {
  64. descPercentage = DISCOUNT;
  65. }
  66. else {
  67. descPercentage = 0;
  68. }
  69. cout << "Units: ";
  70. cin >> units;
  71. sale = true;
  72. cout << sale;
  73. cout << endl;
  74. }
  75. else if (sale != false){
  76. cout << "No sale data!" << endl;
  77. cout << endl;
  78. readMenu = menu();
  79. }
  80. case 3:
  81. if (client == false) {
  82. cout << "No client data!" << endl;
  83. cout << endl;
  84. readMenu = menu();
  85. client = false;
  86. cout << client;
  87. }
  88. else {
  89. readMenu = menu ();
  90. cout << endl << "Invoice:" << endl;
  91. cout << setw(50) << right << name << endl;
  92. cout << setw(50) << right << nif << endl;
  93. cout << setw(50) << right << address << endl << endl;
  94. readMenu = menu ();
  95. client = true;
  96. cout << client;
  97. }
  98.              case 4:
  99. if (sale == false) {
  100. cout << "No sale data!" << endl;
  101. cout << "1. Client Data" << endl;
  102. cout << "2. Sale Data" << endl;
  103. cout << "3. Invoice" << endl;
  104. cout << "4. Totals" << endl;
  105. cout << "5. Restart" << endl;
  106. cout << "0. Exit" << endl;
  107. cout << "Option: ";
  108. cin >> option;
  109. sale = false;
  110. cout << sale;
  111. }
  112. else {
  113. readcost = cost(descPercentage, total, final, amountDisc, amountVAT);
  114. cout << "Product: " << product << endl;
  115. cout << setw(40) << left << "Unit Price";
  116. cout << setw(10) << right << fixed << setprecision(2) << price << endl;
  117. cout << setw(40) << left << "Units";
  118. cout << setw(7) << right << units << endl;
  119. cout << setw(40) << left << "Total";
  120. cout << setw(10) << right << fixed << setprecision(2) << total << endl;
  121. cout << setw(40) << left << "Discount";
  122. cout << setw(7) << right << descPercentage << " %" << endl;
  123. cout << setw(50) << right << fixed << setprecision(2) << -amountDisc << endl;
  124. cout << setw(40) << left << "Total after discount";
  125. cout << setw(10) << right << fixed << setprecision(2) << total << endl;
  126. cout << setw(40) << left << "VAT";
  127. cout << setw(7) << right << VAT << " %" << endl;
  128. cout << setw(50) << right << fixed << setprecision(2) << amountVAT << endl;
  129. cout << setw(40) << left << "Final Price";
  130. cout << setw(10) << right << fixed << setprecision(2) << final << endl;
  131. sale = true;
  132. cout << sale;
  133. }
  134. case 5:
  135. system ("CLS");
  136. break;
  137. default:
  138. cout << "1. Client Data" << endl;
  139. cout << "2. Sale Data" << endl;
  140. cout << "3. Invoice" << endl;
  141. cout << "4. Totals" << endl;
  142. cout << "5. Restart" << endl;
  143. cout << "0. Exit" << endl;
  144. cout << "Option: ";
  145. cin >> option;
  146. cout << endl;
  147. }
  148. return 0;
  149. }
  150.  
  151.      int menu() {
  152.  
  153. int option;
  154. int readMenu;
  155.  
  156. cout << "1. Client Data" << endl;
  157. cout << "2. Sale Data" << endl;
  158. cout << "3. Invoice" << endl;
  159. cout << "4. Totals" << endl;
  160. cout << "5. Restart" << endl;
  161. cout << "0. Exit" << endl;
  162. cout << "Option: ";
  163. cin >> option;
  164. cout << endl;
  165. while (option <= -1 || option >= 6) {
  166. cout << "1. Client Data" << endl;
  167. cout << "2. Sale Data" << endl;
  168. cout << "3. Invoice" << endl;
  169. cout << "4. Totals" << endl;
  170. cout << "5. Restart" << endl;
  171. cout << "0. Exit" << endl;
  172. cout << "Option: ";
  173. cin >> option;
  174. cout << endl;
  175. }
  176. return option;
  177. }
  178. double cost(int descPercentage, double total, double final, double amountDisc, double amountVAT) {
  179. const int VAT = 21;
  180. const double DISCOUNT = 15;
  181. char discount;
  182.  
  183. amountDisc = total * descPercentage / 100;
  184. total = total - amountDisc;
  185. amountVAT = total * VAT / 100;
  186. final = total + amountVAT;
  187.  
  188. return final;
  189. }
  190.  


Título: Re: Ayuda urgente con un problema de C++
Publicado por: Beginner Web en 14 Octubre 2018, 11:16 am
Código
  1. int main()
  2. {
  3. mis variables...
  4. do{
  5. system("cls");
  6. cout<<"1. Primera opcion"<<endl;
  7. cout<<"2. Segunda opcion"<<endl;
  8. cout<<"3. ...."<<endl;
  9. cout<<"4. .."<<endl;
  10. cout<<"5. Salir"<<endl;
  11. cin>>opcion;
  12. switch(opcion){
  13. case 1: hacer_cosas();break;
  14. case 2: hacer_cosas();break;
  15. case 3: hacer_cosas();break;
  16. case 4: hacer_cosas();break;
  17. case 5: cout<<"FIN DEL PROGRAMA"<<endl;break;
  18. default: cout<<"Opcion incorrecta"<<endl;
  19. }
  20. system("pause");
  21. }while(opcion!=5);
  22. }
  23.  


Título: Re: Ayuda urgente con un problema de C++
Publicado por: JohnAMH en 14 Octubre 2018, 14:03 pm
Gracias, pero como haría la función hacer_cosas con las variables bool? Es que las opciones dependen uno de los otros.

Escriba un programa en C ++ que ofrezca al usuario estas opciones:
Option 1: Pregunta al usuario por su client data (name, NIF and address).
Option 2: Pregunta por el nombre del producto, nº de unidades y descuento a aplicar. (product, units, discocunt).
Option 3: Genera la factura. No lo generará si el cliente no ha puesto información suya y del producto (Client Data y Sale Data), devolviéndole al menú.
Option 4: Muestra las ventas totales y el VAT Displays, solo si has puesto antes los datos del cliente y de las ventas, sino te pondrá error y te devolverá al menú.
Option 5:Todos los totales son 0 y no hay datos de clientes ni datos de ventas. Borra todos los datos ofrecidos anteriormente y empieza de 0.

Utilice dos variables bool para averiguar si se han proporcionado datos de clientes y datos de ventas (falso después de reiniciar).

Aún así muchas gracias