Since you want to repeat the data again, then it is best to use a do-while
loop that will guarantee that the data is requested at least once.
On the other hand, the following is half ugly: "Que operación deseas hacer, 1(suma), 2(producto) , 3 (division), 4 (resta)/n" ;
besides that at the end it should be \n
instead of /n
. In these cases it is better to create a menu of options in the following way:
cout << "Que operacion deseas hacer?\n";
cout << "1. Suma\n";
cout << "2. Producto\n";
cout << "3. Division\n";
cout << "4. Resta\n";
In turn, when the user selects an option, we proceed to validate it using another do-while
in the following way:
do {
cout << "Seleccione una de las opciones anteriores:\t";
cin >> opcion;
if (opcion <= 0 || opcion > 4)
cout << "ERROR. La opcion no existe.\n";
} while (opcion <= 0 || opcion > 4);
By the way, I have changed the variable c
, which does not tell me anything, by the variable opcion
, since the last one is much clearer in its meaning. Remember that programs must be written as legibly as possible.
Finally, the if
is fine ... in principle. Your logic works, but it is always better to do something that is more optimal. In this case, it is better to make use of the selective control structure switch
. The syntax is quite simple, every if(...)
will become a case
.
More concretely, we have the following:
switch(opcion)
{
case 1: {
cout << "El resultado de suma es:\t" << num1 + num2 << '\n';
} break;
case 2: {
cout << "El resultado de producto es:\t" << num1 * num2 << '\n';
} break;
case 3: {
cout << "El resultado de division es:\t" << num1 / num2 << '\n';
} break;
case 4: {
cout << "El resultado de resta es:\t" << num1 - num2 << '\n';
} break;
}
Here I have changed your variables again. In this case a
and b
for the most descriptive num1
and num2
. We do not need the default
because we have previously validated that the options are in the [1-4]
range.
This part of my solution may be a bit controversial, since I do not usually use and / or recommend using the system
function (which is in the header <cstdlib>
), however, in A little exercise like this, I see nothing wrong. In any case, I recommend you read the following link (in English).
We will use system("cls")
that will make the screen clean. Since the process is immediate, it will not give us time to see the result of the chosen operation with our pair of numbers entered. Therefore, before cleaning the screen I use the function Sleep
of the header <Windows.h>
. This is also not recommended, because it is not multiplatform. As you can deduce from the name, the above only works in Windows. There is a multiplatform way using the function sleep_for
of the header <thread>
of the C ++ standard, but it is something a little more complicated and I do not want to make your code more complex.
Finally, all the code with the modifications already mentioned:
#include <iostream>
#include <cstdlib>
#include <Windows.h>
using namespace std;
int main ()
{
double num1, num2;
int opcion;
do {
cout << "Introduzca el primer numero:\n";
cin >> num1;
cout << "Introduzca el segundo numero:\n";
cin >> num2;
cout << "Que operacion deseas hacer?\n";
cout << "1. Suma\n";
cout << "2. Producto\n";
cout << "3. Division\n";
cout << "4. Resta\n";
do {
cout << "Seleccione una de las opciones anteriores:\t";
cin >> opcion;
if (opcion <= 0 || opcion > 4)
cout << "ERROR. La opcion no existe.\n";
} while (opcion <= 0 || opcion > 4);
switch(opcion)
{
case 1: {
cout << "El resultado de suma es:\t" << num1 + num2 << '\n';
} break;
case 2: {
cout << "El resultado de producto es:\t" << num1 * num2 << '\n';
} break;
case 3: {
cout << "El resultado de division es:\t" << num1 / num2 << '\n';
} break;
case 4: {
cout << "El resultado de resta es:\t" << num1 - num2 << '\n';
} break;
}
Sleep(1000); //Puedes poner otro valor (esta función toma milisegundos)
system("cls");
} while (opcion != 5);
return 0;
}
I hope it serves you.