Struggling with this code

Hey guys, I am struggling with this code. . I have gotten this far, and can't seem to pull it off. Please see if you can find out what the issues are.

This is suppose to take in date, and check to make sure the date is valid and return one of the cases according to the validity of the date. It is also taking in Leap year into effect. .after it does that, it is simply suppose to ask again for another date.

See if you can figure this out. I have been trying for 3 days now without any success.

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
109
110
111
112
113
114
115
#include<iostream> 
#include<conio.h>
#include<string>
using namespace std;

void getDate(int& mon, int& day, int& year);
int ckDate(int mon, int day, int year);	
void displayMsg(int message);

int main()
{
	int mon, day, year, message;
	string date;
	

	while(cin)
     {
		getDate(mon,day,year);			// call function getDate
		int message=ckDate(mon,day,year);		// cal function ckDate
		cout << mon << "/" << day << "/" << year << "-";
		displayMsg(message);					//call function displayMsg
		
     }
	
	_getch();
	return 0;

}  

void getDate(int& mon, int& day, int& year)
{	cout << "\n\nEnter a date (mm/dd/yyyy): ";
	cin >> mon;
	cin.ignore(100, '/');
	cin	>> day;
	cin.ignore(100, '/');
	cin >> year;
	
	
}	

int ckDate(int mon, int day, int year)
{	
	
	 bool isLeapYear = false;
	 int resultNum = 0;

           if ((year <= 999) || (year > 10000))
			resultNum = 1;//attempting to change from multiple return statements with an integer to a variable resultNum.

               
           
           if ((year % 4 == 0 && year % 100 != 0) || (year % 4 == 0 && year % 100 == 0 && year % 400 == 0))
               isLeapYear = true;

           
           switch (mon)
           {
               case 1:
                   if (day < 1 || day > 31) 
					   resultNum = 3;
				//resultNum = 3; attempting to assign variable to 3 
				//return resultNum;//returning it to displayMsg

                   break;
               case 2:
                   if (isLeapYear && (day < 1 || day > 29)) 
					   resultNum = 5;
                   if (!isLeapYear && (day < 1 || day > 28)) 
					   resultNum = 6;
                   break;
               
               case 3: // the month was not 1-12 so return 2
					if (mon < 1 || mon > 12) 
						resultNum = 2;
					break;
				   return resultNum;//appears to work correctly


           }
          	return resultNum;//appears to work correctly	
				return 0;
	}



void displayMsg(int message)
{
	switch(message)
	{
	case 0:
		cout << " Good Date";
		break;
	case 1:
		cout << " Bad Year";
		break;
	case 2:
		cout << " Bad Month";
		break;
	case 3:
		cout << " Bad Day not 1-31";
		break;
	case 4:
		cout << " Bad Day not 1-30";
		break;
	case 5:
		cout << " Bad Day not 1-29";
		break;
	case 6:
		cout << " Bad Day not 1-28";
		break;
	}

}

Last edited on
You're over complicating this for your self. Don't make "displayMsg()" a separate function, evaluate mon, day and year from with in "ckDate()" and display the messages accordingly.
yeah I know, but am suppose to keep it separated this time around.

I just really have gotten to the point where I have no idea what to play around with anymore
Think about the switch statement for "mon" on line 56.
funny you say that as that was what I I have staring at for the past 20 min, , not sure what would need to happen there though.

can you elaborate?
If you know how to step through your code with a debugger, I suggest doing so and paying attention to the value for mon. Barring that, just consider entering in a date of 05/09/2000 (assuming mm/dd/yyyy) and walk through your code keeping "5" in mind as the value for "mon". Try paying attention to every instance that "mon", and by extention in this case "5", is being used.
not sure how to do that exactly
To use a debugger would depend on your IDE but the basic idea is just to set a break point in your code and have it run in debug mode. With Visual Studios it's just Debug -> Start Debugging. From that point F10 and F11 will let you step through your code line by line. F10 will step over code and F11 will step into code. For example stepping over code will just execute functions, while stepping into code will try to enter the function so you can step through it as well. This is assuming you have Visual Studios, I have no idea how debugging works with other IDEs.

However you don't -need- to use the debugger for your code, as I said you can run through it in your head, keeping track of what "mon" is doing.

I'm sorry if I'm being irritating, btw, I'm just trying to nudge you in the right direction for finding a problem because it's great practice to solve these sorts of things yourself.
Last edited on
yeah I am running studio.

I am running through it in my head, and the only part that I see 'mon' come into use with the value of 5 would be here

1
2
3
4
case 3: // the month was not 1-12 so return 2
					if (mon < 1 || mon > 12) 
						resultNum = 2;
					break;


trust me you are not being irritating at all, I appreciate that you are helping me out here
Keep looking at your switch statement and its relation to "mon". Hopefully it will click after you mull it over enough. :)
Last edited on
ok using the debugging technique which you said to, and seeing that it will always return a 0 at line 80. . but am still not getting why it is doing this
I think I have a good idea as to why you may be missing the problem, and for that I have this to say: Stop thinking about what you planned for the switch statement to do so you can look at what it's actually doing. Maybe that will help you some.
ok well the only thing that I can come up with at this point is that the Switch (mon) is testing mon, but in fact inside the switch there is also testing for days.

Is this by any chance what I am missing?
What does the switch do if "mon" was, say, 7?
Last edited on
it would go down the cases and according to the 'if' statements, it would give a result.

So for 7, when it hits case 3 it would check to see if it is less then 1 and greater then 12, and if so then it would return case 3 meaning resultNum = 2
but in this case 7 is within the rule, therefore it wouldn't return a month error.

Am I missing something completely obvious here?
Ah, I see now. Read up on switches again. Basically the switch is being skipped entirely because 7 is not a case the switch is designed to handle. :)
well I figured that that is what you wanted me to say, but there are the if statements on there that choose that switch if it is true
ok I see exactly what you are saying now. . I still have a problem though. . ok I can get it to fall under certain categories for all of them, I can't get it to say bad month though
Topic archived. No new replies allowed.