1,105,534 Community Members

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

Member Avatar
BHUJI
Newbie Poster
4 posts since May 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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();
}
Member Avatar
Nick Evan
Industrious Poster
4,827 posts since Oct 2006
Reputation Points: 4,005 [?]
Q&As Helped to Solve: 560 [?]
Skill Endorsements: 30 [?]
Team Colleague
Featured
 
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?

Member Avatar
BHUJI
Newbie Poster
4 posts since May 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Nick Evan
Industrious Poster
4,827 posts since Oct 2006
Reputation Points: 4,005 [?]
Q&As Helped to Solve: 560 [?]
Skill Endorsements: 30 [?]
Team Colleague
Featured
 
0
 

What is this code supposed to do?

Still don't know

Member Avatar
carljonson07
Newbie Poster
5 posts since Nov 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
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();
}
Member Avatar
ramagiripratap
Newbie Poster
1 post since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-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]

Member Avatar
anand01
Posting Whiz
301 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 23 [?]
Skill Endorsements: 0 [?]
 
0
 

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

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