How to sum the values in a loop?

0

I want to add and display the total value of the query. In the case of 3 patients the sum would be 1350. But something is going wrong.

case 2 :


printf ("Particular:\n");
printf("O valor da consulta e de R$ 450,00: \n");
for(i=0;i<3;i++)
{
     printf("Digite o nome do paciente:");
     scanf("%s",&nome[i]);
     valorp[i]= valorp[i]+450.00;
     valortotal[i]=valorp[i];
     qtdpaciente = qtdpaciente +i;
}

printf("A quantidade de pacientes particulas foi: %i \n", qtdpaciente);
printf("o valor total de hoje foi: %.2f\n",valortotal[i]);


break;
    
asked by anonymous 26.05.2017 / 16:34

1 answer

2

The code has several errors besides being very disorganized. Not picking up the name correctly. The total value is one, so it does not have to be a vector. The value of each patient is unique and should not be added up. Something I improved as optimization, but not increase the amount of patients after all already has an increase in the loop. I do not know if the variables were initialized correctly since they do not have all the code.

#include <stdio.h>

int main(void) {
    char nome[3][30];
    float valorPaciente[3];
    printf ("Particular:\n");
    printf("O valor da consulta e de R$ 450,00: \n");
    float valorTotal = 0;
    int i;
    for (i = 0; i < 3; i++) {
        printf("Digite o nome do paciente:");
        scanf("%30s", nome[i]);
        valorPaciente[i] = 450.00;
        valorTotal += valorPaciente[i];
    }
    int qtdPaciente = i - 1; //precisa disto mes mo?
    printf("A quantidade de pacientes particulas foi: %i \n", qtdPaciente);
    printf("o valor total de hoje foi: %.2f\n", valorTotal);
}

See running on ideone . And on the Coding Ground (with problems now). Also put it on GitHub for future reference .

    
26.05.2017 / 16:52