944,052 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2360
  • C++ RSS
Nov 2nd, 2004
0

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

Expand Post »
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sweety is offline Offline
6 posts
since Nov 2004
Nov 2nd, 2004
0

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

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
Reputation Points: 26
Solved Threads: 4
Junior Poster
Stack Overflow is offline Offline
185 posts
since Sep 2004
Nov 2nd, 2004
0

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

thanks for ur reply...
but didn't it go into an infinite loop even after correcting
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sweety is offline Offline
6 posts
since Nov 2004
Nov 2nd, 2004
0

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

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
Reputation Points: 26
Solved Threads: 4
Junior Poster
Stack Overflow is offline Offline
185 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Troubled Baseball program
Next Thread in C++ Forum Timeline: new implementation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC