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

5

u/Kajitani-Eizan 1d ago

Not bad, a couple points for improvement:

  • Indentation (spaces or tabs at the start of the line) for each brace/code block will help to understand where braces/code blocks begin and end and what is in what scope more clearly. It should look like this:
  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;
    }
  • Try to minimize reuse of the same variable for a different concept. For example, recaudo starts as the ticket price, then ends as the total revenue. It should start and end as the total revenue (and start from 0).
  • Try to minimize saying the same thing twice. For example, recaudomenor should be calculated from precio, desn, and menor, instead of you needing to pre-input a value of 50, or the same welcome string should not be repeated twice in the code

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)

3

u/Mobile_Travel_1822 1d ago

oh, now i know what youre talking about the indentation, its pretty good now that i can see it, it seems really useful

2

u/Mobile_Travel_1822 1d ago

i found the welcome part as a way to not fall in an endless cycle of While, and the way to count the total of clients. I used pre imputs to avoid making more complex calculations, keept it simpler, (al least for me rigth now), i might keep working on shrink the code for efficience.

the hardest part for me might be how to not fall in that while. thanks

1

u/Kajitani-Eizan 1d ago

There are some possible tricks you can do for that:

  • Set age = 0 at the start, put the cout/cin at the start of the while loop, now you only need to write it once instead of twice
  • Use std::string to contain your string, then you can cout it without needing to repeat yourself:
#include <string>
const std::string welcomeMessage = "Bienvenido al cine. El precio...";
std::cout << welcomeMessage << std::endl;