Doubt about code in C

Asked

Viewed 153 times

0

Hello. I’m having doubts about this code.

In "case 1", when executing, I see this:

The problem is that none of the discounts is applied. I have tried other ways to mount this operation, but all result in error.

In "case 2", I wanted to know if there is a way to, "if (c==987)" turn " if (c==0987)", without errors referring to the octal basis. And, preferably, it has to be the way it is in the code, since it was the way the teacher instructed us to do, initially.

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

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

int quant, questoes,codigo;
float preco, valor, calculo, desconto, salario, valor1, valor2, valor3;
char nome_produto;

puts("----- RESOLUÇÃO DE PROBLEMAS -----");
puts("Digite o número da questão desejada:\n1 - Desconto em Produtos\n2 - Códigos de Produtos \n3 - Rejuste de Salário \n4 - Maior valor \n5 - Soma de Valores ");
scanf("%d",&questoes);

       switch(questoes){
                        case 1:
                                system("cls");
                                puts("Digite o nome do produto");
                                scanf(" %[^\n]c ",&nome_produto);
                                puts ("Digite a quantidade adquirida");
                                scanf ("%i",&quant);
                                puts ("Digite o preço do produto");
                                scanf ("%f",&preco);
                                if ("quant<=10") {
                                                  calculo = preco * quant;
                                                  printf ("Seu produto não pode obter desconto. O preço total é de: %.2f",calculo);
                                              }
                                else if ("quant<=20") {
                                                  desconto = preco * quant * 10/100;
                                                  calculo=preco*quant-desconto;
                                                  printf ("Seu produto obteu um desconto de 10%. O preço total é de: %.2f",calculo);
                                  }
                                else if ("quant<=50") {
                                                  desconto = preco * quant * 20/100;
                                                  calculo = preco * quant - desconto;
                                                  printf ("Seu produto obteu um desconto de 20%. O preço total é de: %.2f",calculo);
                                  }
                                else if ("quant>50") {
                                                  desconto = preco * quant * 25/100;
                                                  calculo = preco * quant - desconto;
                                                  printf ("Seu produto obteu um desconto de 25%. O preço total é de: %.2f",calculo);
                                  }
                        break;

                        case 2:
                                system("cls");
                                puts("Digite o código do produto");
                                scanf("%i",&codigo);
                                puts("Digite a quantidade adquirida");
                                scanf("%i",&quant);
                                if (codigo==1001) {
                                    calculo=quant*5.32;
                                    printf("O valor total é %.2f",calculo);
                                }
                                if (codigo==1324) {
                                    calculo=quant*6.45;
                                    printf("O valor total é %.2f",calculo);
                                }
                                if (codigo==6548) {
                                    calculo=quant*2.37;
                                    printf("O valor total é %.2f",calculo);
                                }
                                if (codigo==987) {
                                    calculo=quant*5.32;
                                    printf("O valor total é %.2f",calculo);
                                }
                                if (codigo==7623) {
                                    calculo=quant*6.45;
                                    printf("O valor total é %.2f",calculo);
                                }
                                else
                                    puts("Código Inválido");
                        break;

                        case 3:
                                system("cls");
                                puts("Digite o código funcional");
                                scanf("%i",&codigo);
                                puts("Digite o salário atual do funcionário");
                                scanf("%f",&salario);
                                if (codigo==01) {
                                    calculo=salario*1.15;
                                    printf("O salário atual, após o reajute de 15 por cento é de: %.2f. Seu cargo é de operador.",calculo);
                                }
                                if (codigo==02) {
                                    calculo=salario*1.1;
                                    printf("O salário atual, após o reajute de 10 por cento é de: %.2f. Seu cargo é de supervisor.",calculo);
                                }
                                if (codigo==03) {
                                    calculo=salario*1.05;
                                    printf("O salário atual, após o reajute de 5 por cento é de: %.2f. Seu cargo é de gerente.",calculo);
                                }
                                if (codigo==04) {
                                    printf("O salário atual é de: %.2f. Seu cargo é de diretor.",salario);
                                }
                                else
                                    puts("Código Inválido");

                        break;

                        case 4:
                              system("cls");
                              puts("Entre com o primeiro valor");
                              scanf("%f",&valor1);
                              puts("Entre com o segundo valor");
                              scanf("%f",&valor2);
                              puts("Entre com o terceiro valor");
                              scanf("%f",&valor3);
                              if (valor1>valor2, valor1>valor3) {
                                puts("O primeiro valor é maior");
                              }
                              else if (valor2>valor1, valor2>valor3) {
                                puts("O segundo valor é maior");
                              }
                              else if (valor3>valor1, valor3>valor2) {
                                puts("O terceiro valor é maior");
                              }
                              else
                                puts("Os valores são iguais");
                        break;

                        case 5:
                              system("cls");
                              puts("Entre com o primeiro valor");
                              scanf("%f",&valor1);
                              puts("Entre com o segundo valor");
                              scanf("%f",&valor2);
                              puts("Entre com o terceiro valor");
                              scanf("%f",&valor3);
                              if (valor1>valor3, valor2>valor3) {
                                calculo=valor1+valor2;
                                printf("%.0f + %.0f = %.0f",valor1,valor2,calculo);
                              }
                              else if (valor1>valor2, valor3>valor2) {
                                calculo=valor1+valor3;
                                printf("%.0f + %.0f = %.0f",valor1,valor3,calculo);
                              }
                              else if (valor3>valor1, valor2>valor1) {
                                calculo=valor2+valor3;
                                printf("%.0f + %.0f = %.0f",valor2,valor3,calculo);
                              }
                              else
                                puts("Os valores são iguais");
                        break;

                        default:
                                puts ("Opção Inválida");
                        }

}

To clarify, all this is part of academic questions. I am learning now. I will leave the command corresponding to case 1 below:

Case 1

  1. Make a program that reads a product name, price and quantity purchased. Write the name of the product purchased and the total amount to be paid, considering that discounts are offered by the number of units purchased, according to the data below:

    • Up to 10 units - total value

    • From 11 to 20 units - 10% discount

    • From 21 to 50 units - 20% discount

    • Above 50 units - 25% discount

  • %d, &c ? You are playing a decimal binary encoding of (possibly) 4 bytes on an address interpreted as a 4 byte IEEE floating point, this reading will not return a reliable result

  • Hello. I added the commands of the questions. I hope you are of better understanding now.

2 answers

0

Case 1

You have to print the message on the screen to type name. Then use the scanf to receive the result. Then print on the screen the message to type price. Then use scanf to receive the result.

Case 2

First thing you should do that will help you and help others read your code is to put understandable names in your variables. Names like a,x,y,z don’t help at all to understand what’s going on. Put names like "product", "quantity", "price".

Well, in case two I won’t solve for you because it’s an academic issue, but I’ll give you the steps to resolve the issue:

1- Ask the user to enter the product code and receive the value 2- Ask him to enter the quantity and receive the value 3- Make a switch to pick the price of the product he chose, only the price. You don’t have to put the IFS inside the switch or else you’ll be repeating code. Something like this oh:

double preco;

switch(codigo) {

    case 1001:
        preco = 5.32;

E ASSIM POR DIANTE....

4- After that create the quantity check. If she goes up to ten take the quantity and multiply by the price, if it is from 11 to 20 makes quantity times price minus 20% and so on.

Well I hope you can solve it but it’s very important that you solve it yourself because it will help you immensely in the future.

  • Hello. Well, as far as case 1 is concerned, I edited the post, since now the error is different. But I figured out why I couldn’t get characters in. Case 2, I got it to work, but I still have the problem with the octal base. Oh, and thanks for the tip! I’ve already modified the code, it really makes reading easier.

  • Take this if-lse from inside the switch and leave it just to determine equal price I showed you in the reply. After the switch makes your if just to see the discount. if(price <= 10) { has no discount } Else if (price >10 && price <= 20) { precoFinal = blablablabla } Got it?

  • Of that form?

  • This and its octal error is that ta starting a variable with 0 as the first number and when starting it with it is treated as base number 8. Remove the 0 from the front.

  • Yes, that was my question. The problem is that the activity asks for this code. I think the best thing would be to inform myself with my teacher. Anyway, thank you very much.

  • Turns your code into a string or instead of an integer that solves the problem

Show 1 more comment

0

A tip for you to solve this problem is to determine a variable for unit and price.

Example:

float quantidade = 0;
float preco = 0;

So,

You take the product code, check the price of the product and increment this value in the price variable, and increment 1 in the quantity variable.

At the end just check.

Browser other questions tagged

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