My program is not adding up the data it receives. Program in C

Asked

Viewed 61 times

0

I need to do a program that:

  • Get the pc model, its price and display the highest value, plus the amount of computers without peripherals.
  • In the main function, receive the amount of peripherals.
  • A function that receives as a parameter the number of peripherals, make the sum of their prices.
  • display the quantity of sales without additional peripherals and the value of the highest sale.

However, when displaying the highest value, it goes wrong.

Below, the code performed.

#include <stdio.h>

float pg_peri (int qtdper){
    float precoperi;


            printf ("Digite o valor do periferico:\n");
            scanf ("%f",&precoperi);
    return precoperi;

    }



int main (){
    float precomod, maior, pctot, pcperi, somaper;
    int cont, qtdperi1, codmod, vdperi, qtdmodelos, cc;
    somaper = 0;
    vdperi = 0;
    cont = 0;
    maior = 0;
    printf ("Digite quantos computadores deseja comprar\n");
    scanf ("%d", &qtdmodelos);
    while (cont < qtdmodelos){
            printf ("Digite o codigo do modelo desejado e seu preco\n");
            scanf ("%d%f", &codmod,&precomod);
            if (codmod == 0){
                            return 1;
            }
            printf ("Digite a quantidade de perifericos que você precisa\n");
            scanf ("%d",&qtdperi1);
            cc = 0;
                while (cc < qtdperi1){
                    pcperi = pg_peri(qtdperi1);
                    somaper = pcperi + somaper;
                    cc++;
        }
                            if (qtdperi1 == 0){
                                vdperi++;
            }

                            pctot = pcperi + precomod;
                                    if (maior < pctot ){
                                    maior = pctot;
            }


                cont++;

            }
            printf ("A quantidade de vendas sem peri \n%d e o maior eh de \n%.2f", vdperi, maior);
            return 0;
}

1 answer

0

I start by saying that it should give relevance to indentation, because it’s more important than it looks when it’s starting. Quickly you find the code easier to read and organized, and the same for third parties who want to read your code.

As for the code two things were missing:

  • Reset the value of pctot and somaper every passage of while, so that it is calculated correctly every pc:

    while (cont < qtdmodelos)
    {
        pctot = 0; // <-----
        somaper = 0; // <-----
        printf ("Digite o codigo do modelo desejado e seu preco\n");
        ....
    
  • Increase pc value with the sum of all peripherals instead of the last peripheral value :

    pctot = somaper + precomod; //em vez de pctot = pcperi + precomod;
    

The full, indented code would look like this:

#include <stdio.h>

float pg_peri (int qtdper) {
    float precoperi;
    printf ("Digite o valor do periferico:\n");
    scanf ("%f",&precoperi);
    return precoperi;
}

int main () {
    float precomod, maior, pctot, pcperi, somaper;
    int cont, qtdperi1, codmod, vdperi, qtdmodelos, cc;
    somaper = 0;
    vdperi = 0;
    cont = 0;
    maior = 0;
    printf ("Digite quantos computadores deseja comprar\n");
    scanf ("%d", &qtdmodelos);
    while (cont < qtdmodelos) {
        pctot = 0; // <------
        somaper = 0; // <-----
        printf ("Digite o codigo do modelo desejado e seu preco\n");
        scanf ("%d%f", &codmod,&precomod);
        if (codmod == 0) {
            return 1;
        }
        printf ("Digite a quantidade de perifericos que você precisa\n");
        scanf ("%d",&qtdperi1);
        cc = 0;
        while (cc < qtdperi1) {
            pcperi = pg_peri(qtdperi1);
            somaper = pcperi + somaper;
            cc++;
        }
        if (qtdperi1 == 0) {
            vdperi++;
        }

        pctot = somaper + precomod;  // <-----
        if (maior < pctot) {
            maior = pctot;
        }

        cont++;
    }
    printf ("A quantidade de vendas sem peri \n%d e o maior eh de \n%.2f", vdperi, maior);
    return 0;
}
  • Really, I realize that my code is very poorly formulated and it greatly disturbs my understanding of it. Thanks for the help, I need to understand better about this type of repetition. About the reset in while, every time I do a separate sum, I must put the null variable in that structure?

  • @Matheuscoelho I said this to be something you should think about getting better too. 'Cause a lot of times the one who’s starting ends up thinking that it has no relevance, when it has.

Browser other questions tagged

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