Help with a C++ calculator

I'm trying to make a calculator in C++, for some reason the code works, but it doesn't seem to detect the Switch statement. I'm using the console, this is the code (BTW it's in Spanish).

#include <iostream>

using namespace std;

int menu;
int num1, num2;
int resultado;

int main()
{
cout << "Bienvenido a la calculadora \n" << endl;
cout << "Seleccione la operacion deseada con el numero indicado \n" << endl;
cout << "1. Suma" << endl;
cout << "2. Resta" << endl;
cout << "3. Multiplicacion" << endl;
cout << "4. Division" << endl;

switch(menu)
{
case '1':
resultado = num1+num2;
cout << "Introduzca el primer numero" << endl;
cin >> num1;
cout << "Introduzca el segundo numero" << endl;
cin >> num2;
cout << "El resultado es: " + resultado << endl;

}
case '2':
{
resultado = num1-num2;
cout << "Introduzca el primer numero" << endl;
cin >> num1;
cout << "Introduzca el segundo numero" << endl;
cin >> num2;
cout << "El resultado es: " + resultado << endl;
}
case '3':
{
resultado = num1*num2;
cout << "Introduzca el primer numero" << endl;
cin >> num1;
cout << "Introduzca el segundo numero" << endl;
cin >> num2;
cout << "El resultado es: " + resultado << endl;
}
case '4':
{
resultado = num1*num2;
cout << "Introduzca el primer numero" << endl;
cin >> num1;
cout << "Introduzca el segundo numero" << endl;
cin >> num2;
cout << "El resultado es: " + resultado << endl;
}
break;
}
cin.get();
return 0;
}
closed account (Dy7SLyTq)
well it actually does detect it, but you are a)not giving menu a value to be tested against the switch and b)testing it against char values
Could you explain a bit more?.
closed account (jwkNwA7f)
You are not getting the input from the user to menu.
Before the switch add cin >> menu;

Also ,' ' is used for chars. Just use: case 1:

Hope this helped!
closed account (3qX21hU5)
You never assign anything to int menu;. You don't take user input or give a hardcoded value to it, so it is holding a random number inside of it (Whatever was used last in that memory spot) when it reaches the switch statement.

After all your cout statements include a cin statement that assigns whatever the user inputs to the variable menu. Also remove all the ' marks around your cases. They are saying you want to test a char whereas your menu variable is of type integer.

Also please use codetags when you post your code. It was provided for you even when you created the new thread.
Last edited on
It's working perfectly now :D. Thanks!.
Topic archived. No new replies allowed.