The error was precisely that @Sveen already commented. Your calculation of saque
is not correct:
saque= valor-saldo;
If you have saldo
100
and valor
50
, your saque
will be -50
. For this reason the instruction should be the opposite:
saque = saldo - valor;
And only with this exchange will work as intended. However it is overly complicated, and does not even need this variable saque
. It's best to compare directly to if
:
}else if (menu == "retirada") {
if(valor <= saldo){ //<----- compara direto aqui
saldo -= valor; //<----- atualiza com o operador -= para ser mais curto
printf("O saldo menos o valor depositado e igual a: %.2f\n", saldo);
}else { //também já não precisa de else if aqui
printf("Nao foi possivel fazer a retirada, pois o valor que voce retirou e maior que o saldo da conta! \n");
}
}
Only valor <= saldo
parison already tells you whether withdrawal is valid or not.
See this example working on Ideone
Recommendations:
- If you always use
std::
it does not make sense to keep using namespace std;
, and it ends up polluting namespace .
- Avoid using global variables, just like you did for all you have in the program
- Keep the declaration of each variable as close as possible to its use instead of declaring everything at the top