Pages

Sunday, 26 June 2016

Chapter 3 Drill - Principles & Practice Using C++

In this drill I am using an internet based IDE found here:

https://www.codechef.com/ide

So there may be some discrepancies from the book.

Drill 3.1

This drill is to write a program that produces a simple form letter based on user input. begin by typing the code from 3. prompting a user to enter his or her first name and writing "Hello, first_name" where first_name is the name entered by the user. then modify your code as follows: change the prompt to "Enter the name of the person you want to write to" and change the output to "Dear first_name,". Don't forget the comma.



In the book it says to use underscores to separate words however I started learning on a website that used capitals between words and I kind of prefer that method.

Drill 3.2

Add an introductory line or two, like "How are you? I am fine. I miss you." Be sure to indent the first line. Add a few more lines of your choosing - it's your letter.



*For those of you who are not as sophisticated as I am, those are the lyrics to the opening song from Spongebob Squarepants. 

Drill 3.3

Now prompt the user for the name of another friend, and store it in friend_name. Add a line to your letter : "Have you seen friend_name lately?".




Drill 3.4

Declare a char variable called friend_sex and initialize its value to 0. Prompt the user to enter an m if the friend is male and f is the friend is female. Assign the value entered to the variable friend_sex. Then use two if-statements to write the following:
If the friend is is male, write "If you see friend_name please as him to call me."
If the friend is female, write "If you see friend_name please ask her to call me."




Drill 3.5

Prompt the user to enter the age of the recipient and assign it to an int variable age. have your program write "I hear you just had a birthday and you are age years old!" If age is 0 or less or 110 or more call simple_error("you're kidding!") using simple_error() from std_lib_facilities.h



As you can see I didn't use the error function provided but that's because I'm currently writing this on a computer that has no IDE on it and I have no access to any of my files so I'm using a web based IDE from codechef. The simple if statement does the exact same job though.

Drill 3.6

Add this to your letter:
If your friend is under 12, write "Next year you will be age+1"
If your friend is 17, write "Next year you will be able to vote."
If your friend is over 70, write " I hope you are enjoying retirement."

Check your program to make sure it responds appropriately to each kind of value.



Drill 3.7

Add "yours sincerely," followed by two blank lines for a signature, followed by two blank lines for a signature, followed by your name.





This drill was pretty straight forward, covering everything taught in chapter 3. When I started this blog I was actually up to chapter 4 and originally thought I was doing well with the drills if they were all going to be this easy then I got to the chapter 4 drill and my head almost exploded. 

The only thing that initially confused me was all the newlines needed to keep everything readable but the more you write the more that \n becomes second nature.






No comments:

Post a Comment