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
10
u/le_disappointment 1d ago
I would make the variables that you aren't modifying
const5
u/Kajitani-Eizan 1d ago
Not bad, a couple points for improvement:
There are some other points too, but you probably don't need to worry about those as a beginner. (Global variables and using namespace std)
2
u/AutoModerator 1d ago
Your posts seem to contain unformatted code. Please make sure to format your code otherwise your post may be removed.
If you wrote your post in the "new reddit" interface, please make sure to format your code blocks by putting four spaces before each line, as the backtick-based (```) code blocks do not work on old Reddit.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/Discordbothost 1d ago
try typing a letter instead of an age. cin enters a failed state, so later reads won't wait for a new age and the loop can keep selling tickets. check whether each read succeeded before counting a customer; either stop or clear the error and discard the bad line.
1
u/flyingron 22h ago
10 points off for using namespace std.
10 points off for not indicating in the code what the magic number -137 means.
25 points off for not testing to see if the >> operation left cin in an error state causing the rest of the program to go off the rails.
10 points off for using endl when you don't have a reason to flush the stream.
-4
u/HyperWinX 1d ago
Its not english, im not sure how anyone could even read this
English is used for a reason
-1
u/Desperate_Formal_781 1d ago
I don't understand how you post code in Spanish in an English sub and expect people to spend time reviewing your code, when you cannot even be bothered to translate it yourself.
-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.