1.11M Members

C program to calculate number of days between 2 dates using structure of pointers....

 
0
 
/*This program is going infinite.PLS help*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d,e,f,flag=0;
printf("Enter first date");
scanf("%d%d%d",&a,&b,&c);
printf("Enter second date");
scanf("%d%d%d",&d,&e,&f);
const int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
while(c!=f||b!=e||a!=d)

{
	a++;
	if((b==4)||(b==6)||(b==9)||(b==11))
		f=30;
	else if(b==2)
	{
		if(d%4==0)
			f=29;
		else
			f=28;
	}
	else
		f=31;
	if(a>f)
	{
		b++;
		a=1;
	}
	if(b==13)
	{
		c++;
		b=1;
	}
	flag++;

}
	printf("%d",flag);
	getch();
}
 
0
 

First of all: please use codetags. It makes your code easier to read:

#include<stdio.h>

int main()
{
	int a,b,c,d,e,f,flag=0;
	printf("Enter first date");
	scanf("%d%d%d",&a,&b,&c);
	printf("Enter second date");
	scanf("%d%d%d",&d,&e,&f);
	const int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
	while(c!=f||b!=e||a!=d)

	{
		a++;
		if((b==4)||(b==6)||(b==9)||(b==11))
			f=30;
		else if(b==2)
		{
			if(d%4==0)
				f=29;
			else
				f=28;
		}
		else
			f=31;
		if(a>f)
		{
			b++;
			a=1;
		}
		if(b==13)
		{
			c++;
			b=1;
		}
		flag++;

	}
	printf("%d",flag);
	getchar();
}

I've replaced getch with getchar. void main with int main and removed conio. This makes your code standard for all compilers.
You might also want to use some usefull names for your vars. A,b,c, etc is not very practical.

Now for your problem:

while(c!=f||b!=e||a!=d)

{
a++;

What happens if a > d ? -> It becomes a while(1) loop. And that's just 1 problem of the many. What is this code supposed to do?

 
0
 

Suppose a>d.
But the problem is while loop is infinite even if a<d

 
0
 

What is this code supposed to do?

Still don't know

 
0
 

hey i also solved in he same manner

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d,e,f,flag=0;
printf("Enter first date");
scanf("%d%d%d",&a,&b,&c);
printf("Enter second date");
scanf("%d%d%d",&d,&e,&f);
const int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
while(c!=f||b!=e||a!=d)

{
a++;
if((b==4)||(b==6)||(b==9)||(b==11))
f=30;
else if(b==2)
{
if(d%4==0)
f=29;
else
f=28;
}
else
f=31;
if(a>f)
{
b++;
a=1;
}
if(b==13)
{
c++;
b=1;
}
flag++;

}
printf("%d",flag);
getch();
}
 
-2
 

whenever u use like this (c!=f||b!=e||a!=d) if checks only c!=f won't go for remaing part which is always 1 which results infinite loop...
want more??? my id [email snipped]

 
0
 

Any one can explain the logic .... I am not able to understand the logic....

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article