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

0

I need to make a program that:

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

However, at the time of displaying the highest value, it goes out wrongly.

Below, the code you've made.

#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;
}
    
asked by anonymous 10.04.2018 / 00:53

1 answer

0

I begin by saying that it should give relevance to the indentation, because it is more important than it seems when it is beginning. Quickly 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:

  • Restart the value of pctot and somaper with each passing of while , so that it is calculated correctly for each pc:

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

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

The complete and 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;
}
    
10.04.2018 / 01:22