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;
}
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.