Thread: Help with my program

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    Help with my program

    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.
    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();
      }
    
    }
    And this is the set member function:
    Code:
    // 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
      }
    }
    Thanks in advance

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Code:
    while (Set(m_input, d_input, y_input) == 0){
        cout << "Invalid date. Try again:\n";
        Input();
      }
    m_input, d_input, and y_input will never change here. Even if Input() exits successfully, the while loop will still require the same answer again because it keeps setting the same values (thus getting the same answer).

    Try if() instead of while().

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    need help with Show method

    Hi all,
    I am almost done with my program but first I need to do this one last method. It is called the Show() method and it will show the date in a variety of formats dpeneding on how you change the format private data member in the class. The possible formats are 'default', 'Two-Digit', and 'Long'. I could do Default easily enough but the other two are giving me problems. This is what I have so far:
    Code:
    // This function should simply output the date to the screen
    void Date::Show(){
      if (format == 'D'||'d'){
        int m = GetMonth();
        cout << m;
        cout << "/";
    
        int d = GetDay();
        cout << d;
        cout << "/";
    
        int y = GetYear();
        cout << y;
        cout << "\n";
      }
    
      if (format == 'L'||'l'){
        int m = GetMonth();
        char string_literal;
        if (m == '1'){
          string_literal = 'Jan';
        }
        if (m == 2){
          string_literal = 'Feb';
        }
    
    
    
      }
    
      if (format == 'T'||'t'){
        // insert two-digit format
      }
    }
    An example is
    Default=> 10/4/1998
    Two-Digit=> 10/04/98
    Long => Oct 4, 1998
    I have an idea how to do the month and date of the two-digit but everything else I hace no clue how to do it. Thanks guys.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's a clue on the months thing:
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
      const char *Months[] = {
        "January",
        "February",
        "March",
        "and so on"
      };
      
      cout <<"Month 0 is " <<Months[0] <<endl;
      
    }
    For a long year, if you only have 2 digits from the user, you need to make an assumption about the other two. For example, if the users entry is greater than 50, assume they mean 19xx (1951-1999). If it is less than or equal to 50, assume they mean 20xx (2000-2050). If the user enters a 4 digit year, simply write out the 3rd and 4th digits only for a two digit year.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    One last question

    I have one more last question for those who would like to proofread code. I have almost finished my date class except for the increment function. I am sure that everyone here knows how to increment the date but I don't know what I am doing wrong in my code. Here are my function.(PLease pay particular attention to Date::Increment()
    Code:
    // 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
      }
    }
    Code:
    // This function should move the date forward one calendar day.
    void Date::Increment()
    {
      int m = GetMonth();
      int d = GetDay();
    
      if ((m == 1)||(m == 3)||(m == 5)||(m == 7)||(m == 8)||(m == 10)||(m == 10) && (d == 31)){
        int new_month, new_day, new_year;
        if (m == 12){
          new_month = 1;
          new_day = 1;
          new_year = GetYear() + 1;
          // Set new date
          Set(new_month, new_day, new_year);
        }
        if (m != 12){
          new_month = GetMonth() + 1;
          new_day = 1;
          new_year = GetYear();
          // Set new date
          Set(new_month, new_day, new_year);
        }
      }
      if ((m == 4)||(m == 6)||(m == 9)||(m == 11) && (d == 30)){
        int new_month, new_day, new_year;
        new_month = GetMonth() + 1;
        new_day = 1;
        new_year = GetYear();
        // Set new date
        Set(new_month, new_day, new_year);
      }
    
      if ((m == 2) && (d == 28)){
        int new_month, new_day, new_year;
        new_month = GetMonth() + 1;
        new_day = 1;
        new_year = GetYear();
        // Set new Date
        Set(new_month, new_day, new_year);
      }
    }
    Code:
    // Accessor function shtat returns the month
    int Date::GetDay(){
      return day;
    }
    
    
    // Accessor function shtat returns the month
    int Date::GetYear(){
      return year;
    }
    
    
    // This function allows the user to change the format setting
    bool Date::SetFormat(char f){
      if ((f == 'D')||(f == 'd')||(f == 'T')||(f == 't')||(f == 'L')||(f == 'l')){  // if the format codes are either 'D' or 'T' or 'L'
        format = f;
        return true;
      }
      else {
        return false;
      }
    }
    I believe that everything except for my Increment() is working correctly. Thank you everybody!

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    You might want to add a few parentheses in there, just to make sure everything is evaluated correctly (I've never paid attention to the precedence, I always just put parentheses)
    Example:

    Code:
    if ((m == 1)||(m == 3)||(m == 5)||(m == 7)||(m == 8)||(m == 10)||(m == 10) && (d == 31))
    
    //if you want to check to see if the month is 1,3,5, etc
    //and if those are true, then see if the day is 31
    
    if (  (  (m==1)||(m==3)||(m==5)||(m==7)||(m==8)||(m==10)  )  && (d==31)  )
    also, I'm not sure that it would assign the date correctly the way you did it. You might want to put:
    Code:
     new_month = (GetMonth() + 1);
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM