constructor function

Hello everyone, with this program I wanted to display 3 dates, but i'm lost with how to put day, day2, day3 in the main and return those 3 values. the error I get is reinitialization of d.
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
97
98
99
100
101
102
103
104
105
106
107
108
 #include<iostream>
#include<iomanip>
#include<cstring>
#include<string>

using namespace std;

// Static member variable type string to translate from integer to month-day format

// Class declaration
class DayOfYear
{
public:
	int day;
	int day2;
	int day3;
	string Month;

	// Constructor function
	DayOfYear(int dayEntered)
	{
		day = dayEntered;
		

	} // end constructor function

	void print()
	{

		if (day,day2,day3 >= 1 && day,day2,day3 <= 31)
		{
			cout << "January " << day << endl;
		}

		if (day, day2, day3 >= 32 && day, day2, day3 <= 59)
		{
			cout << "February " << (day, day2, day3 - 31) << endl;
		}

		if (day, day2, day3 >= 60 && day, day2, day3 <= 90)
		{
			cout << "March " << (day, day2, day3 - 59) << endl;
		}

		if (day, day2, day3 >= 91 && day, day2, day3 <= 120)
		{
			cout << "April " << (day, day2, day3 - 90) << endl;
		}

		if (day, day2, day3 >= 121 && day, day2, day3 <= 151)
		{
			cout << "May " << (day, day2, day3 - 120) << endl;
		}

		if (day, day2, day3 >= 152 && day, day2, day3 <= 181)
		{
			cout << "June " << (day, day2, day3 - 151) << endl;
		}

		if (day, day2, day3 >= 182 && day, day2, day3 <= 212)
		{
			cout << "July " << (day, day2, day3 - 181) << endl;
		}

		if (day, day2, day3 >= 213 && day, day2, day3 <= 243)
		{
			cout << "August " << (day - 212) << endl;
		}

		if (day, day2, day3 >= 244 && day, day2, day3 <= 273)
		{
			cout << "September " << (day, day2, day3 - 243) << endl;
		}

		if (day, day2, day3 >= 274 && day, day2, day3 <= 304)
		{
			cout << "October " << (day, day2, day3 - 273) << endl;
		}

		if (day, day2, day3 >= 305 && day, day2, day3 <= 334)
		{
			cout << "November " << (day, day2, day3 - 304) << endl;

		}
		if (day, day2, day3 >= 335 && day, day2, day3 <= 365)
			{
				cout << "December " << (day, day2, day3 - 334) << endl;
			}


		} // end print function


	}; // end Class DayOfYear



	int main()
	{
		// choose a day to print
		DayOfYear day(78), day2(98);
		// print day 78 in Month and Day format
		cout << day(78) << day2(98);

		system("Pause");
		return 0;

	}
So what do you think does the comma do on e.g. on line 30?

Line 103 in this way is currently nonsense. You have a print() function, so use that:

1
2
3
4
5
6
7
8
9
10
11
12
	int main()
	{
		// choose a day to print
		DayOfYear day(78), day2(98);
		// print day 78 in Month and Day format
		day.print();
		day2.print();

		system("Pause");
		return 0;

	}


day2, day3 and Month are of no use in your class. I suggest that you remove them.
Hello ddaniel10,

An observation here. In the class all the variables are in the "public" section. This defeats the purpose of the class protecting the variables. As "public" variables the whole program has access to them.

Just so you know your comment // Constructor function would be better as // Overloaded Constructor function . It is more descriptive of what it is.

On line 94 watch your indenting. I almost missed seeing the closing brace of the class, if it were not for the comment. BTW nice job on the comments. The extra blank lines are not really necessary, but not a problem.

Hope that helps,

Andy
The problem is that I want to insert a few values and print them, instead of just one value.
We can have many objects; use a different object for each value. For example:

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
#include <iostream>

struct day_of_year // non-leap year
{
    explicit day_of_year( int d = 1 ) : day(d)
    {
        if( day < 1 ) day = 1 ;
        else if( day > 365 ) day = 365 ;
    }

    std::ostream& print( std::ostream& stm = std::cout ) const
    {
        if( day < 1 || day > 365 ) return stm << "invalid day of year " << day << '\n' ;

        const int NMONTHS = 12 ;
        static const int days_in_month[NMONTHS] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } ;
        static const char* const month_name[NMONTHS] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                                                         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" } ;
        int d = day ;
        for( int i = 0 ; i < NMONTHS ; ++i )
        {
            if( d <= days_in_month[i] )
                return stm << month_name[i] << ' ' << d << " (#" << day << ')' ;

            else d -= days_in_month[i] ;
        }
        
        return stm ; // placeholder to supress a finicky compiler warning
    }

    std::ostream& println( std::ostream& stm = std::cout ) const
    { return print(stm) << '\n' ; }

    private: int day ;

    friend std::ostream& operator<< ( std::ostream& stm, day_of_year d )
    { return d.print(stm) ; }
};

int main()
{
    const day_of_year day(25), day1(150), day2(350) ;

    day.println() ;
    day1.println() ;
    day2.println() ;

    std::cout << "\n day: " << day << '\n'
              << "day1: " << day1 << '\n'
              << "day2: " << day2 << '\n' ;
}

http://coliru.stacked-crooked.com/a/405ddaf9979d88d0
First an ugly distraction:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <map>
#include <string>

int main()
{
  const std::map<int,std::string> names {{0,""},{3,"foo"},{5,"bar"},{10,"gaz"}};
  for ( int x=1; x <= names.rbegin()->first; ++x ) {
    auto m = names.lower_bound( x );
    auto p = m;
    --p;
    std::cout << x << ' ' << x - p->first << ' ' << m->second << '\n';
  }
}


Now a comment on your code:
1
2
3
4
5
6
7
8
9
10
11
if ( day >= 1 && day <= 31)
{
  cout << "January " << day << endl;
}

if (day >= 32 && day <= 59)
{
  cout << "February " << (day - 31) << endl;
}

// and then some 

Your if-statements are independent. Your program has to evaluate all of them.

Let say that day is <= 31. The first if is true and January shows. Nevertheless, your print() tests the remaining 11 cases, even though we already know that they are false.

There is else:
1
2
3
4
5
6
7
8
9
if ( day >= 1 && day <= 31)
{
  cout << "January " << day << endl;
}
else if (day >= 32 && day <= 59)
{
  cout << "February " << (day - 31) << endl;
}
else if ( // and then some 

The else-parts are skipped, if the IF is true. Hey, we can simplify the conditions:
1
2
3
4
5
6
7
8
9
10
11
if ( day < 1 ) return; // skip the rest

if ( day <= 31 )
{
  cout << "January " << day << endl;
}
else if ( day <= 59 )
{
  cout << "February " << (day - 31) << endl;
}
else if ( // and then some 

Topic archived. No new replies allowed.