this code isn't working...why..plz help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2004
Posts: 6
Reputation: sweety is an unknown quantity at this point 
Solved Threads: 0
sweety sweety is offline Offline
Newbie Poster

this code isn't working...why..plz help

 
0
  #1
Nov 2nd, 2004
  1. // PROGRAM TO FIND THE NEXT DATE OF A GIVEN DATE
  2.  
  3. #include<iostream.h>
  4. #include<conio.h>
  5.  
  6. class udate
  7. {
  8.  
  9. int day, month, year;
  10. public:
  11. void read()
  12. {
  13. cin>>day>>month>>year;
  14. }
  15. void write()
  16. {
  17. cout<<day<<"/"<<month<<"/"<<year;
  18. }
  19. friend int valid(udate);
  20. void operator++();
  21. };
  22.  
  23. int valid(update D)
  24. {
  25. int d = D.day;
  26. int m = D.month;
  27. int y = D.year;
  28.  
  29. if(d <= 0|| d>31 || m <= 0 || m>12 || y<=0)
  30. return(0);
  31. else
  32. if(( m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12) && d>31)
  33. return(0);
  34. else
  35. if(( m==4 || m==6 || m==9 || m==11) && d>30)
  36. return(0);
  37. else
  38. if( m==2 )
  39. {
  40.  
  41. if((y%100==0 && y%400==0 && d>29)
  42. || (y%100==0 && y%400!=0 && d>28))
  43. return(0);
  44. else
  45. if((y%4==0 && d>29) || (y%4!=0 && d>28))
  46. return(0);
  47. }
  48.  
  49. return(1);
  50. }
  51.  
  52. void udate :: operator ++()
  53. {
  54.  
  55. if(month==2)
  56. {
  57. if((y%100==0 && year%400==0 && day==29) || (year%100==0 && year%400!=0 && day==28))
  58. {
  59. day=1;
  60. month ++;
  61. }
  62.  
  63. else
  64. if(year%4==0 && day==29)||(year%4!=0 && day==28))
  65. {
  66. day=1;
  67. month++;
  68. }
  69.  
  70. else
  71. day++;
  72. }
  73.  
  74. else
  75. if((month==1 || month==3 || month ==5 || month==7
  76. || month==8 || month==10 || month==12) && day==31)
  77. {
  78. day=1;
  79. if(month==12)
  80. {
  81. month = 1;
  82. year++;
  83. }
  84. else
  85. month++;
  86. }
  87.  
  88. else
  89. {
  90. if((month==4 || month==6 || month==9 || month==11) && day == 30)
  91. {
  92. day=1;
  93. month++;
  94. }
  95. else
  96. day++;
  97. }
  98. }
  99.  
  100. void main()
  101. {
  102. udate d1;
  103. clrscr();
  104.  
  105. cout<<" PROGRAM FOR FINDING THE NEXT DATE USING OPERATOR OVERLOADING '++'\n\n";
  106.  
  107. while(1)
  108. {
  109. cout<<"ENTER THE DATE :";
  110. d1.read();
  111.  
  112. if(!valid(d1))
  113. cout<<"INVALID DATE PLEASE ENTER THE CORRECT DATE ..."<<endl;
  114. else
  115. break;
  116. }
  117.  
  118. cout<<endl<<"\n THE GIVEN DATE IS :";
  119. d1.write();
  120. ++d1;
  121.  
  122. cout<<endl<<"\n AFTER INCREMENTING :";
  123. d1.write();
  124. getch();
  125. }
Last edited by alc6379; Nov 2nd, 2004 at 4:11 pm. Reason: added [code] tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: this code isn't working...why..plz help

 
0
  #2
Nov 2nd, 2004
Greetings,

There are a few typographical and syntax issues with your program. Firstly, lets look at the minor issues:

Point A
int valid(update D)
I'm guessing that update should be udate, hence the class.

Point B
In your operator overload function operator++() one of your if statements have a slight issue:
if(year%4==0 && day==29)||(year%4!=0 && day==28))
As seen, you open a pararenthesis and close it. Open another and then close two. We can simple fix this by adding a parenthesis in the beginning of the if statement:
if((year%4==0 && day==29)||(year%4!=0 && day==28))
So far this has worked, and your program compiled just fine after this. Without using the [code] [/code] tags it was difficult for me to see if your programs if/else statements were properly called, though it seemed fine once I tabbed things over in my compiler. If you have further questions, please feel free to ask.


I hope this helps,
- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6
Reputation: sweety is an unknown quantity at this point 
Solved Threads: 0
sweety sweety is offline Offline
Newbie Poster

Re: this code isn't working...why..plz help

 
0
  #3
Nov 2nd, 2004
thanks for ur reply...
but didn't it go into an infinite loop even after correcting
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: this code isn't working...why..plz help

 
0
  #4
Nov 2nd, 2004
Greetings,

How about if we try this:
int main() {
	udate d1;

	cout << "PROGRAM FOR FINDING THE NEXT DATE USING OPERATOR OVERLOADING '++'" << endl << endl;

	while (1) {
		cout << "ENTER THE DATE: ";
		d1.read();

		if (!valid(d1)) {
			cout << "INVALID DATE PLEASE ENTER THE CORRECT DATE ..." << endl;
			continue;
		}

		cout << endl << "THE GIVEN DATE IS: ";
		d1.write();
		++d1;

		cout << endl << "AFTER INCREMENTING: " << endl << endl;
		d1.write();
		getch();
	}

	return 0;
}
What have we changed?
We moved our given date and incrementing lines inside the infinite loop. What we did before was break out of the loop when it was time to write the date. Indeed the program won't infintely loop if we break it to soon.

How to we get the user to ask again?
Simple. We take advantage of the infinite loop. The continue statement is related to break though it causes the next iteration of the enclosing while loop to begin. Exactly what we need! When we don't get the results, we start over. The while loop will always run, so why not start back at the beginning.


Hope this helps,
- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
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



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC