| | |
this code isn't working...why..plz help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2004
Posts: 6
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
// PROGRAM TO FIND THE NEXT DATE OF A GIVEN DATE #include<iostream.h> #include<conio.h> class udate { int day, month, year; public: void read() { cin>>day>>month>>year; } void write() { cout<<day<<"/"<<month<<"/"<<year; } friend int valid(udate); void operator++(); }; int valid(update D) { int d = D.day; int m = D.month; int y = D.year; if(d <= 0|| d>31 || m <= 0 || m>12 || y<=0) return(0); else if(( m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12) && d>31) return(0); else if(( m==4 || m==6 || m==9 || m==11) && d>30) return(0); else if( m==2 ) { if((y%100==0 && y%400==0 && d>29) || (y%100==0 && y%400!=0 && d>28)) return(0); else if((y%4==0 && d>29) || (y%4!=0 && d>28)) return(0); } return(1); } void udate :: operator ++() { if(month==2) { if((y%100==0 && year%400==0 && day==29) || (year%100==0 && year%400!=0 && day==28)) { day=1; month ++; } else if(year%4==0 && day==29)||(year%4!=0 && day==28)) { day=1; month++; } else day++; } else if((month==1 || month==3 || month ==5 || month==7 || month==8 || month==10 || month==12) && day==31) { day=1; if(month==12) { month = 1; year++; } else month++; } else { if((month==4 || month==6 || month==9 || month==11) && day == 30) { day=1; month++; } else day++; } } void main() { udate d1; clrscr(); cout<<" PROGRAM FOR FINDING THE NEXT DATE USING OPERATOR OVERLOADING '++'\n\n"; while(1) { cout<<"ENTER THE DATE :"; d1.read(); if(!valid(d1)) cout<<"INVALID DATE PLEASE ENTER THE CORRECT DATE ..."<<endl; else break; } cout<<endl<<"\n THE GIVEN DATE IS :"; d1.write(); ++d1; cout<<endl<<"\n AFTER INCREMENTING :"; d1.write(); getch(); }
Last edited by alc6379; Nov 2nd, 2004 at 4:11 pm. Reason: added [code] tags
Greetings,
There are a few typographical and syntax issues with your program. Firstly, lets look at the minor issues:
Point A 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: 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: 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
There are a few typographical and syntax issues with your program. Firstly, lets look at the minor issues:
Point A
int valid(update D)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))if((year%4==0 && day==29)||(year%4!=0 && day==28))
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
IRC
Channel: irc.daniweb.com
Room: #c, #shell
Greetings,
How about if we try this: 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
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; }
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
IRC
Channel: irc.daniweb.com
Room: #c, #shell
![]() |
Similar Threads
- Code error.....plz rectify me :) (VB.NET)
- my code is working but for some reason i cant restart my loop (C++)
- CHECKBOX VAILDATION CODE not working (PHP)
- Does this code working after changing environment? (VB.NET)
- javascript code not working in firefox (JavaScript / DHTML / AJAX)
- I would liike to multiply two matrices but my code is not working..help me out! (C++)
Other Threads in the C++ Forum
- Previous Thread: Troubled Baseball program
- Next Thread: new implementation
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





