How to add the values in a loop?

Asked

Viewed 648 times

0

I want to add and show the total value of the consultation. 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;
  • What’s going wrong? Where are you setting i, valorp and valortotal? Create a MVCE so that we can reproduce the problem.

  • i want this printf("today’s total value was: %.2f n",total value[i]); it shows the total value of queries .... in the above case my for goes up to 3 so the final value would be 1350.... but the result that shows is 0 ... already in the variable qtdpacientes it shows correctly or is the result of 3 that would be the 3 patients

  • @Tiago Resolveu? Do you think it is enough to accept the answer? Have you seen the [tour]

2 answers

2

The code has several errors in addition to well disorganized. It is not getting the name correctly. The total value is one, so there is no reason why it should be a vector. The value of each patient is unique and should not be added up. Something I improved as optimization, when not increasing the number of patients after all already has an increment in the loop. I don’t know if the variables were initialized correctly since you don’t 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);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

-2

getting to the point: total value[i]=total value[i]+value[i];

case 2 :

printf ("Particular: n"); printf("The value of the consultation is R$ 450,00: n"); for(i=0;i<3;i++) { printf("Enter patient’s name:"); scanf("%s",&name[i]); valorp[i]= valorp[i]+450.00; total value[i]=value[i]; qtdpaciente = qtdpaciente +i; }

printf("The number of particulate patients was: %i n", qtdpacient); printf("Today’s total value was: %.2f n",total value[i]);

break;

Browser other questions tagged

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