I am trying to compare the word deposit with what the user will type, and if it is equal he will make the deposit

Asked

Viewed 33 times

-1

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

char nome[45], menu[45];
float saldo, valor;

main(){
    printf("Digite seu nome \n");
    scanf("%s", &nome);
    printf("Digite o saldo \n");
    scanf("%f", &saldo);
    printf("Digite o valor \n");
    scanf("%f", &valor);

    printf("Digite uma das opcoes (saldo/deposito/retirada) \n");
    scanf("%s", &menu);

    if(menu=='deposito'){
        saldo=saldo+valor;
        printf("O saldo mais o valor depositado e igual a: %.2f\n", saldo);
    }else if(menu=='retirada'){
        saldo=saldo-valor;
        printf("O saldo menos o valor depositado e igual a: %.2f\n", saldo);
    }else if(menu=='saldo'){
        printf("O saldo e igual a: %.2f\n", saldo);
    }else{
        printf("Erro");
    }

}
  • 1

    Is it C++ right? More like C.

1 answer

0

You cannot compare a char string with an element enclosed by single quotes, i.e.,

menu == 'deposito' // deveria ser:
menu == "deposito"

Here your complete code, I traded the printf for library elements iostream since you said the code is c++ and they are easier to use and understand.

#include <iostream>

std::string nome, menu;
float saldo, valor;

main()
{
    std::cout << "Digite seu nome: \n";
    std::cin >> nome;
    std::cout << "Digite o saldo: \n";
    std::cin >> saldo;
    std::cout << "Digite o valor: \n";
    std::cin >> valor;

    std::cout << "Digite uma das opcoes (saldo/deposito/retirada):\n";
    std::cin >> menu;

    if (menu == 'deposito') {
        saldo = saldo + valor;
        printf("O saldo mais o valor depositado e igual a: %.2f\n", saldo);
    }
    else if (menu == 'retirada') {
        saldo = saldo - valor;
        printf("O saldo menos o valor depositado e igual a: %.2f\n", saldo);
    }
    else if (menu == 'saldo') {
        printf("O saldo e igual a: %.2f\n", saldo);
    }
    else {
        printf("Erro");
    }
}

Browser other questions tagged

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