C++ programming challenge



Have gotten this far but am lost
could use some guidance
Am writing to get an output of: My date is May 25, 2002 or any date that is to be inputted by the user
1
2
3
4
5
6
7
8
9
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
  #include <iostream>

using namespace std;

 

class Date {

public: 

    int year;  

private: 

    int month; 

    int day;

public:

    int getYear() {

        return year;

    }

    int getMonth() {

        return month;

    }

    int getday() {

        return day;

    }

    void setYear(int y) {

       return year = y;

    }

    void setMonth(int m) {

         month=m;

    }

    void setday(int d) {

        return day = d;

    }

};

 

int main() {

    Date myDate;

    myDate.input();

    myDate.output();

    return 0;

}

 
Well, you're calling two functions that don't exist–input() and output(). Might start by defining and initializing them.

Also, you will want to have call the setter functions before the getters.

And, if you want to get user inputs with std::cin, I advise you to do that through main, because doing it through a class is not generally good programming practice because it can be tricky to do right.

max
Last edited on
Hello Beginners12,

Thank you for using code tags.

Your code is a good start and your use of blank lines is good, but you use to many.

When you define a class it is "private" by default unless otherwise changed, so you could do this:
1
2
3
4
5
6
7
8
9
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
class Date
{
    int year;
    int month;
    int day;

    public:
        int getYear()
        {
            return year;
        }

        int getMonth()
        {
            return month;
        }

        int getday()
        {
            return day;
        }

        void setYear(int y)
        {
            year = y;
        }

        void setMonth(int m)
        {
            month = m;
        }

        void setday(int d)
        {
            day = d;
        }
};

No offense agent max, but it makes no difference which comes first the getters or setters as long as they are there. I usually put the getters first because they are quicker to write.

The 2 functions that you call in "main" are missing.

In the setter functions for year and day you are trying to return something from a void functions that returns nothing. Remove the "return".

And when you do write the class functions for input and output remember that these public function already have access to the "private" variables. There is no need to call the getters or setters for this.

Andy
Thanks Andy but it’s not debugging neither is it giving me the result : my date is 22 March 2007
Hello Beginners12,

I do not understand what you mean by "not debugging neither".

Are you entering an actual string for the month or a number that you want to change into a string?

I could say that it compiled and ran fine in MSVS 2017, but that is meaningless because it is not your computer, IDE and compiler Also I have no idea what C++ standard your compiler is using.

If you received error messages when you compiled you need to post the complete error message and not what you think it means.

You also need to show you input function and how you are typing the date.

Andy

Edit typo:
Last edited on
Hello Beginners12,

Do not make this an X/Y problem http://xyproblem.info/ where I have to keep asking questions just to figure out what you are doing wrong.

Andy
@Handy Andy,
Oh! (smh) Duh. I was meaning to say that you should call the setter functions first. Thank you for pointing that out, sorry if I confused anyone.
@Beginners12,
Andy is right, you can't return anything from a void function. See below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// This function can't return anything-
// the return type is "void".
void func1 (int thing)
{
      something = thing;
}

// This function returns a double-
// its return type is "double".
double func2 ()
{
      double somethingElse;

      somethingElse = something + 2;
      return somethingElse;
}
Hope this clarifies.
max
@agent max,

If you look closely at "main" the 2 function call are class member functions. They already have access to the private variables, so in the end the getters and setters are not needed right now.

Andy
Wait...but they can't be class member functions because they are never even defined. Now, if they were defined as members of the class, with prototypes inside the class, then they would be member functions and have access to all the variables.
That is my point.
With the class member functions input and output defined (as well as overloading operator<< for easy output of a class instance):
1
2
3
4
5
6
7
8
9
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
#include <iostream>
#include <string>

class Date
{
private:
   int year;
   int month;
   int day;

public:
   // ctor(s)
   Date()
      : year(1), month(1), day(1) { }

   Date(int y, int m, int d)
      : year(y), month(m), day(d) { }

   // I/O functions
   void input();
   void output() const;

   // declare operator<< as a friend so it can "see" the private data members
   // and output to the passed std::ostream (usually std::cout)
   friend std::ostream& operator<<(std::ostream&, Date);

   // getters/setters
   // declare the getters as const so the member funcs can't alter the data members
   int getYear() const { return year; }
   void setYear(int y) { year = y; }

   int getMonth() const { return month; }
   void setMonth(int m) { month = m; }

   int getDay() const { return day; }
   void setDay(int d) { day = d; }
};

int main()
{
   Date myDate;

   myDate.input();
   myDate.output();

   Date anotherDate(2007, 2, 25);
   anotherDate.output();

   // with operator<< overloaded output of a class is as easy as
   // Plain Old Data like an int
   std::cout << myDate << '\n';
   std::cout << anotherDate << '\n';
}

void Date::input()
{
   std::cout << "Enter the new year: ";
   int y;
   std::cin >> y;
   year = y;

   std::cout << "Enter the new month: ";
   int m;
   std::cin >> m;
   month = m;

   std::cout << "Enter the new day: ";
   int d;
   std::cin >> d;
   day = d;
}

void Date::output() const
{
   std::cout << day << ' ';

   std::string months[12] = { "January", "February", "March", "April",
                              "May", "June", "July", "August",
                              "September", "October", "November", "December" };

   std::cout << months[month] << ' ' << year << '\n';
}

// let's chain the output into cout
std::ostream& operator<<(std::ostream& os, Date d)
{
   os << d.day << ' ';

   std::string months[12] = { "January", "February", "March", "April",
                              "May", "June", "July", "August",
                              "September", "October", "November", "December" };

   os << months[d.month] << ' ' << d.year;

   return os;
}

Enter the new year: 2002
Enter the new month: 2
Enter the new day: 7
7 March 2002
25 March 2007
7 March 2002
25 March 2007


I refined operator<< a bit, removing the automatic "end of line" so it works more like it does with PODs.
Last edited on
agent max wrote:
they can't be class member functions because they are never even defined.

So the OP needs to define the member functions for the class to be fully functional. And linkable.

agent max wrote:
if they were defined as members of the class, with prototypes inside the class, then they would be member functions and have access to all the variables.

A member function can be defined within the body of the class declaration, as the setters/getters are. Or they can be defined outside the class declaration body as I did with input(), output() and the friend function operator<<.

When defining a class member function outside the class the scope resolution operator (::) MUST be used. operator<< isn't one of the member functions, so no need to resolve the class scope.
Thanks for clarifying, @Furry Guy!

Although, I was told that it is not considered good practice to perform input/output from a class. Is this true? I mean, I know it's possible, but is it a good idea?
Topic archived. No new replies allowed.