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

Reply

Join Date: May 2006
Posts: 4
Reputation: BHUJI is an unknown quantity at this point 
Solved Threads: 0
BHUJI BHUJI is offline Offline
Newbie Poster

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

 
0
  #1
Nov 14th, 2007
/*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();
}
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,841
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 298
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Roasting Maven

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

 
0
  #2
Nov 14th, 2007
First of all: please use codetags. It makes your code easier to read:

  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5. int a,b,c,d,e,f,flag=0;
  6. printf("Enter first date");
  7. scanf("%d%d%d",&a,&b,&c);
  8. printf("Enter second date");
  9. scanf("%d%d%d",&d,&e,&f);
  10. const int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
  11. while(c!=f||b!=e||a!=d)
  12.  
  13. {
  14. a++;
  15. if((b==4)||(b==6)||(b==9)||(b==11))
  16. f=30;
  17. else if(b==2)
  18. {
  19. if(d%4==0)
  20. f=29;
  21. else
  22. f=28;
  23. }
  24. else
  25. f=31;
  26. if(a>f)
  27. {
  28. b++;
  29. a=1;
  30. }
  31. if(b==13)
  32. {
  33. c++;
  34. b=1;
  35. }
  36. flag++;
  37.  
  38. }
  39. printf("%d",flag);
  40. getchar();
  41. }

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:
  1. while(c!=f||b!=e||a!=d)
  2.  
  3. {
  4. 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?

Niek
Last edited by niek_e; Nov 14th, 2007 at 6:07 am.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 4
Reputation: BHUJI is an unknown quantity at this point 
Solved Threads: 0
BHUJI BHUJI is offline Offline
Newbie Poster

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

 
0
  #3
Nov 14th, 2007
Suppose a>d.
But the problem is while loop is infinite even if a<d
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,841
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 298
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Roasting Maven

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

 
0
  #4
Nov 14th, 2007
Originally Posted by Myself View Post
What is this code supposed to do?
Still don't know

Niek
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 5
Reputation: carljonson07 is an unknown quantity at this point 
Solved Threads: 2
carljonson07 carljonson07 is offline Offline
Newbie Poster

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

 
0
  #5
Nov 14th, 2007
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();
}
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC