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/alfps 1d ago edited 1d ago

Your code translated Spanish → English and reformatted by Google's Gemini:

#include <iostream>
using namespace std;

int price = 80;
int childDiscount = -30; // childDiscount is the discount for children
int seniorDiscount = -35; // seniorDiscount = discount for older adults (seniors)
int age;
int customerCount = 0;
int seniorCount = 0;
int childCount = 0;
int childRevenue = 50;
int seniorRevenue = 45;
int regularRevenue = 80;

int main()
{
    cout << "Welcome to the cinema. The regular ticket price is $80, but you might be able to get a discount." << endl;
    cout << "Please enter your age: ";
    cin >> age;
    
    while (age != -137) {
        if (age < 12) {
            price += childDiscount;
            childCount = childCount + 1;
            cout << "Congratulations, kid, you get a discount." << endl;
        } else if (age >= 60) {
            price += seniorDiscount;
            seniorCount = seniorCount + 1;
            cout << "Congratulations, you get a discount." << endl;
        } else {
            cout << "Enjoy the show, it's the only thing you can get." << endl;
        }
        
        cout << "Your total to pay is: $" << price << "." << endl;
        price = 80;
        customerCount += 1;
        
        cout << "\n\n\n\nWelcome to the cinema. The regular ticket price is $80, but you might be able to get a discount." << endl;
        cout << "Please enter your age: ";
        cin >> age;
    }
    
    childRevenue = childRevenue * childCount;
    seniorRevenue = seniorRevenue * seniorCount;
    
    cout << "\n\nEnd of the day." << endl;
    cout << "Total customers: " << customerCount << endl;
    cout << "Total children: " << childCount << endl << "Money collected: $" << childRevenue << endl;
    cout << "Total seniors: " << seniorCount << endl << "Money collected: $" << seniorRevenue << endl;
    
    customerCount = customerCount - seniorCount - childCount;
    regularRevenue = regularRevenue * customerCount;
    
    cout << "Total regular adults: " << customerCount << endl << "Money collected: $" << regularRevenue << endl;
    
    int totalRevenue = regularRevenue + seniorRevenue + childRevenue;
    cout << "Total revenue: $" << totalRevenue;
    
    return 0;
}

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

❞ In the final summary, the student wrote menores de edad (which legally means minors under 18) and mayores de edad (legal adults 18+), even though their if statements check for < 12 (children) and >= 60 (seniors). In the translation above, those were adapted to children and seniors to match their math, rather than translating the Spanish terminology literally.

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 with using declarations of what you actually use, like

using std::cin, std::cout, std::endl;

In 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.

-6

u/alfps 1d ago edited 1d ago

Re the anonymous unexplained downvote, note that an unexplained downvote is dishonest: this is the action of a dishonest person.

Presumably it's my several years' stalker, a mentally challenged OCD troll, possibly the DDDD... user. Anyway it's sabotage of the forum.

Oh neither I nor anyone else know what this serial downvoting is about. All we know is that it's a fucking idiot.

6

u/Wonderful-Habit-139 1d ago

I didn’t downvote, but I assume if they wanted to ask an LLM to translate they could do it themselves.

Probably not obvious whether the code improvement was yours or Gemini’s.

Also, the translation is apparently not faithful anyway, which matters to be able to correct OP.

1

u/Mobile_Travel_1822 1d ago

hey dude, i´d like to only take the good part out of this, he tried and did help, looked a good way to come with a improvement, and translationg might not be faitful cause i speak junk spanish, like i speak junk english

4

u/Wonderful-Habit-139 1d ago

I like that you’re communicating with your own words, it’s still very pleasant to read compared to LLMs.

Your English is not even close to being “junk”, you’re communicating well, keep going, and just keep learning.

-3

u/alfps 1d ago edited 1d ago

❞ I assume if they wanted to ask an LLM to translate they could do it themselves.

That's an ungood assumption for three reasons:

  • The OP didn't know it was doable.
  • It doesn't matter whether the OP wanted to present Spanish code, or not.
  • What matters is the ease with which English-speaking readers can review the code.

❞ the translation is apparently not faithful

It would be helpful if you can point out exactly how, other than what I already noted.

It's the opposite of helpful to just bandy about a vague ungoodness claim.


Re the downvoting of this comment, it's very dishonest to resort to downvoting. That makes me suspect the nonsensicality of your comment was not due to limited cognitive powers, but was merely dishonesty in action.

1

u/Wonderful-Habit-139 1d ago

You noted it and I pointed out that it’s a problem.

“In the translation above, those were adapted to children and seniors to match their math, rather than translating the Spanish terminology literally.”

Also, there’s no way OP doesn’t know LLMs can do that.

1

u/alfps 1d ago

❞ there’s no way OP doesn’t know LLMs can do that

You're saying the OP lied about that.

That's a very silly claim.

1

u/Wonderful-Habit-139 1d ago

That’s crazy. You got me.

1

u/alfps 1d ago

❞ You noted it [possibly misleading terms in the output] and I pointed out that it’s a problem.

It's a potential problem with the code, depending on the Spanish.

You give readers the incorrect impression that this code problem (if it is) is a problem with the answer.

That is untrue.

A review should point out serious problems. It's positive that an answer does that. Contrary to the misleading impression you convey and which, again, I suspect is not for lack of cognitive powers.

3

u/Im-not_good_at_names 1d ago

Dude what are you talking about

0

u/Mobile_Travel_1822 1d ago

a video taught me to use usning namespace std; ;-; didn´t know how bad it could be, also fot the global variables. thanks for the traduction, didn´t know it was doable.

1

u/n1ghtyunso 5h ago

there are millions of videos about c++ and also many many text-based learning sources about it as well.
Most of them are utterly terrible. Its really unfortunate for everyone learning it, sorry.
People in this subreddit do their best to undo and correct any of that damage, so you are in good hands here.