Call a specific function as you choose from the menu

Asked

Viewed 2,462 times

1

I need to make a program that simulates a bank ATM in C++, presenting a menu that has Deposit, Withdrawal and Payment of Tickets. How am I going to do that? I’ve done the following:

#include <iostream>
using namespace std;

bool ValidaNumeroAgencia (int agencia)
{
    if (agencia >= 100 && agencia <= 999)
        return true;
    else
        return false;
}
bool ValidaNumeroConta_Corrente (int conta)
{
    if (conta >= 10000 && conta <= 99999)
        return true;
    else
        return false;
}
float Deposito (float credito)
{
    float novo_valor;
    novo_valor = 500 + credito;
    return novo_valor;
}
int Saque (int debito, int saldo)
{
    if (debito <= saldo)
    {
        if (debito % 10)
            return debito;
    }
}
bool ValidaClienteEspecial (int saldo, int limite)
{
    int n_limite;
    if (saldo >= 10000 && ValidaClienteEspecial == false)
    {
        n_limite = limite + 5000;
        return true;
        cout << "Seu novo limite e de: " << n_limite << endl;
    }
}
float Codigo_barras(int codigo, float valor, int saldo)
{
    float n_saldo;
    if (codigo >= 100000000000 && codigo <= 999999999999)
    {
        if (valor <= saldo)
        {
            n_saldo = saldo - valor;
            return n_saldo;
        }
    }
}
float Saldo (float saldo)
    {
        cout << "Seu saldo e de:\n" "R$ " << saldo << endl;
    }

int main ()
{
    int n_agencia, n_conta_corrente, n, a;

    ValidaNumeroAgencia(n_agencia);
    ValidaNumeroConta_Corrente(n_conta_corrente);

    do
    {
        cout << "Digite o Numero da sua Conta Corrente: ";
        cin >> n_conta_corrente;
    }
    while (ValidaNumeroConta_Corrente(n_conta_corrente) == false);

    do
    {
        cout << "Digite o Numero da sua Agencia: ";
        cin >> n_agencia;
    }
    while (ValidaNumeroAgencia(n_agencia) == false);

    cout << "\n\nSelecione qual opcao deseja realizar: ";
    cout << "\n1 - Deposito";
    cout << "\n2 - Saque";
    cout << "\n3 - Pagamento de Boletos";
    cout << "\n4 - Extrato";
    cout << "\n0 - Sair\n";
 return 0;
}

My question is, what will I do for the program to call the functions according to what the user choose in the menu? How can I continue the code above?

  • 1

    Give more details. What have you done? Where specifically is your question?

  • I have already created some functions such as asking and validating the current account, the number of the user agency, etc... I printed a simple menu, look here: Cout << " n nSelect which option you want to perform: "; Cout << " n1 - Deposit"; Cout << " N2 - Withdrawal"; Cout << " N3 - Billet payment"; Cout << " N4 - Extract"; Cout << " N0 - Exit n"; And now I’m trying to do by switch to call each of the functions, but I’m not getting... Gives a tip ai pfv, q I can do. Thank you.

  • 1

    Edit the question by adding this code and where the doubt is. This makes it easier to help.

  • How do I put the code here in the comments? It is pq I entered the site has little time... kkk

  • 1

    Edit your question. Click Editar below the question.

  • 1

    I’ve already edited... Help???

  • Check the range of values you can represent with an int. In the case of 32 bits the range is: -2.147.483.648 to 2.147.483.647. You set the code variable to int and test: if (code >= 100000000000 && code <= 999999999999999).

Show 2 more comments

1 answer

3


You can solve this basically with a switch:

cout << "\n\nSelecione qual opcao deseja realizar: ";
cout << "\n1 - Deposito";
cout << "\n2 - Saque";
cout << "\n3 - Pagamento de Boletos";
cout << "\n4 - Extrato";
cout << "\n0 - Sair\n";

int opcao;

// Repete até que o usuário peça para sair.
while (opcao != 5)
{
    std::cin >> opcao;

    switch (opcao)
    {
        case 1:
            // Pedo ao usuário a quantidade que será depositada.
            // E depois chama a função de deposito:
            // Exemplo:
            // Deposito(1000);
            break;
        case 2:
            // Pedo ao usuário a quantidade que será sacada.
            // E chama a função:
            // Saque(1000);
            break;
        case 3:
            // Pede os valores do boleto e chama a função Codigo_barras.
            break;
        case 4:
            // Chama a função para mostrar o extrato.
            break;
        case 5:
            // Usuário quer sair.
            break;
        default:
            // Se o usuário informar um valor inválido.
            std::cout << "Opcao invalida! Informe outro valor." << std::endl;
            break;
    }

Just one observation: It has some functions of its that do not always return a value. For example:

int Saque (int debito, int saldo)
{
    if (debito <= saldo)
    {
        if (debito % 10)
            return debito;
    }
}

If the debito <= saldo for false what the function Saque will you return? Gives a fix on this, so that the functions always return expected values.

  • Thank you very much, man! Vlw msm...

Browser other questions tagged

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