Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Why is the error like "No such file or directory", what should i do

+1 vote
asked Jul 21, 2021 by Lasala, Roselyn Joy (130 points)
#ifndef _Date_
#define _Date_

class Date
{
    private:
       short month, day, year;

    public:
       void init (void);
       void init ( int month, int day, int year);
       void print (void);
};
#endif

#include "date.h"
#include <iostream>
#include <ctime>
using namespace std;

void Date::init (void)
{
    struct tm*ptr;
    time_t sec;

     time(&sec)
     ptr = localtime (&sec);

     month = (short) ptr-> tm_mon +1;
     day = (short) ptr->tm_mday;
     year = (short) ptr-> tm_year + 2002;

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

    void Date::print (void)
    {
       cout << month << '-' << day << '-' << year << endl;
     }

#include "date.h"
#include <iostream>
using namespace std;

int main ()
{
     Date today, birthay, aDate;

     today.init();
     birthday.init (12, 11, 2007);

     cout << "Today's date: ";
     today.print();

     cout << '\n Chris' birthday : ";
     birthday.print();

     cout << "------------------------\n"
             "Some testing outputs: " << endl;

     aDate = today;
     aDate.print();

      Date &holiday = aDate;
      holiday.init ( 1, 5, 2009);
      aDate.print();

  return 0;
}

1 Answer

0 votes
answered Sep 22, 2021 by Peter Minarik (86,240 points)

I do not see "No such file or directory" error. But I see other problems.

Problem #1 - typo

Date today, birthay, aDate;

missing a D from birthday.

Problem #2 - wrong string literal

cout << '\n Chris' birthday : ";

Strings should start with double quotes and end with double quotes. In your code you started with a single quote and ended with a double quote. That not correct.

Problem #3 - missing semicolon

In C/C++ (and many other languages) you must terminate your instructions with a semicolon (;)

time(&sec);

Enhancement

I suggest using the constructor to set up your class, not additional "Init" functions:

class Date
{
private:
    unsigned char month;
    unsigned char day;
    short year;

public:
    Date();
    Date(unsigned char month, unsigned char day, short year);
    void print() const;
};

Furthermore, you can store the month and day on just an unsigned char (1 byte) data structure each.

Last but not least printing the date can be a constant function as there's no need to change the instance while printing it.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...