This document is part of the HTML publication "An Introduction to the Imperative Part of C++"

The original version was produced by Rob Miller at Imperial College London, September 1996.

Version 1.1 (modified by David Clark at Imperial College London, September 1997)

Version 1.2 (modified by Bob White at Imperial College London, September 1998)

Version 1.3, 1.4, 2.0, ..., 2.20 (modified by William Knottenbelt at Imperial College London, September 1999-September 2021)

# Introduction to C++ Programming: Exercise Sheet 1

These exercises assume some familiarity with PCs running a UNIX-based operating system (e.g. Linux, MacOS, etc.).

## Question 1

Using Appendix A.1 to help you, create a directory (folder) called "AgeCalculator". Inside this directory, create a program file called "AgeCalculator.cpp", type in Program 1.5.1 in the notes, save the file, compile it, and run it. Compare your screen output with the example output in the lecture notes. Briefly experiment in improving and changing the output format.

## Question 2

Alter Program 1.5.1 so that if "another_age" works out to be more than 150, the screen output is:

Sorry, but you'll probably be dead by [year]!

Test the program with various different inputs from the keyboard.

## Question 3

(More difficult.) Alter your program from question 2 so that it deals with months as well as years, and produces output such as the following:

```	Enter the current year then press RETURN.

1996

Enter the current month (a number from 1 to 12).

10

Enter your current age in years.

36

Enter the month in which you were born (a number from 1 to 12).

5

Enter the year for which you wish to know your age.

2001

Enter the month in this year.

6

Your age in 6/2001: 41 years and 1 month.```

The program should cope with singulars and plurals properly in the output, e.g. "1 month" but "2 months".

Hints: you will have to introduce extra variables in the variable declaration, and may find the following arithmetical operations useful:

 Symbol + - * / % Operation Addition Subtraction Multiplication Division Modulus Example 3 + 5 43 - 25 4 * 7 9 / 2 20 % 6 Value 8 18 28 4 2

(Notice that when the division sign "/" is used with two integers, it returns an integer.) You may also want to use the following comparison operators:

 Symbol < <= > >= == != Meaning less than less than or equal to greater than greater than or equal to equal to not equal to Example 3 < 5 43 <= 25 4 > 7 9 >= 2 20 == 6 20 != 6 Value TRUE FALSE FALSE TRUE FALSE TRUE