Computer Science 1511
Computer Science I

Laboratory Assignment 7
Conditions and Selection Statements
Due at the end of Lab


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:

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:

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;
  char Discard;
  printf("Please enter the current date (MM/DD/YYYY): ");
  scanf("%d %c%d %c%d",&CurrMonth,&Discard,&CurrDay,&Discard,&CurrYear);

  printf("Please enter your birth date (MM/DD/YYYY): ");
  scanf("%d %c%d %c%d",&BirthMonth,&Discard,&BirthDay,&Discard,&BirthYear);

  /* 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);
  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.