Help needed! if-then-else structure

First off, the program is part of an assignment due tomorrow for my intro to C++ class. Here is the assignment prompt:





CSIT 575 – Programming Fundamentals for Computer Science Lab #3A

Objectives:

To learn to code, compile and run a program containing SELECTION structures

Assignment:

A. Plan and code a C++ program utilizing the if-then-else structure to solve the following:

B. Write an interactive C++ program to determine the day of the week for any given date from 1900-2099. Following are the steps you should use:

C. Input the date in 3 separate parts, month, day and 4-digit year. Error check to make sure the month is between 1 and 12, the day between 1 and 31, and the year between 1900-2099. If there is an error, identify the type of error and stop the program.

D. If the data is good, divide the last two digits of the year by 4. Store the quotient (ignoring the remainder) in Total. For example, for 1983, divide 83 by 4 and store 20 in Total.

E. Add the last 2 digits of the year to Total.

F. Add the two digits of the day of the month to Total.

G. Using the following table, find the "value of the month" and add it to Total.

January = 1 May = 2 September = 6
February = 4 June = 5 October = 1
March = 4 July = 0 November = 4
April = 0 August = 3 December = 6

H. If the year is 2000 or later, add 6. If the year is 2000 exactly and the month is January or February, subtract 1.

I. Then, determine if the year is a leap year. A leap year is divisible by 4 but not divisible by 100. If it is a leap year and the month is January or February, subtract 1. (The year 2000 is NOT a leap year).

J. Find the remainder when the Total is divided by 7. Use the remainder to find the day:

1= Sunday 3 = Tuesday 5 = Thursday
2 = Monday 4 = Wednesday 6 = Friday
0 = Saturday

Input

Your three initials, the month, day, and 4-digit year of the date to analyze.

Output

All of the input fields, a message if the data is OK and the day of the week of the date. Output the date and the appropriate error message if the data is bad.

Turn in

Program listing and program output.
Test today’s date, your birthdate, and the dates your instructor gives you and error conditions.





I'm having trouble with the output. I am trying to get an output message of "Data is NOT OK" if the day is not between 1-31, month not between 1-12, and year not between 1900-2099 and an output of "Data is OK" if the day is between 1-31, month between 1-12, year between 1900-2099. The output always comes out as "Data is OK". I think it might be a precedence problem.

This is what I have so far. I know its a bit messy, but Im still new to C++. Any help would be appreciated! primarily on the else/ else-if statement.





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

int main ()
{
	string initials;
	string mystring;
	int month;
	int day;
	int year;
	int wtf;

	cout << "Enter your initials: ";
	cin >> initials;

	cout << "Enter the month: ";
	cin >> month;

	cout << "Enter the day: ";
	cin >> day;

	cout << "Enter the year: ";
	cin >> year;

	if (month >12 && day >31 && year >2099)
		
		cout << "Data is NOT OK";

	else if (month >=1 <=12 && day >= 1 <=31 && year >= 1900 <=2099)
		
		cout << "Data is OK";
	
	cout << endl;

	cout << "wtf is: ";
	cin >> wtf;
}
Going through this slowly step by step might help you get a better hang of it. Why don't you try just the first part of C: try writing a small code snippet that will read the month into an integer then check if that integer is valid.

It's close to what you have, but if you do that I can give you specific pointers on where you are misunderstanding syntax or anything if that is the issue.
Ok, I got past step C but I'm having trouble with step D. How do you store the quotient (ignoring the remainder) in Total and how do you divide just the last two digits of the year by 4..
The / operator on integers will give you the quotient without the remainder by default.

To divide the last two digits of a year you can creatively use division ignoring the remainder to discard the last two digits. Here's a hint: 29 / 10 = 2, and 475 / 100 = 4 when discarding remainders.
Thank you. What I want to know is how do you get the program to analyze the input year, divide just the last 2 digits of the year, and store it without me having to prompt the user to divide the last 2 digits of the year. Reason Im asking is because Im pretty sure the professor only wants the user to input initials, day, month, year while the program does the rest :/
I didnt make much sense of your response Zhuge, please clarify. Can you use an example, say, what if the year is 2016.

Please help!
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <iostream>
#include <string>
using namespace std;

int main ()
{
	string initials;
	int month;
	int day;
	int year;
	int Total1;
	int Total2;
	int Total3;
	int Total4;
	int Total5;
	int Total6;
	int Total7;
	int LeapYear;
	int year2;

	cout << "Enter your initials: ";
	cin >> initials;

	cout << "Enter the month: ";
	cin >> month;

	cout << "Enter the day: ";
	cin >> day;

	cout << "Enter the year: ";
	cin >> year;

	cout << endl;

	cout << "Your initials are: " << initials << endl;

	cout << "You entered: " << month << " " << day << " " << year << endl;

	{
	if (month >12 || day >31 || year >2099)
		
		cout << "Data is NOT OK";

	if (month >12 || day >31 || year >2099)

		system ("pause");

	else
		
		cout << "Data is OK";

	}
	
	cout << endl;
	cout << endl;

	{
	if (year >= 2000) 

		Total1 = year - 2000;

	else

		Total1 = year - 1900;
	}	
	
	cout << "Your total after dividing the last 2 digits of the year by 4 and storing the quotient (ignoring the remainder) is: " << Total1 << endl;

	Total2 = Total1 / 4;

	cout << "Your total after dividing the last 2 digits of the year by 4 is: " << Total2 << endl;

	Total3 = Total1 + Total2;
	Total4 = Total3 + day;

	if (month==1 || month==10)

		Total5 = Total4 + 1;

	else if (month==5)

		Total5 = Total4 + 2;

	if (month==8)

		Total5 = Total4 + 3;

	else if (month==2 || month==3 || month==11)

		Total5 = Total4 + 4;

	if (month==6)

		Total5 = Total4 + 5;

	else if (month==9 || month==12)

		Total5 = Total4 + 6;

	if (month==4 || month==7)

		Total5 = Total4 + 0;

	cout << "Your total after adding the last 2 digits of the year is: " << Total3 << endl;

	cout << "Your total after adding the 2 digits of the day of the month is: " << Total4 << endl;

	cout << "Your total after finding the 'value of the month' is: " << Total5 << endl;

	if (year < 2000)

		Total6 = Total5;

	if (year > 2000)

		Total6 = Total5 + 6;

	else if (year==2000 && month==2 || month==1)

		Total6 = Total5 - 1;

	cout << "Your total after step H. is: " << Total6 << endl;

	LeapYear = year%4;

	if (LeapYear==0 && month==1 || month==2)
	
		Total7 = Total6 - 1;

	else if (LeapYear!=0 && month!=1 || month!=2)

		Total7 = Total6 + 0;
	
	cout << "Your total after step I. is: " << Total7 << endl;

	cout << endl;

	year2 = Total7%7;

	if (year2==1)
	
		cout << "This person was born on a Sunday.";
	
	else if (year2==2)

		cout << "This person was born on a Monday.";

	if (year2==3)

		cout << "This person was born on a Tuesday.";

	else if (year2==4)

		cout << "This person was born on a Wednesday.";

	if (year2==5)

		cout << "This person was born on a Thursday.";

	else if (year2==6)

		cout << "This person was born on a Friday.";

	if (year2==0)

		cout << "This person was born on a Saturday.";

	cout << endl;

	cout << endl;

	system ("pause");

}


Finalized assignment, did some reading and it really helped.

If anyone finds anything that can be improved, please let me know. Passing out haha.
It a Basic programing Language.
Topic archived. No new replies allowed.