Modular Menu
A unit converter with a menu that keeps coming back.
- Level
- Intermediate
- Topic:
- Subprocesses & functions
- Concepts:
- Input & output
- SubProcesos
- Segun
- Repetir
Open in Codoritmo
Run, change and debug this example in your browser.
What you'll learn
The program is split into SubProceso blocks without parameters, each with one job. Repetir ... Mientras Que shows the menu again until you choose to quit.
Code
SubProceso MostrarMenu Escribir "" Escribir "1. Kilómetros a millas" Escribir "2. Litros a galones" Escribir "3. Salir" Escribir "Elige una opción:"FinSubProcesoSubProceso KilometrosAMillas Definir km Como Real Escribir "Kilómetros:" Leer km Escribir km, " km = ", Redon(km * 0.621371 * 100) / 100, " millas"FinSubProcesoSubProceso LitrosAGalones Definir litros Como Real Escribir "Litros:" Leer litros Escribir litros, " L = ", Redon(litros * 0.264172 * 100) / 100, " galones"FinSubProcesoAlgoritmo MenuModular Definir opcion Como Entero Repetir MostrarMenu Leer opcion Segun opcion Hacer 1: KilometrosAMillas 2: LitrosAGalones 3: Escribir "¡Hasta luego!" De Otro Modo: Escribir "Esa opción no existe." FinSegun Mientras Que opcion <> 3FinAlgoritmoFlowchart
Each block in the flowchart matches one instruction in the code.
Sample run
This is what the program shows when it runs. The answers typed by the user are highlighted.
1. Kilómetros a millas2. Litros a galones3. SalirElige una opción:User answer: 1Kilómetros:User answer: 1010 km = 6.21 millas 1. Kilómetros a millas2. Litros a galones3. SalirElige una opción:User answer: 2Litros:User answer: 55 L = 1.32 galones 1. Kilómetros a millas2. Litros a galones3. SalirElige una opción:User answer: 7Esa opción no existe. 1. Kilómetros a millas2. Litros a galones3. SalirElige una opción:User answer: 3¡Hasta luego!
Challenge
Add an option to convert kilograms to pounds.
Show hint
Create a new SubProceso (1 kg = 2.20462 lb), add it to the menu, and move Salir to option 4.
Related examples
Quicksort
Picks a pivot, splits smaller from larger values and sorts each side recursively.
AdvancedBy Reference and by Value
Sorts three numbers by swapping them in subprocesses.
IntermediateTic-Tac-Toe
The classic three-in-a-row game for two players.
Advanced