Validation of strings in C

Asked

Viewed 36 times

0

Good afternoon, I am learning C in college, and the following challenge has been proposed:

  1. A seller needs an algorithm that calculates the total price due for a customer. The algorithm must receive the code of a product and the quantity purchased and calculate the total price using the table below. Display a message in case of invalid code.

       Código    Preço Unitário
    
       "ABCD"    R$ 5,30
    
       "XYPK"    R$ 6,00
    
       "KLMP"    R$ 3,20
    
       "QRST"    R$ 2,50
    

I realized that with switch would not be possible because the cases support only one character, so I built this structure with if:

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

int main()
{
setlocale(LC_ALL, "Portuguese");

char codigo;
int quantidade=0;
float preco= 0.00;

printf("Digite o código do produto: ");
scanf("%s", &codigo);



printf("Quantidade: ");
scanf("%i", &quantidade);

if(codigo == "ABCD"){
    preco = quantidade * 5.30;
    printf("O preço total é de R$%f", preco);
}else{
    if(codigo == "XYPK"){
        preco = quantidade * 6.00;
        printf("O preço total é de R$%f", preco);
    }
    else if(codigo == "KLMP"){
        preco == quantidade * 3.20;
        printf("O preço total é de R$%f", preco);
    }
    else if(codigo == "QRST"){
        preco == quantidade * 2,50;
        printf("O preço total é de R$%f", preco);

    }
}

Return 0; }

It turns out that the condition is not validated regardless of what I put in the scanf, can anyone help me with this situation? My program does not enter any IF!

No answers

Browser other questions tagged

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