class member functions and output..HELP!

ok here's my deal... i'm a student learning classes and my assignment has me using a default constructor to create an object of class Date and then using a class member function to output it three different ways.. the cout<< stuff is easy enough.. the part that's tripping me up is feeding the class data into the function and displaying it... i've added strategic cout's to confirm the object works with parameters entered as well as with the default values, and spent hours trying to get the member function to spit out an answer... finally i got code that's SYNTACTICALLY correct and doesn't freak out the compiler, but the output is ALL MESSED UP.... here's my code...

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
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <windows.h>
//***************************************************************
//  Usings
//*****************************************************************
using namespace std;
//**************************************************************
//Classes and Structures
//***************************************************************
class Date
{
    private:    //private variable declaration
       int month,
           day,
           year;
          
    public:   //public variable declaration
           int a;
           int mo,
               dy,
               yr;
           Date(int mo = 1, int dy = 1, int yr = 2001)
           {
                    month=mo;
                    day = dy;
                    year = yr;
                    if(mo != 1 && dy != 1 && yr != 2001)
                    {
                        cout<<"the constructor is running"<<endl;
                    }
                    else
                    {
                        cout<<"the constructor is running in default"<<endl;
                    }
                    cout<<mo<<"/"<<dy<<"/"<<yr<<endl;

           }
           void displayDate(Date now);

};
//***************************************************************
// Function Prototypes
//***************************************************************
 void Date::displayDate(Date now)
{ 
                int a;
                cout<<"displaydate is running"<<endl;
                cout<<" today's date is:"<<endl;
                cout<<now.mo<<"/"<<now.dy<<"/"<<now.yr<<endl;
                cout<<"or maybe it's"<<now.mo<<"-"<<now.dy<<"-"<<now.yr<<endl;
                Sleep(3000);
                cout<<"press 1 to exit"<< endl;
                cin>>a;
                while(a !=1)
                {
                     cout<<"try again"<<endl;
                     cin>>a;
                }
}
         
//***************************************************************
//  Main Function
//***************************************************************
int main()
{
    int a;
    Date then;
    Date now(5,3,2012);
    Date dummy(1,1,0);
    cout<<"main is running?"<<endl;
    cout<<"miancopy"<<now.mo<<"/"<<now.dy<<"/"<<now.yr<<endl;
    void displayDate(Date now);
    Sleep(3000);
     cout<<"press 1 to exit"<< endl;
     cin>>a;
    while(a !=1)
    {
            cout<<"try again"<<endl;
            cin>>a;
           
    }
    
return(0);         
};// End of main function

//***************************************************************
//  Helper Functions
//***************************************************************
 


and let's see if i can add the output...

 the constructor is running in default
1/1/2001
the constructor is running
5/3/2012
the constructor is running in default
1/1/0
main is running?
miancopy2009252814/2359064/8
press 1 to exit




the "miancopy" line is the erroneous output, as you can guess...i instantiated three different objects as test subjects, but i'm only trying to get one to output the "5/3/2005" object AKA Date.now
You never set the mo, dy, yr members
In the constructor you are showing the parameters passed.

display is a method, ¿why does it receive a Date object, insead of acting on itself?
Line 74 is a function declaration, not a function call.
Last edited on
1. mo, dy, and yr are initialized in the class declaration and set by the constructor
2. displayDate is the name of the function.
3. and my compiler throws a fit everytime i try to remove the void from line 74 in order to MAKE it a call.... i get this error when i remove the void:
expected primary-expression before "now"
`displayDate' undeclared (first use this function)

i've bounced a decent amount of code in and out of this to try and fix it..
if display is a command in c++ our rof hasn't covered that yet so i apologize if that is confusing, but displayDate is the name i gave to the function, i'm not using it as a method for anything...
C Theroux wrote:
1. mo, dy, and yr are initialized in the class declaration and set by the constructor


No, they're not. month, day, and year are assigned values in the constructor. mo, dy, and yr are not. Notice that the constructor parameters named mo, dy, and yr do not refer to those declared in the class definition and hide their names. Each instance of your Date class has 7 int variables. Only 3 of those are given values in the constructor. The variables are declared at lines 15-17 and lines 20-23.


C Theroux wrote:
2. displayDate is the name of the function.


displayDate is the name of a member function of Date (a method.) See line 40. Note how displayDate is declared inside the definition of Date. Furthermore, when you defined displayDate (line 46) you used the Date:: scope qualifier which indicated you were defining a member function of Date.

My suggestion to you is to change the prototype of displayDate to:

void displayDate() const; where it's declared in the class defintion.

and change the definition to

1
2
3
4
void Date::displayDate() const
{
     // code to display date using month, day and year.
}


And completely remove lines 20-23.


C Theroux wrote:
3. and my compiler throws a fit everytime i try to remove the void from line 74 in order to MAKE it a call.... i get this error when i remove the void:


Just removing void isn't enough.

If you made the changes above the call becomes simply:

now.displayDate();
Last edited on
ok i've made a few changes and got output from the function displayDate, but it's still in error..
here's the new code, at least the section between the function declaration in the class to the call in main..
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
           }
           void displayDate(Date now);

};
//***************************************************************
// Function Prototypes
//***************************************************************
 void Date::displayDate(Date now)
{ 
                int a;
                cout<<"displaydate is running"<<endl;
                cout<<" today's date is:"<<endl;
                cout<<now.mo<<"/"<<now.dy<<"/"<<now.yr<<endl;
                cout<<"or maybe it's"<<now.mo<<"-"<<now.dy<<"-"<<now.yr<<endl;
                Sleep(3000);
                cout<<"press 1 to exit"<< endl;
                cin>>a;
                while(a !=1)
                {
                     cout<<"try again"<<endl;
                     cin>>a;
                }
}
         
//***************************************************************
//  Main Function
//***************************************************************
int main()
{
    int a;
    Date then;
    Date now(5,3,2012);
    Date dummy(1,1,0);
    cout<<"main is running?"<<endl;
    cout<<"miancopy"<<now.mo<<"/"<<now.dy<<"/"<<now.yr<<endl;
    now.displayDate(now);
    Sleep(3000);
     cout<<"press 1 to exit"<< endl;
     cin>>a;
    while(a !=1)
    {


and here's the new output:
the constructor is running in default
1/1/2001
the constructor is running
5/3/2012
the constructor is running in default
1/1/0
main is running?
miancopy2009252814/2359064/8
displaydate is running
 today's date is:
2009252814/2359064/8
or maybe it's2009252814-2359064-8
press 1 to exit
i've tried displayDate(now); before as well and it still threw a fit.. the above NEW code displays SOMETHING at least, but the data is wrong...
and sorry if i sound crabby i've been up for two days fixing this and attending classes
You may want to reread what I posted above. Looks like I was editing while you were posting.
GOTCHA! BINGO! ok i see what you were talking about declaring mo,dy,yr in the class itself was pointless since the constructor was using them as local variables and they never "got out" of the constructor!and so as extra variables they were being displayed with the data i wanted and getting it all garbled up! (and the extra int a was me not erasing all of something else from an earlier version i was working on... the "press 1 to continue" clause that i at one point inserted EVERYWHERE to make sure i saw every step before it vanished )... also, so far i hadn't used the const "identifier" (for lack of a more appropriate word) in that manner before, always used it inside the variable declaration during the function definition.. never OUTSIDE like that... so when i tried to make the reference const it kept giving me errors.. and now i have a little bit better understanding of why... since it's calling multiple individual data from a class, i'm GUESSING you can't try to declare them constants separately, so putting it outside declares the whole thing at once... is that right? again sorry for any attitude... and thank you!!!! i'll stare at this for about another hour before bed and again in the morning to remind myself of the changes and the why's, where's, and how's, before jumping back on here and making a fool of myself showering you guys with more compliments LOL
Topic archived. No new replies allowed.