-1

Having trouble determining the cause of this. Everything builds properly just does not print any type of output. I'm sure it is something minuscule so please be understanding and not chew me out for asking such a simple question. Trying my best to learn on my own. The program is supposed to ask you to enter a date dd/mm/yy. Below I have attached the main.cpp and date.h. Thank you for any assistance.

main.cpp
#ifndef DATE_H_
#define DATE_H_

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class Date
{
private:
    int day, month, year;
    char format;
    bool isLeapYear() const;
    int days_in_month() const;

public:
    Date(int month=1, int day=1, int year=2000);
    void Input();
    void Show();
    bool Set(int m, int d, int y);
    int GetMonth() const;
    int GetDay() const;
    int GetYear() const;
    bool SetFormat(char f);
    void Increment(int numDays=1);
    int Compare(const Date& d);
    void printJulianDate();
};

#endif

//end of date.h

// date.c
#include "date.h"

// constructor

Date::Date(int month, int day, int year)
{
    // set the parameters to default values
    this->month = 1;
    this->day = 1;
    this->year = 2000;
    this->format = 'D';
    // call the method to set the passed month, day and year
    if(!Set(month,day,year))
    {
        cout<<"Invalid date"<<endl; // display error message if invalid date
    }
}

// helper function to return if the year is leap or not
bool Date:: isLeapYear() const
{
    return ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)));
}

// helper function to return the number of days in the month
int Date::days_in_month() const
{
    if(month == 2)
    {
        if(isLeapYear())
            return 29;
        else
            return 28;
    }else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month ==10 || month==12)
        return 31;
    else
        return 30;

}

// function to input date from the user
void Date:: Input()
{
    int m,d,y;
    char sep;
    // input of date
    cout<<"Enter date:";
    cin>>m>>sep>>d>>sep>>y;
    // loop that continues till the user enters a valid date
    while(!Set(m,d,y))
    {
        cout<<"Invalid date. Try again: ";
        cin>>m>>sep>>d>>sep>>y;
    }
}

// function to display the date in the format specified
void Date:: Show()
{
    if(format == 'D')
    {
        cout<<month<<"/"<<day<<"/"<<year;
    }else if(format == 'T')
    {
        cout<<setw(2)<<setfill('0')<<month<<"/"<<setw(2)<<setfill('0')<<day<<"/"<<setw(2)<<setfill('0')<<(year%100);
    }else if(format == 'L')
    {
        switch(month)
        {
            case 1:
                cout<<"Jan";
                break;
            case 2:
                cout<<"Feb";
                break;
            case 3:
                cout<<"Mar";
                break;
            case 4:
                cout<<"Apr";
                break;
            case 5:
                cout<<"May";
                break;
            case 6:
                cout<<"June";
                break;
            case 7:
                cout<<"July";
                break;
            case 8:
                cout<<"Aug";
                break;
            case 9:
                cout<<"Sep";
                break;
            case 10:
                cout<<"Oct";
                break;
            case 11:
                cout<<"Nov";
                break;
            case 12:
                cout<<"Dec";
                break;
        }

        cout<<" "<<day<<", "<<year;
    }
}

// method to validate the passed arguments and set the date if the arguments represent a valid date
bool Date:: Set(int m, int d, int y)
{
    if(y > 1582)
    {

        if(m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
        {
            if(d < 1 || d > 31)
                return false;
            else
            {
                day = d;
                month = m;
                year = y;
                return true;
            }
        }else if(m == 4 || m == 6 || m == 9 || m == 11)
        {
            if(d < 1 || d > 30)
                return false;
            else
            {
                day = d;
                month = m;
                year = y;
                return true;
            }
        }else if(m == 2)
        {
            if((y%400 == 0 ) || ((y%4 == 0) && (y%100) != 0))
            {
                if(d < 1 || d > 29)
                    return false;
                else
                {
                    day = d;
                    month = m;
                    year = y;
                    return true;
                }
            }else
            {
                if(d < 1 || d > 28)
                    return false;
                else{
                    day = d;
                    month = m;
                    year = y;
                    return true;
                }
            }
        }else
            return false;
    }else if(y == 1582)
    {
        if(m >= 10)
        {
            if(m == 10 || m == 12)
            {
                if(d < 1 || d > 31)
                    return false;
                else
                {
                    day = d;
                    month = m;
                    year = y;
                    return true;
                }
            }else if( m == 11)
            {
                if(d < 1 || d > 30)
                    return false;
                else
                {
                    day = d;
                    month = m;
                    year = y;
                    return true;
                }
            }else
                return false;
        }
    }

    return false;
}

// function to return the month
int Date::GetMonth() const
{
    return month;
}

// function to return the day
int Date::GetDay() const
{
    return day;
}

// function to return the year
int Date::GetYear() const
{
    return year;
}

// function to set the format of the date
bool Date::SetFormat(char f)
{
    if(f == 'D' || f == 'T' || f == 'L')
    {
        format = f;
        return true;
    }

    return false;

}

// function to increment the number of days passed to the date and update the date accordingly
void Date:: Increment(int numDays)
{
    day += numDays; // add number of days passed
    // loop that continues till the date is valid
    while(day > days_in_month())
    {
        day -= days_in_month();
        month++;

        if(month > 12) // if month > 12, i.e it represents the first month of next year
        {
            // increment year and set month to 1
            year++;
            month = 1;
        }
    }
}

// function to compare the passed date with this date and return
// -1, if the calling object comes first chronologically
// 0, if the calling object comes first chronologically
// 1, if the objects are the same date

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

     // function to print the julian date
      void Date:: printJulianDate()
    {
    int days=0;
    // loop to get the day number
    for(int i=1;i<month;i++)
    {
        switch(i)
        {
            case 1:
                days += 31;
                break;
            case 2:
                if(isLeapYear())
                    days += 29;
                else
                    days += 28;
                break;
            case 3:
                days += 31;
                break;
            case 4:
                days += 30;
                break;
            case 5:
                days += 31;
                break;
            case 6:
                days += 30;
                break;
            case 7:
                days += 31;
                break;
            case 8:
                days += 31;
                break;
            case 9:
                days += 30;
                break;
            case 10:
                days += 31;
                break;
            case 11:
                days += 30;
                break;
            case 12:
                days += 31;
                break;
          }

       }
       days += day;
       cout<<days<<"-"<<setw(4)<<setfill('0')<<year;
     }



    Date.h
   #ifndef UNTITLED16_DATE_H

   #define UNTITLED16_DATE_H

   #endif //UNTITLED16_DATE_H



     date.cpp

     int main(){return 0;}
  • 2
    *Everything builds properly* -- All that means is that there are no syntax errors, and all the functions you are calling exist. It has nothing to do with whether your program is or is not logically correct. If all that is required is for a program to build properly, there would be no such thing as bugs. – PaulMcKenzie Jan 26 '22 at 17:12
  • Use your debugger to see what your code is doing. Step through the code 1 line at a time looking at the variables and flow at each step. Don't just run your code in the debugger and hope it will tell you something. – drescherjm Jan 26 '22 at 17:14
  • 3
    What exactly do you expect `int main(){return 0;}` to print? – molbdnilo Jan 26 '22 at 17:15
  • `int main(){return 0;}` if this is your main of course it does not output anything. It should not output anything. – drescherjm Jan 26 '22 at 17:15
  • [What is a debugger?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). And if the problem is that you immediately `return 0;` inside of main, then that makes my first comment shine through. Sure, the code built successfully, but is it correct? No. – PaulMcKenzie Jan 26 '22 at 17:16
  • 1
    Why is your `Date` class in "main.cpp", and your `main` function in "Date.cpp"? Why is "Date.h" empty? – molbdnilo Jan 26 '22 at 17:17
  • FYI, you can get rid of some of the `switch` statements by using array lookup, e.g. `static const char month_names[] = {"zero", "Jan", "Feb", /*...*/, "Nov", "Dec"};` You convert a month number to name: `std::cout << "Name of month 4: " << month_names[4] << "\n";` – Thomas Matthews Jan 26 '22 at 18:15

1 Answers1

1

Since your main function is int main(){return 0;} all your code is doing is: starting up, calling main(), and then returning back to the caller. Of course there is no output involved here...

There are many other details with your code where I am not sure if it will work as you intend. But the minimum change you have to do to see anything is something like this:

#include "Date.h"
#include <iostream>

int main() {
 Date theDate;
 theDate.Input();
 theDate.Show();
 // and maybe also
 Date theDate2;
 theDate2.Input();
 if (theDate2.Compare(theDate)) {
   std::cout << "true" << std::endl;
 }
 return 0;
}
Ralf Ulrich
  • 1,575
  • 9
  • 25