Functions/Testing them manually

I have a Project that I am working with functions, where I need to write these functions and have the code to test these functions manually. I have the following functions.

void testMenu(); //post-condition is displayed for user to choose
bool isLeapYear(int year);
int getCenturyValue(int year);
int getYearValue(int year);
int getMonthValue(int year, int month);
int dayOfWeek(int year, int month, int day);
std::string dayOfWeek (int day); // pre-condition: day has integer

0,1,2,3,4,5, or 6.
// post-condition: the name of the day of the week is returned as a std::. If day has 0 then Sunday returned, or 1 then Monday is returned and so on.

I am new to programming and feel that I am stuck, not sure if I am doing it correctly. Hopefully I can be steered in the right direction.
This is what I have so far:


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
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "stdafx.h"
#include <iostream>

int main() {
	
	using namespace std;
	
	void testMenu(); //post-condition is displayed for user to choose
	bool isLeapYear(int year);
	int getCenturyValue(int year);
	int getYearValue(int year);
	int getMonthValue(int year, int month);
	int dayOfWeek(int year, int month, int day);
	
	
	int choice;
	int day, month, year;

	do {
		
		testMenu();
		cout << "Please choose from the menu: ";
		cin >> choice;
		
		switch (choice) {

		case 1: //check if a given year is a leap year
			cout << "Please enter a year: ";
			cin >> year;
			if (isLeapYear(year))
				cout << "Year " << year << " is a leap year." << endl;
			else
				cout << "Year " << year << " is NOT a leap year." << endl;
			break;

		case 2: // calculate the century value of a given year
			cout << "Please enter a year: ";
			cin >> year;
			cout << "The century value is: " << getCenturyValue(year) << endl;
			break;

		case 3: //calculate the year value of a given year
			cout << "Please enter a year: ";
			cin >> year;
			cout << "The year value is: " << getYearValue(year) << endl;
			break;

		case 4: // calculate the month value of a given month in a given year
			cout << "Please enter a year and month: ";
			cin >> year >> month;
			cout << "The month value is: " << getMonthValue(year, month) << endl;
			break;

		case 5: // calculate the day of the week of a given date
			cout << "Please enter a year, a month, and a day: ";
			cin >> year >> month >> day;
			cout << "The day of the week is: " << dayOfWeek(year, month, day) << endl;
			break;

		case 6: // print out the name of a given day of the week
			cout << "Please enter a day of the week (0 for Sunday, 1 for Monday, 2 for Tuesday, etc): ";
			cin >> day;
			cout << "The name of the day of the week is: " << dayOfWeek(day) << endl;
			break;

		case 7: // ask user if they tested all functions
			cout << "Did you test all of the functions? If not, then please rerun the program and do so.\n";
			break;

		default:
			cout << "Wrong option. Please choose from the menu.\n";
			break;
		}

		system("pause");
	} while (choice != 7);
}
void testMenu() 
{
	
	cout << " \t\t**********Test Menu**********" << endl;
	cout << "* 1) isLeapYear *" << endl;
	cout << "* 2) getCenturyValue *" << endl;
	cout << "* 3) getYearValue *" << endl;
	cout << "* 4) getMonthValue *" << endl;
	cout << "* 5) dayOfWeek (year, month, day) *" << endl;
	cout << "* 6) dayOfWeek (day) *" << endl;
	cout << "* 7) Quit *" << endl;
	
}

bool isLeapYear(int year) 
{
	if ((year % 400 == 0) || ((year % 100 != 0) && (year % 4 == 0) ))
		return true;
	else
		return false;
}

int getCenturyValue(int year) 
{
	year = year / 100;
	year = year % 4;
	year = 3 - year;
	return year * 2;
}

int getYearValue(int year)
{
	int tempyear;
	year = year % 100;
	tempyear = year / 4;
	return year + tempyear;
}

int getMonthValue(int year, int month) // not sure on this section or below
{

}

int dayOfWeek(int year, month, day)     //here 
{
}

int std::string dayOfWeek(int day);  // or here
{
}



Last edited on
Hello bnolan22,

Welcome to the forum.

Lines 6 - 13 I would put above main to let everything in this file know about the functions. Line 6 I would avoid using as it WILL get you in trouble some day.

Above line 19 you could put the individual function that you want to test followed by "return 0;", so you only test that function. If the function takes any parameters they will have to be set before you call the function.

Other things I have done is to comment out what I do not need to test what I do need. Or move the "return 0;" around where needed.

Some people will say something about "#include "stdafx.h". Not a big problem, but it does tell me that you are using VS as your IDE which is helpful sometimes. It is best if you leave this line out when you post your program though.

Note: lines 16 and 17 you should initialize your variables. I found that {} is the easiest way.
1
2
int choice{};
int day{}, month{}, year{};

This will initialize the variables to zero or you could put a number between the {}s. This makes it easier when testing the functions.

Hope that helps,

Andy
Hello bnolan22,

For your last three functions you will have to give me some idea of what you are expecting to get from these functions before I can help you with the code.

For the function "getMonthValue". What value? Or what value are you expecting to return from the function? Based on the parameters that you are sending to the function it does not make much sense.

Andy
Topic archived. No new replies allowed.