Skip to content

Lab — Calculator menu (console)

Learn by programming: follow each step, write your own code, then apply it to the course assignment.

What you will build

Console program with menu, functions, and flow control (Partial I).

  1. 1

    Calculator CMake project

    Create `calculadora/` with `CMakeLists.txt` and `main.cpp`. Reuse the structure from [first program](/en/tutoriales/primer-programa/).
  2. 2

    Operation functions

    Implement `sumar`, `restar`, `multiplicar`, and `dividir` taking two `double` values. Review [Functions](/en/fundamentos/funciones/) if needed.
    
    								double dividir(double a, double b) {
        if (b == 0) throw std::invalid_argument("division entre cero");
        return a / b;
    }
    							
  3. 3

    Menu with switch

    A `do-while` loop showing options 1–5 (operations + exit), reads the choice with `cin`, and uses `switch`. Apply [Control structures](/en/fundamentos/estructuras-control/).
  4. 4

    Validate input

    If the user chooses divide and the second operand is 0, show a clear message without crashing. Repeat the menu until exit is chosen.
  5. 5

    Manual test

    Test add, subtract, multiply, valid divide, and divide-by-zero. Save screenshots or console output to review before Partial I.

Back to tutorial page