Sentinels

I'm trying to have the program exit the loop when the sentinel is entered and I'm pretty sure it has something to do with my errorCheck function saying m has to be greater than 1 and 12 but I don't know how to work around it. Please help! Pseudocode your responses, if possible. I just need a kick in the right direction.

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
116
117
118
#include <iostream>
#include <string>
using namespace std;

const int SENTINEL = -1;

void errorCheck (int, int, int, int&, int&, int&);
void year (int, int&);
void month (int, int, int&);
void day (int, int, int, int&);
void weekday (int, int, int, int);

int main ()
{//1
	int m=0, d=0, y=0, total=0;
	int errorm=0, errord=0, errory=0;

	while (m != SENTINEL)
	{//20
		cout << "Enter random 2-digit month and day and a 4-digit year." << endl;
		cout << "For example: 10 05 1989, then press enter." << endl;
		cin >> m >> d >> y;
		cout << endl;

		errorCheck (m, d, y, errorm, errord, errory);

		if (!errorm && !errord && !errory)
		{//3
			year (y, total);
			month (m, y, total);
			day (d, m, y, total);
			weekday (m, d, y, total);
		}//3
	}//2

	system("pause");
	return 0;
}//1

void errorCheck (int m, int d, int y, int& errorm, int& errord, int& errory)
{//4
	int error[] = {1, 12, 1990, 2099};
	errorm = (m < error[0] || m > error[1]);
	errory = (y < error[2] || y > error[3]);

	const int dayOfMonth[] = {31, 28, 31, 30, 30, 30, 31, 31, 30, 31, 30, 31};
	switch (m)
	{
	case 1:
		errord = d>dayOfMonth[0];
		break;
	case 2:
		errord = d>dayOfMonth[1];
		break;
	case 3:
		errord = d>dayOfMonth[2];
		break;
	case 4:
		errord = d>dayOfMonth[3];
		break;
	case 5:
		errord = d>dayOfMonth[4];
		break;
	case 6:
		errord = d>dayOfMonth[5];
		break;
	case 7:
		errord = d>dayOfMonth[6];
		break;
	case 8:
		errord = d>dayOfMonth[7];
		break;
	case 9:
		errord = d>dayOfMonth[8];
		break;
	case 10:
		errord = d>dayOfMonth[9];
		break;
	case 11:
		errord = d>dayOfMonth[10];
		break;
	case 12:
		errord = d>dayOfMonth[11];
		break;
	}
}
//4
void year (int y, int& total)
{//5
		total = (y%100)/4;
		total += y%100;
	if (y>=2000)
		total += 6;
}//5

void month (int m, int y, int& total)
{//6
	const int monthValue[] = {1, 4, 4, 0, 2, 5, 0, 3, 6, 1, 4, 6};
	total += monthValue[m-1];
	if ((y%4==0) && (y%400==0) || (y%100!=0))
	{//7
		if (m==1 || m==2)
			total -= 1;
	}//7
}//6

void day (int d, int m, int y, int& total)
{//8
		total += d;
}//8

void weekday (int m, int d, int y, int total)
{//10
	total %= 7;
	static string monthName[] = {"January ","February ", "March ", "April ", "May ", "June ", "July ", "August ", "September ", "October ", "Novemeber ", "Decemeber "};
	static string weekday[] = {"Monday.", "Tuesday.", "Wednesday.", "Thursday.", "Friday.", "Saturday."};
	cout << monthName[m-1] << d << ", " << y << " is a " << weekday[total-1] << endl << endl;
}//10 
Last edited on
Every time you run your while-loop, you need to reset the values of your error-variables to '0'.

Your main doesn't return a value at the end.

I suggest putting an if-statement immediately after your user-input to check if m == SENTINEL. If it's true, call a break for the loop. This means you need to change the boolean value for your while-loop.
Last edited on
@sherre02 One of my later functions returns the weekday, which is the overall point of the program, I just didn't want to include the whole program here. But also, do I need to include a return statement in my main? I thought that main returns 0 by default.
Last edited on
closed account (N36fSL3A)
You need to return a value in main. It will not default.
> You need to return a value in main. It will not default.

No. If control reaches the end of main() without encountering a return statement, the behaviour is as if there was a return 0 ; at the end.

This has always been so in C++; also has been so in C since 1999.
closed account (N36fSL3A)
In VC++, it won't let me make one without returning 0.
Topic archived. No new replies allowed.