1
Can anyone tell what I’m doing wrong in my code?
1) Make a program that determines the chronologically larger date between two dates provided by the user. Each date shall be composed of three integer values, in which the first represents the day, the second, the month and the third, the year.
Here’s what I tried:
#include <stdio.h>
#include <locale.h> //Biblioteca locale é uitlizada para definar o idioma do programa.
int main()
{
setlocale(LC_ALL,"portuguese"); //Comando utilizado para caracteres especiais aparecerem.
int dia,mes,ano;
int dia2,mes2,ano2;
printf("Comparador de datas \n\n");
printf("Primeira data \n");
printf(" Digite o dia (1 a 31): ");
scanf("%d",&dia);
printf(" Digite o mês (1 a 12): ");
scanf("%d",&mes);
printf(" Digite o ano: ");
scanf("%d",&ano);
printf("\n");
printf("Segunda data \n");
printf(" Digite o dia (1 a 31): ");
scanf("%d",&dia2);
printf(" Digite o mês (1 a 12): ");
scanf("%d",&mes2);
printf(" Digite o ano: ");
scanf("%d",&ano2);
//Inicio compartivo de anos
if (ano > ano2){
printf("\n\n");
printf("A maior data é %d/%d/%d \n",dia,mes,ano);
}
else if (ano2 > ano) {
printf("\n\n");
printf("A maior data é %d/%d/%d \n",dia2,mes2,ano2);
printf("\n\n");
}
//
//Inicio compartivo dos meses
if (mes > mes2) {
if (ano > ano2) {
printf("\n\n");
printf("A maior data é %d/%d/%d",dia,mes,ano);
printf("\n\n");
}
else if( ano2 > ano); {
printf("\n\n");
printf("A maior data é %d/%d/%d",dia2,mes,ano2);
printf("\n\n");
}
}
else if (mes2 > mes) {
if (ano > ano2) {
printf("\n\n");
printf("A maior data é %d/%d/%d",dia,mes,ano);
printf("\n\n");
}
else if (ano2 > ano); {
printf("\n\n");
printf("A maior data é %d/%d/%d",dia2,mes2,ano2);
printf("\n\n");
}
}
//
//Inicio compartivo dos dias
if (dia > dia2) {
if (ano > ano2) {
printf("\n\n");
printf("A maior data é %d/%d/%d",dia,mes,ano);
printf("\n\n");
}
else if( ano2 > ano); {
printf("\n\n");
printf("A maior data é %d/%d/%d",dia,mes,ano2);
printf("\n\n");
}
}
else if (dia2 > dia) {
if (ano > ano2) {
printf("\n\n");
printf("A maior data é %d/%d/%d",dia2,mes,ano);
printf("\n\n");
}
else if (ano2 > ano); {
printf("\n\n");
printf("A maior data é %d/%d/%d",dia2,mes2,ano2);
printf("\n\n");
}
}
}
it’s actually simple... you’re not finishing the program after you answer.. and all the blocks
if
are running, so for each of them a response is being shown on the screen. Besides you are mixing the variables inprintf
– Israel Merljak
another way that you could verify is simply by adding up the values
(ano + mes + dia)
and checking which one is the biggest.. this would simplify your code to just one blockif
– Israel Merljak
MDS! It is true, as I had not thought of it, to hours suffering with this code. Thank you Irsael
– Thiago Henrique Domingues
The idea of Israel is good, but if it is naively done give pa problem. The sum should be made considering the quantities of each part of the date, which is much more complicated than it seems because of the irregularity of dates. , reaching the point that it ends up being simpler to convert to string and concatenate everything, even if less elegant and performatic.
– Maniero
Of course the current code is naive and accepts days 29, 30 and 31 in months that do not have this amount of days (well, in practice, it accepts even numbers completely out as millions or negatives). If accepting to do wrong at least eliminates the irregularity, then you can consider the day as a unit, the month as ten (multiplies by 31 in the full months, makes -1) and the year would be like the hundred (multiply by 372 in full years, so you have to do minus 1, obviously it gets wrong, but the basis is already wrong). In the end it would be complex too, because the problem is complex.
– Maniero