### Introduction

In order to make a program respond properly to data input to it, we often need to make the program examine the data and have it respond in different ways depending on the data. We refer to the tests that we perform to analyze the data as conditions that are either true or false with respect to the data. These conditions are generally constructed with two types of operators:

• Relational operators - operators that allow you to compare to values. Possible relational operators are == (equal), != (not equal), < (less than), > (greater than), <= (less than or equal), and >= (greater than or equal).
• Logical operators - these operators allow you to form a complex combination of conditions. Possible logical operators are && (logical and), || (logical or) and ! (logical not).

Once we have constructed these conditions, we use them in an if or if-else statement. These statements allow us to specify a condition and then indicate a statement to be performed if the condition is true (if and if-else) and a statement to be performed if the condition is false (if-else only).

For cases where we expect to select among several options we can use a series of if-else statements, but we can also use the switch statement. The switch statement allows us to examine the value of an expression and pick an action or actions to take based on that value.

#### Uses of conditions

Some examples of how conditions are used to perform several types of actions:

• Checking data - a basic problem with any program is that the user may enter data that is not valid. Conditions allow us to test the data and report the validity of the data (and to avoid performing computations on inappropriate data).
• Performing complex calculations - many computations depend on certain data factors. For example, when calculating the tuition cost of a set of courses at UMD, we need to take into account the status of the student (undergraduate, graduate, med student) which alters the computation.
• Selecting statements to execute - as we are executing a program we may only execute certain statements under certain conditions. For example, a program interacting with UMD students might be programmed to ask whether a student is graduating only when they have more than a certain number of credits.
And there are many more uses of conditions.

### The Program

The program below is supposed to ask for the current date and the users birthday and then tells the user how old they are (in years) and whether they were born in Winter, Spring, Summer, or Fall. It also tells them what their age will on Jan 1 2000. Unfortunately, the program has some problems. For one thing, it doesn't calculate the age of the user correctly if the current date is earlier than the birth date and dosen't tell the user his age on Jan 1 2000 correctly if his birth date is after Jan 1. Furthermore the program doesn't check the data entered at all. Your job is to fix this program so that it responds correctly.

```
#include <stdio.h>

int main() {

int CurrMonth;
int CurrDay;
int CurrYear;
int BirthMonth;
int BirthDay;
int BirthYear;
int NumYears;
int MillYears;
printf("WELCOME TO THE TIME MACHINE \n ");

printf("Please enter the current date (MM/DD/YYYY): ");

/* Check dates entered here */

/* Age calculated incorrectly */
NumYears = CurrYear - BirthYear;
printf("You are %d years old ",NumYears);

/* Multiway using if-else, change to switch */
if ((BirthMonth == 12) || (BirthMonth == 1) || (BirthMonth == 2))
printf("and were born in Winter.  Brrr.\n");
else if ((BirthMonth >= 3) && (BirthMonth <= 5))
printf("and were born in Spring.\n");
else if ((BirthMonth >= 6) && (BirthMonth <= 8))
printf("and were born in Summer.\n");
else if ((BirthMonth >= 9) && (BirthMonth <= 11))
printf("and were born in Fall.\n");

/* Age calculated incorrectly */
MillYears= 2000-BirthYear;
printf("But you will be %d years old at the millenium", MillYears);
fflush(stdin);
getchar();
return 0;
}
```

### What to do

The program allows the user to enter their birth date and the current date but does not check what they enter. Add a test or tests that check the dates that the user enters to make sure both of the dates are legal. To make this easier you may assume all 12 months have 31 days (though you can make this more exact if you want). If either of the dates is illegal, the program should simply note the date is illegal and end.

A second thing to check is to make sure that current date is later than than birth date entered. If this condition fails tell the user to stop using a time machine and end.

Next, rewrite the portion of the code calculating the age so that it correctly determines the age if the current month and day combination is earlier than the birth month and day combination. Do this also for calculating the age on Jan 1 2000.

Finally, rewrite the set of if-else statements that print out whether the user was born in Winter, Spring, Summer or Fall using a switch statement. Your switch statement should work exactly the same as the set of if-else statements shown.

### What to turn in

Turn in a hard copy of your final program. Also, turn in sample output showing that your code calculates the age. You should include data that tests all aspects of your program including the checks it does to make sure the data is valid, the checks to correctly calculate the age, and the checks to print each of the seasons.