my complier is throwing an error up when i run it. the problem is run into is my Increment function is etither given a number to increment by, or if it is left blanlk (Increment()) should default to incrementing 1.

i thought i could do just 2 functions, one with a parameter given and the other without(kinda like what you do for constructors). still throwing errors. how would i make it with/without parameters in one function?

   void Date::Increment(int numDays)
    {   int newDay=0; 

        if(day+numDays > DAYINMONTH[month])
        { (month=month+1);
            newDay = (day+1)-DAYINMONTH[month];
            (day = newDay + 0);
        }
        else
        {day = day + numDays;}
    }
    void Date::Increment()
    {
        int newDay=0,numDays; 

        if(day+numDays > DAYINMONTH[month])
        { (month=month+1);
            newDay = (day+1)-DAYINMONTH[month];
            (day = newDay + 0);
        }
        else
        {day = day + numDays;}


    }

Recommended Answers

All 9 Replies

Did you declare both of the function prototypes?

yes both in the header file. when i do run it like this, only increments 1 when i enter 5. then i get some break error from visual.

void Increment(int numDays);
    void Increment();

he is the header

#define DATE_H
#include <iostream>

using namespace std;


class Date
{
  public:
    Date();
    Date(const int m);
    Date(const int m ,const int d,const int y);
    void Input();
    void Show();
    bool Set(int m,int d,int y);
    bool SetFormat(char f);
    int GetMonth();
    int GetDay();
    int GetYear();
    void Increment(int numDays);
    void Increment();
    int Compare(const Date& d); 
  private:
    int day;
    int month;
    int year;
    char format;

};








#include "date.h"
#include <iostream>
#include <string>
#include <ctime>

using namespace std;

string MONTHS[]={"","Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec"};
string DAYOFWEEK[]={"","1","2","3","4","5","6","7"};

string DAYOFMONTH[]={ "","01","02","03","04","05","06","07","08","09","10",
                      "11","12","13","14","15","16","17","18","19","20",
                      "21","22","23","24","25","26","27","28","29","30","31"};

string TWODIGIT[]= {"00","01","02","03","04","05","06","07","08","09","10","11",
                    "12","13","14","15","16","17","18","19","20","21","22","23",
                    "24","25","26","27","28","29","30","31","32","33","34","35","36",
                    "37","38","39","40","41","42","43","44","45","46","47","48","49",
                    "50","51","52","53","54","55","56","57","58","59","60","61","62",
                    "63","64","65","66","67","68","69","70","71","72","73","74","75",
                    "76","77","78","79","80","81","82","83","84","85","86","87","88",
                    "89","90","91","92","93","94","95","96","97","98","99"};
int DAYINMONTH[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    Date::Date()
    {
       int month = 1;
       int day =  1;
       int year = 2000;     
    }
    Date::Date(const int m)
    {
        int day = 1;
        int year = 2000;


    }
    Date::Date(const int m,const int d,const int y)
    {
       month = m;
       day = d;
       year =y;
    }
    void Date::Input()
    {

     do{
         //ask's to enter a correct date
       cout << "Enter a Correct Date(MM/DD/YYYY) "<< endl;
       //enter month 
       cout << "Month: ";
        cin >> month;
        // enter day    
        cout << "Day: ";
        cin >> day ;
        //enter year
        cout << "Year: ";
        cin >> year;
        }while(Set(month,day,year)==false);
    }
    void Date::Show()
    {   


     cout  <<month<<"/"<<day<<"/"<<year<<endl;


    }
    bool Date::Set(int m,int d,int y)
    {
         if (m < 1 || m > 12)
         {return false;}
         //else 
         //{return true;}

         else if (d < 1 || d > 31)
             {return false;}
        // else 
        // {return false;}

        else if(y < 1)     
        {
            return false;
         }
        else 
        {return true;}


    }
    int Date::GetMonth()
    {return month; }

    int Date::GetDay()
    {return day;}

    int Date::GetYear()
    {return year;}


    bool Date::SetFormat(char f)
    {    
        if(f == 'D'||'L'||'T')
        { 
         switch(f)
         {
            case 'D':         
                  cout  <<month<<"/"<<day<<"/"<<year<<endl;
                    break;
            case 'L':
                  cout << MONTHS[month]<<" "<<day<<","<<year<<endl ;
                    break;
            case 'T':
                  cout<< DAYOFMONTH[month]<<"/"<<DAYOFMONTH[day]<<"/"<<TWODIGIT[year%100]<<endl;
                    break;
            default:
                    cout  <<DAYOFMONTH[month]<<"/"<<day<<"/"<<year<<endl;
                    break;
         }
        }
        else
        {  return false;}
    }    

    void Date::Increment(int numDays)
    {   int newDay=0; 

        if(day+numDays > DAYINMONTH[month])
        { (month=month+1);
            newDay = (day+1)-DAYINMONTH[month];
            (day = newDay + 0);
        }
        else
        {day = day + numDays;}
    }
    void Date::Increment()
    {
        int newDay=0,numDays=0; 

        if(day+numDays > DAYINMONTH[month])
        { (month=month+1);
            newDay = (day+1)-DAYINMONTH[month];
            (day = newDay + 0);
        }
        else
        {day = day + numDays;}


    }
    int Date::Compare(const Date& d)
    {
        if(month < d.month ){return -1;}
        else if(month >d.month){return 1;}
        else if(month == d.month){return 0;}
        // check day
        else if(day < d.day ){return -1;}
        else if(day > d.day){return 1;}
        else if(day == d.day){return 0;}
        //checks year
        else if(year < d.year ){return -1;}
        else if(year >d.year){return 1;}
        else (year == d.year);return 0;







    }  

and the driver.cpp given to us by the prof.

int main() 
{
  int J; 
  Date d1;                      // Default Date
  Date d2(12);                  // One Parameter, day=1 , Year=2000
  Date d3(1,1,2000);            // Leap Year and also to test decrement
  Date d4(2,29,1900);           // Invalid Date
  Date d5(12,31,2011);          // End of Year to test increment

      cout << "******  Testing Constructors ********  " << endl;
      cout << " Showing d1 - Should be default date: ";
      d1.Show();
      cout << " Showing d2 - Should have month at 12 and rest default: ";
      d2.Show();
      cout << " Showing d3 - Should show January 1, 2000: ";
      d3.Show();
      cout << " Showing d4 - Should show default date: ";
      d4.Show();
      cout << " Showing d5 - Should show December 31, 2011: ";
      d5.Show();


      cout << "***** Test Input ******" << endl; 
      d1.Input(); 
      cout << "Input a number to increment the date by (Positive please): " << endl;
      cin >> J ;
      cin.ignore(80,'\n'); 
      cout << "Incrementing by " << J << endl; 
      d1.Increment(J);
      d1.SetFormat('D');
      cout << " *** Show the Increment worked - Format in D ** " << endl; 
      d1.Show();

      cout << "*** Set Format to T  and Display the results *** " << endl; 
      d1.SetFormat('T');
      d1.Show();
      cout << "** Set Format to L and Display the results *** " <<  endl; 
      d1.SetFormat('L');
      d1.Show();


       cout << "*** Setting Format back to T *** "<< endl;
       d1.SetFormat('T');
       d2.SetFormat('T');
       d3.SetFormat('T');
       d4.SetFormat('T');
       d5.SetFormat('T');

       cout << "*** d5 is current set at : " ;
       d5.Show(); 
       cout << "*** Increment d5 by 1 and Display the results ***" << endl; 

       d5.Increment(); 
       d5.Show();
       cout << "*** Increment d5 by 31 and Display the results ***" << endl; 
       d5.Increment(31);
       d5.Show();

       cout << "*** Shows that GetMonth,GetDay, and GetYear worked *** " << endl;
       cout << "The d4 Date is: " << d4.GetMonth() << '/'<<d4.GetDay()<<'/'<<d4.GetYear() << endl; 

       cout << endl;
       cout << "*** Showing the compares work by comparing a series of dates: *** " << endl; 
       if(d3.Compare(d4)==-1) cout << "d3" << " Is less then " << "d4" << endl;
       if(d4.Compare(d3)==1) cout << "d4" << " Is Grtr then " << "d3" << endl;
       if(d4.Compare(d5)==0) cout << "d4" << " Is Equal to  " << "d5" << endl;
       if(d1.Compare(d3)==-1) cout << "d1" << " Is Less then " << "d3" <<endl;
       else if (d1.Compare(d3)==1) cout << "d1" << " Is Greater than " << "d3" << endl;
       else if (d1.Compare(d3)==0) cout << "d1" << " Is equal to " << "d3" << endl;      

  return 0;
}



#endif

It compiles fine without the error you stated above, so whatever you're compiling isn't the code you showed us, or there's something up with your compiler.

In other news, your function Date::SetFormat is meant to return a bool but there's a path through it that doesn't return anything. Also, this - if(f == 'D'||'L'||'T') - is NOT testing for f being equal to D, or f being equal to L, or f being equal to T. What you have - f == 'D'||'L'||'T' - will always always always be true.

Why not just combine the two functions by setting a default value for the parameter, they are both the same thing anyway

class Date
{
  public:
    Date();
    Date(const int m);
    Date(const int m ,const int d,const int y);
    void Input();
    void Show();
    bool Set(int m,int d,int y);
    bool SetFormat(char f);
    int GetMonth();
    int GetDay();
    int GetYear();
    void Increment(int numDays = 0); // <<<<<<<<<<<<<<<<<
    //void Increment();
    int Compare(const Date& d); 
  private:
    int day;
    int month;
    int year;
    char format;

};

i tried this

void Increment(int numDays =1);

some reason or another it threw an error up when i called d5.Increment();

post new code please

i got an error saying Date::SetFormat does not return a value, so i added this....

bool Date::SetFormat(char f)
{    


       if('D'==f)      
       { cout  <<month<<"/"<<day<<"/"<<year<<endl; return true;}

       else if('L'==f)
       {cout << MONTHS[month]<<" "<<day<<","<<year<<endl ;return true;}

       else if('T')
       {  cout<< DAYOFMONTH[month]<<"/"<<DAYOFMONTH[day]<<"/"<<TWODIGIT[year%100]<<endl;return true;}
       else
       {cout  <<DAYOFMONTH[month]<<"/"<<day<<"/"<<year<<endl; return true;}     
}    

`

now im getting an unresolved error LNK 2019? any thoughts?

This linker error indicates that you're not linking all the compiled code together, OR you're trying to call a function that just plain doesn't exist in the cpp file (i.e. the header promises it exists, but it doesn't really). First, read the rest of the error message and check that the function it's looking for really does exist.

Then, if it does exist, you're just not linking it. You have two cpp files, I think. They both need to be compile, and then be linked together. If you're using some kind of IDE, you need to make sure they're both in the "project" or whatever your IDE calls them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.