Going out of scope and I don't know how to fix it.. Help me!

Okay, so I'm suppose to modify and older program so that it increments the date set by the user.. When the days hit 30, the month should incrememnt.. When the month hit's 13 then the year should increment... Then it should display these..

As of now, the loop ends up displaying a bunch of junk.. I'm thinking this is because I'm calling the code out of scope.. Anyways, here's what I got :


Header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef string_H_INCLUDED
#define string_H_INCLUDED

class Date
{


public:
    Date(int = 0, int = 0, int = 1); // constructor
    void getDate(int, int, int);
    void display();
    void nextDay(int, int, int);



private:

    int month;
    int day;
    int year;
};

#endif // LAB2HEADER_H_INCLUDED 



Implementation file:
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>
#include "string.h"

using namespace std;

Date::Date(int mo, int da, int ye)
{
};


void Date::getDate(int m, int d, int y)
{
    cout << "Enter month : ";
    cin >> m;
    month = ( m = 0 && m < 13) ? m : 1;

    cout << "Enter day : ";
    cin >> d;
    day = ( d = 0 && d < 33) ? d : 1;

    cout << "Enter year : ";
    cin >> y;
    year = ( y = 0) ? y : 2016;
}

void Date::display()
{
    cout << "Month: " << month << "//////" << "Day: " << day << "//////" << "Year: " << year << endl;
}

void Date::nextDay(int m, int d, int y)
{
    int x;
    for (x=1; x<=367; x++)
    {
        d = day = day+x;
        cout << day;

        if (day == 30)
        {
           m = month = month+1;
            cout << month;
        }

        if (month == 13)
        {
            y = year == year+1;
            cout << year;
        }
    }
}



main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include "string.h"

using namespace std;

int main()
{

Date e;
Date t1(1,1,1);

t1.getDate(0, 0, 1);

t1.display();

e.nextDay(0, 0, 0);
};




Any suggestions?? Thank you!
A style suggestion:
Write a constant operand (if you have one) to the left side of operator whenever writing a test for equality (==).

1
2
3
4
5
6
7
8
9
month = ( m = 0 && m < 13) ? m : 1;

// is same as
m = 0;
month = ( 0 && m < 13) ? m : 1;

// is same as
m = 0;
month = 1;


What is the nextDay() supposed to do? To advance the date by one? Why does it take input that does nothing? Why does it print 367 lines of digits?

When 13==month you do advance the year, but leave the month at 13? Same for day.
What are you trying to do with this line y = year == year+1;?


Yes, nextDay is suppose to increase day by 1 and then when the day hits 30, increase the month by 1, and then when the month hit's 13, increase the year by one.. I realize now that showing each day increment is pointless though, but I do want to show the month when it is increased and the year.

What I was trying to do was increment year by one.. I first had it as

1
2
3
4
if (month == 13)
{
year = year+1
};


or something along those lines.. But with that, I still got junk.. So I figured maybe by using y, it would bring year into scope.. That didn't end up being the case -_-

edit: ignore the double == heh... So sleep deprived right now, it might explain why I'm not able to get this atm.


double edit: I ended up doing it with an ugly if if if if if loop, heh.. But at least it works! Thanks for the help you guys!
Last edited on
Topic archived. No new replies allowed.