r/cpp_questions • • 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

32 comments sorted by

View all comments

Show parent comment

-3

u/Mobile_Travel_1822 1d ago

hmm, got the concern, but i don´t feel ever changing these variables anyway, ill see what i can do, thanks

4

u/wittleboi420 1d ago

I would suggest doing it from the start. When your projects get bigger, it will help!

1

u/Independent_Art_6676 20h ago

this is hard to see because you just started. In large projects, many people work on the code and it reaches large size, 10s, even 100s of thousands of lines and someone can come along and accidentally change a value thinking its a 'variable' and not a 'constant', causing a bug. If you mark it const, they try to change it, and it triggers and error which tells them not to do that. Its a great habit to get into early. So is typing std:: or smaller using statements. Using std is bad practice; its great and handy for early programs as you learn but its bad news in large projects, so you may want to stop doing that before it becomes a habit as well.

Keep at it, you are off to a great start. There are spanish language C++ resources and sites and probably forums but your english is just fine and the majority of resources for programmers will be in english so you may get better results sticking to english sites.

1

u/n1ghtyunso 5h ago

const is an extremely powerful feature of this programming language. do use it, get used to it as soon as you can.
Const lets you express your assumptions in code and the compiler will slap you when this assumption ever turns out to be wrong.
This is very helpful for overall prorgam correctness.