counting how many days left to finish the year

0

I was doing a program in c ++ to count how many days left to finish the year from a date in the format dd / mm / yyyy but according to the date of the month that I enter it always gives the same result type: / p>

If I enter 18/02/2018 or 12/02/2018 it says that 340 days are missing or if I enter 03/14/2018 or 03/20/2017 it shows that 275 days are missing. Why is he doing this?

The program is this below.

#include <stdio.h>


int main ()
{
 int falta_dias = 0;
 int dia, mes, ano;
 int dias_mes[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

 do{
    printf("Digite uma data no formato dd/mm/yyyy: ");
    scanf("%d/%d/%d", &dia, &mes, &ano);

    if(dia > 31 || dia < 1)printf("\n\tDia %d invalido.!!\n\tDigite um dia de 01 a 31\n",dia);

    if(mes > 12 || mes < 1)printf("\n\tMes %d invalido.!!\n\tDigite um mes de 01 a 12\n",mes);

   }while((dia > 31 || dia < 1) || (mes > 12 || mes < 1));

   dias_mes[1] = (ano%4 == 0 || ano%400 == 0 && ano%100 != 0) ? 29 : 28;

   for(int i = mes; i<12; i++)
   falta_dias += dias_mes[i];

   printf("\n\nFaltam %d dias para terminar o ano %04d.\n\n", falta_dias,ano);

 return 0;
}
    
asked by anonymous 19.02.2018 / 13:02

1 answer

3

You are only counting the days from the next month to which you indicate in the default entry (your for(int i = mes; i<12; i++) loop). Add the remaining days in the chosen month to your code:

for(int i = mes; i<12; i++)
falta_dias += dias_mes[i];

falta_dias+=dias_mes[mes-1] - dia; // conta os dias restantes do mes indicado na entrada padrão
printf("\n\nFaltam %d dias para terminar o ano %04d.\n\n", falta_dias,ano);
    
19.02.2018 / 13:28