Warmup Lab: Learn To Run a Simple Program

 

Overview

In this lab you will learn to write and edit C++ source code, compile, and run a simple program, starting from an existing "shell" project.

You will write a simple program that will read input, process the data, and write output.

Objectives

Program Management Skills

Programming Statements and Directives

Design and Programming Patterns

 Project Description

Project Goals

You goal is to write a program that performs the following tasks:

  1. Print the names of both lab partners.
  2. Ask the user how is she feeling today and print a message indicating that the computer also feels that way.
  3. Ask the user the year when he was born and print a message indicating user's age.
  4. Declare four variables for the rectangle coordinates (top left corner and the bottom right corner) and ask the user to enter coordinates.
  5. Paint a red rectangle.
  6. Invert oval within the rectangle.
  7. Paint a green circle of radius 10 in the middle of the rectangle.

Project Environment

The C++ code that you will write will be only the middle part of a larger program. We call this larger program a shell - it performs a number of tasks such as opening windows for the graphics and text output, letting the compiler know what toolkits and libraries to use, and has a few statements at the end that make the computer wait for user input before closing the output windows. For each project you will work on, we prepare the Shell.cpp file that will contain a skeleton of the program you will need to complete.

For a compiler to work properly, a number of different files must be placed in correct folders and with correct names. It is a tedious task to create a project with all its components. In the industrial environment programmers often build a skeleton project and use it as a starting point for all programs related to the same product. We, too, follow this practice. So, for every lab and recitation there will be a Shell folder with the appropriate Shell.cpp C++ source code file and a Shell.dsw project workspace file. When you double-click on the Shell.dsw icon, the Visual C++ environment opens up and you are ready to work on your program.

 

The Mechanics of Editing, Compiling, and Running a Program

The following steps will guide you through learning how to open a project, edit a program, rebuild a project and run the finished program.

  1. Start by running the solution to the program by double-clicking on the Warmup.exe icon. Observe how the program behaves. Your program should be very similar.
  2. Double-click on the Shell.dsw icon. You will see several windows in the workspace. On the left is the list of all the files that are part of your project - you do not need to worry about them for now.
  3. The largest window shows you the Shell.cpp file. The compiler colors different parts of the code in blue keywords), green (comments), and black (the rest of the code). You can edit this file just as you can edit any other file in a word processor. (Of course, you do not worry about fonts.) Comments indicate where your part of the code should be placed.
  4. Once you typed in your code, you need to do three things:

·        Save the Shell.cpp file (use Save in the File menu)

·        Select the Rebuild All command from the Build menu ... and if there are no errors,

·        Select Execute Shell.exe command from the Build menu. (You can also press Control/F5 keys.)

·        If you had errors in your program (either when you compiled it using the Rebuild All command or when you ran the program using the Execute Shell.exe command), you need to think about the problem, fix it, and try again.

Lab Guide

In order to write this program, start with thinking with a piece of paper and your class notes, recitation notes, and all other resources available. Sketch out your solution to each of the seven tasks. Once you think you know how your program should look, start adding the parts to the Shell.cpp. Add code for one task, save the file, and test your work before you proceed to the next task. If you do not know how to continue, ask the instructor, ask your friends (if they know the answer), ask other people in the lab. Most of the problems at the beginning require very little to fix for an experienced programmer, but may look very confusing to you. The sooner you ask, the sooner you can proceed with your work.

  1. Print the names of both lab partners. This is a very simple task.
  2. Ask the user how is she feeling today and print a message indicating that the computer also feels that way. Observe the solution to see how you should react to the user's input. This should require one declaration, one input and one output statement.
  3. Ask the user the year when he was born and print a message indicating the user's age. Think about what kind of identifiers you need to record someone's year of birth and someone's age. Write down how do you compute someone's age if you know the year of birth and translate it into a C++ statement or a C++ expression. The rest is easy.
  4. Declare four variables for the rectangle coordinates (top left corner and the bottom right corner) and ask the user to enter coordinates. All coordinates should be of the type int or short. The range should be from 0 to 300, but we will not worry about any errors. If the input does not make sense, the picture may not fit within the graphics window.
  5. Paint a red rectangle. First you have to set the foreground color, then paint.
  6. Invert an oval within the rectangle. No comments here.
  7. Paint a green circle of radius 10 in the middle of the rectangle. Here you need to compute the coordinates of the center of the circle. This is the hardest task of the whole lab (still a simple thing to do). Again you need to set the foreground color and then paint.

As you are working on the different tasks, make sure your program is readable. Use comments, tell the reader which task is being performed, use extra blank lines to separate different kinds of actions, use identifiers that are meaningful. Part of your grade will depend on the organization and readability of your program. You may even finish up at home, adding comments and polishing the final layout of the source code.

Completing the Lab Project

Once your program is correct, you need to do the following: