Waivio

Recommended Posts

[ENG/SPA] Programming C++ #3 - Creating Easy Options Menu with Switch

3 comments

taradraz184.39last yearPeakD5 min read

ENGLISH

Hello everybody!

I want to make this guide to start bringing more programming languages and also to bring more complex things, always trying to bring easy programs like this so that someone without experience can learn in the same way.

I don't want to overextend myself so I will leave the code and I will explain with annotations the process this way I find it easier.

https://img.freepik.com/free-vector/programmer-work-with-working-day-symbols-flat-illustration_1284-60322.jpg?t=st=1686200378~exp=1686200978~hmac=f82109b7953512f067eb0bb010aee076067da335ac25d26439325fa73511ceab

If any part is not understood you can review the previous guides where the basics are explained or search the internet for information on the part you want to deepen or have doubts but I also try that the code is understood by itself.

Here we have the Code:

#include <iostream>
#include <stdio.h>
#include <conio.h>

using namespace std;

int main() {
int ope, age; // Variables to start the menu and store age
int sum1, sum2; // Variables for addition
int rest1, rest2; // Variables for subtraction
int result1, result2;//Variables to save results

// Print menu text on the screen
cout << " ****   Welcome to the Main Menu   ****\n";
cout << "Select an option\n";

cout << "1 = Enter Age\n";

cout << "2 = Perform Addition\n";

cout << "3 = Perform Subtraction\n";

cout << "4 = Quit the program\n";

// Read the operator variable from the user input
cin >> ope;

// The variable 'ope' determines the case to execute in the switch statement
switch (ope) {
    // Each case represents an option in the menu
    // If the user enters 1, the first case is executed
    case 1:  // Start of case

        cout << "You selected option 1\n";

        cout << "Enter a number: ";

        cin >> age;

        cout << "The entered age is: " << age << endl;

        break; // End of Case 1

    case 2: // Start of case

        cout << "You selected option 2 for addition" << endl;
        cout << "Enter the first number" << endl;
        cin >> sum1;
        cout << "Enter the second number" << endl;
        cin >> sum2;
          result1 = sum1 + sum2;
        cout<<"The result is: "<<result1<<endl;;
        break; // End of Case 2

    case 3: // Start of case

        cout << "You selected option 3" << endl;
        cout << "Enter the first number" << endl;
        cin >> rest1;
        cout << "Enter the second number" << endl;
        cin >> rest2;
             result2 = rest1 - rest2;
        cout<<"The result is : "<<result2<<endl;;
        break; // End of Case 3

    case 4: // Start of case

        cout << "The program has finished";

        break; // End of Case 4

}

return 0;}

Any doubts will be answered in the comments!!!, this way you can also make everything clearer!!!

https://media.tenor.com/cj8PbXwdLPcAAAAC/thumbs-up-simon-cowell.gif

That's all Thank you very much!!!

ESPAÑOL

Hola con todos!!

Quiero realizar esta guia para empezar a traer mas lenguajes de programación y también para traer cosas mas complejas, siempre intentando traer programas faciles como este para que alguien sin experiencia pueda aprender de igual forma.

No me quiero sobreextender asi que dejaré el código e iré explicando con anotaciones el proceso de esta forma me parece mas sencillo.

https://img.freepik.com/free-vector/programmer-work-with-working-day-symbols-flat-illustration_1284-60322.jpg?t=st=1686200378~exp=1686200978~hmac=f82109b7953512f067eb0bb010aee076067da335ac25d26439325fa73511ceab

Si alguna parte no se entiende puedes revisar las guias anteriores donde se eplica los fundamentos o buscar en internet información sobre la parte que deseas profundizar o tienes dudas pero tambíen intento que el código se entienda por si solo.

Aqui tenemos el programa:

#include <iostream>
#include <stdio.h>
#include <conio.h>

using namespace std;

int main() {

int ope, edad;// Variables para inicar menu y guardar edad
int sum1,sum2;//Variables para sumar
int rest1,rest2;//Variables para restar
int result1, result2;//Variables para guardar resultados
&nbsp;
// Imprimimos texto en pantalla con forma de menú

cout<<" ****   Bienvenido al Menu Principal  ****\n";

cout<<"Seleccionar una opcion\n";

cout<<"1 = Ingresar Edad \n";

cout<<"2 = Realizar Suma \n";

cout<<"3 = Realizar Resta \n";

cout<<"4 = Finalizar programa \n";

// leemos del usuario la variable operador desde teclado
cin>>ope;

// Esta anterior variable ope inicializa nuestro switch
// Depende el valor ejecutara cada opcion estas se llaman case
switch(ope){
// Cada caso es una opcion del menú basicamente
// si el usuaior ingresa 1 se ejecuta el primer caso
case 1:  //inicia el caso

cout<<"Selecciono la opcion 1 \n";

cout<<"Digite un numero: ";

cin>>edad;

cout<<"La edad ingresada fue: "<<edad<<endl;

break; //Se acaba el Case 1
&nbsp;
case 2: //inicia el caso

cout<<"Selecciono la opcion 2 para sumar"<<endl;
cout<<"Ingrese el primer numero"<<endl;
cin>>sum1;
cout<<"Ingrese el segundo numero"<<endl;
cin>>sum2;
    result1 = sum1 + sum2;
cout<<"El resultado es : "<<result1<<endl;;

break;//Se acaba el Case 2

case 3: //inicia el caso

cout<<"Selecciono la opcion 3"<<endl;
cout<<"Ingrese el primer numero"<<endl;
cin>>rest1;
cout<<"Ingrese el segundo numero"<<endl;
cin>>rest2;
    result2 = rest1 - rest2;
cout<<"El resultado es : "<<result2<<endl;;

break; //Se acaba el Case 3

case 4: //inicia el caso

cout<<"El programa ha Finalizado";

break; //Se acaba el Case 4

}

return 0;

}

Cualquier duda se reponderá en los comentarios!!, de esta forma también se puede dejar más claro todo!!

https://media.tenor.com/cj8PbXwdLPcAAAAC/thumbs-up-simon-cowell.gif

Eso es Todo Muchas Gracias!!!

Comments

Sort byBest
AI
Waivio AI Assistant
How can I help you today?