The program just gives me back the list again, without me being able to enter the values, what am I doing wrong?

Asked

Viewed 26 times

-2

#include <stdio.h>

main()
{
float saldo, valor;
int opc;


printf("Entre com o saldo inicial da conta: ");
scanf("%f", &saldo);

while (opc != 4)
{
    printf("\nBem vindo ao banco.\n");
    printf("1 - Sacar\n");
    printf("2 - Depositar\n");
    printf("3 - Saldo\n");
    printf("4 - Sair\n");

    printf("Opcao: ");
    scanf("%d", &opc);
}
switch (opc)
{
case 1:
    printf("Valor a sacar: ");
    scanf("%f", &valor);

    saldo = saldo - valor;
    break;

case 2:
    printf("Valor que quer depositar: ");
    scanf("%f", &valor);
    saldo = saldo + valor;
    break;
case 3:
    printf("O saldo atual na conta e: %.2f reais.\n", saldo);
    break;
case 4:
    printf("\nSair\n");
    break;

default:
    break;
}

return 0;
}

//He should ask me how much I want to withdraw/deposit, but he keeps just returning me the list of options. For example: Sign in with the opening balance of the account: 1000 /This is from an earlier entry/ Welcome to the bank. 1 - Draw

2 - Deposit

3 - Balance

4 - Quit

Option: /And here he repeats again/

1 answer

1


Your code is correct the problem is the fact the switch is outside the while block, just put it inside the while. The way it is the code only enters the switch when it exits the loop, that is when you type 4.

#include <stdio.h>

main()
{
  float saldo, valor;
  int opc;
  printf("Entre com o saldo inicial da conta: ");
  scanf("%f", &saldo);

  while (opc != 4)
  {
      printf("\nBem vindo ao banco.\n");
      printf("1 - Sacar\n");
      printf("2 - Depositar\n");
      printf("3 - Saldo\n");
      printf("4 - Sair\n");

      printf("Opcao: ");
      scanf("%d", &opc);

    switch (opc)
    {
    case 1:
        printf("Valor a sacar: ");
        scanf("%f", &valor);

        saldo = saldo - valor;
        break;

    case 2:
        printf("Valor que quer depositar: ");
        scanf("%f", &valor);
        saldo = saldo + valor;
        break;
    case 3:
        printf("O saldo atual na conta e: %.2f reais.\n", saldo);
        break;
    case 4:
        printf("\nSair\n");
        break;

    default:
        break;
    }

  }
  return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.