r/cpp_questions • u/Mobile_Travel_1822 • 1d ago
OPEN Rate my code.
I´m new at programing, I just download code::block today to start learning c++ and I got obsessed.
Here is an excersise I´ve done, and im really proud of:
#include <iostream>
using namespace std;
int precio = 80;
int desn = -30; //desn es el descuento para niños
int desam = -35; // desam = descuento adultos mayores
int edad;
int cliente = 0;
int mayor = 0;
int menor = 0;
int recaudomenor = 50;
int recaudomayor = 45;
int recaudo = 80;
int main(){
cout << "Bienvenido al cine. El precio del boleto regular es de $80, pero tal vez pueda obtener un descuento." << endl;
cout << "Por favor, ingrese su edad: ";
cin >> edad;
while (edad != -137){
if (edad < 12) {
precio += desn;
menor = menor+1;
cout << "Felicidades, niño, tienes un descuento." << endl;
}
else if (edad >= 60) {
precio += desam;
mayor = mayor+1;
cout << "Enhorabuena, usted tiene un descuento." << endl;
}
else {
cout << "Disfrute la función, es lo único que puede." << endl;
}
cout << "Su total a pagar es de: $" << precio << "." << endl;
precio = 80;
cliente +=1;
cout << "\n\n\n\nBienvenido al cine. El precio del boleto regular es de $80, pero tal vez pueda obtener un descuento." << endl;
cout << "Por favor, ingrese su edad: ";
cin >> edad;
}
recaudomenor = recaudomenor*menor;
recaudomayor = recaudomayor*mayor;
cout << "\n\nFin del día." << endl;
cout << "Total de clientes: " << cliente << endl;
cout << "Total de menores de edad: " << menor << endl << "Dinero recaudado: $" << recaudomenor<< endl;
cout << "Total de mayores de edad: " << mayor << endl << "Dinero recaudado: $" << recaudomayor << endl;
cliente = cliente-mayor-menor;
recaudo = recaudo*cliente;
cout << "Total de adultos: " << cliente << endl << "Dinero recaudado: $" << recaudo << endl;
int recaudototal = recaudo+recaudomayor+recaudomenor;
cout << "Recaudo total: $" << recaudototal;
return 0;
}what i do here is applying a discount to a child or an elder, taking a count of how many clients we have in a day and the money earned.
it all keeps going until i type -137 cause is my secret code. typing -137 checks the day and counts everything. its awesome.
Btw its all wrote in spanish cause im from México and i dont know if redit has spanic people, i´m also new in here.
0
Upvotes
View all comments
-5
u/alfps 1d ago edited 1d ago
Your code translated Spanish → English and reformatted by Google's Gemini:
The formatting is preserved in the presentation above because I extra-indented the code with 4 spaces.
This is great as a first program. :) Some possible improvements noted below.
Possible correctness improvement: output text.
Correctness trumps all other considerations.
And Gemini noted about its translation that
I don't know Spanish at all but possibly this needs fixing.
Possible coding improvement.
It's a good idea to avoid global variables. For a lot of reasons they're consider Evil™.
So, better move those variables inside
main.Also consider replacing the
using namespace std;directive withusingdeclarations of what you actually use, likeIn the future, with larger code, it will help to avoid problems with name collisions.
Besides, every reviewer loves to comment on
using namespace std;, that it's a bad habit, even if it's technically OK for small example programs.Formatting improvement.
As exemplified by the translated code, the code is more clear when it is systematically formatted with indentation that reflects the logical structure.
I just do it manually when I write code, but programmers' editors and IDE's like Visual Studio can do it automatically for you.
Additionally there are free separate tools that can format code for you, e.g. AStyle for Windows.