Hi all,
I need some help with my program. I am working with classes and objects right now in my C++ course and we have to design a Date class. Designing some of the functions of this class is quite tricky to me though. I was wondering if you could please look at two of my functions in particular, especially the Input() function. I need to be able to ask the user what the month, day, and year is but if it is not a valid date(i.e - if we cannot "Set" it) we have to prompt the user with an error message and ask him again. This looping is not working for some reason. Could anyone tell me where I am going wrong.
And this is the set member function:Code:// This function should prompt the user to enter a date and then allow the user to input a date void Date::Input(){ int m_input, d_input, y_input; cout << "Please enter a month:"; cin >> m_input; cout << "Please enter a day:"; cin >> d_input; cout << "Please enter a year:"; cin >> y_input; // bool status = Set(m_input, d_input, y_input); // cout << "Status of Set function:" << status << "\n"; // The function passes the trivial test above!! Set(m_input, d_input, y_input); while (Set(m_input, d_input, y_input) == 0){ cout << "Invalid date. Try again:\n"; Input(); } }
Thanks in advanceCode:// This function should set the date to the specified values. If resulting date is invalid // the existing date should not be changed bool Date::Set(int m, int d, int y){ if (y > 0){ if ((m == 2) && (d > 0 && d <= 28)){ // handle the anomaly month February first! month = m; day = d; year = y; return true; }; // handles the months January, March, May, July, August, October, December if (((m == 1)||(m == 3)||(m == 5)||(m == 7)||(m == 8)||(m == 10)||(m==12)) && (d>0 && d<=31)){ month = m; day = d; year = y; return true; } // handles the months April, June, September, November if (((m == 4)||(m == 6)||(m == 9)||(m == 11)) && (d>0 && d<=30)){ month = m; day = d; year = y; return true; } // catch - all statement return false; } else { return false;// if year < 0 } }



LinkBack URL
About LinkBacks


