The necessary C and C++ library routines for this course are accessed by placing the following
#includeline near the top of a program file:
#include "CHeaders.h" // access to standard librariesSimilarly, to access our software tools for windows, graphics, and robust input and output, the following three lines must be placed near the top of the file as well:
#include "SWindows.h" // set up for windows #include "Graphics.h" // graphics functions #include "IoTools.h" // tools for safe I/O
void main() { }The pair of parentheses following the name main says that main is a C++ function. The keyword void says the function main is not expected to return a value.
Most of the work done in main is accomplished by calling other functions. The first call is
BigSquarePair();This executes a function in SWindows that opens a 400x400 square Drawing window for graphics on the left and a 240x400 Text window for user interaction on the right.
The next three calls transfer control to the three tasks that are at the heart of the lab:
Credits(); // identify yourself as the programmer NumbersTask(); //reading, writing, and arithmetic SimpleGraphicsTask(); //simple graphicsThe work of the program is divided into separate functions for each task to avoid excessive complication in the main function. The code written for each task can then be written without reference to or interference from the code in the other tasks.
The main function ends with a call to
PressReturnto cause the program to pause until the user presses the return key. This is necessary to hold the Drawing window open until the program terminates.
The Tasks of this Laboratory
To help you to find the places in the code where you will have to add or modify code,
we have placed special comments which start with:
Credits - Output of String Constants
Before we discuss Credits, notice that the very last line of main is the statement:
cout << "Select Quit from the File Menu" << endl;In Credits, use the same technique to create three lines of output with your name, your student ID number, and a blank line for extra space. The output should look like:
Written by Bill Gates ID: 123-45-6789Of course, use your own name and student ID number not those of Bill Gates. In the function Credits, you will see the following comments to remind you of the work to be done at that stage:
// ? ? (1) add a message with your own name // ? ? (2) add a message with your student-id number // ? ? (3) add an extra blank lineYou should save your file and run the program as soon as you have finished the Credits function. It this way, you can check for and correct errors before moving onto the next function. Do the same with the other tasks as well.
Henceforth, in all laboratory assignments, you should create a Credits function that is called at the beginning of main to identify you as the programmer.
NumbersTask - Integer Input and Arithmetic Output
In NumbersTask, you will read the values of two integer (
int) variables x and y typed in by the user and you will then compute and display the five arithmetic operations:
x+y x-y x*y x/y x%y.A typical user session should look like:
Test arithmetic operators Enter x: 31 Enter y: 5 x = 31 y = 5 x + y = 36 x - y = 26 x * y = 155 x / y = 6 x % y = 1 Press Return To ContinueThe user input is shown in italics. You enter values of x and y and then the program computes and displays the five possible arithmetic operations involving x and y.
The first steps in NumbersTask are the variable definitions that request storage for two int variables:
int x; // used for arithmetic int y; // used for arithmeticThe value of x is requested in a prompt-response interaction. Your first task is to request the value of y in the same manner. The program then echoes the x and y values to the user.
You must next do the arithmetic computations. A sample is given in the file as a comment:
// cout << "x + y = " << x + y << endl;By removing the comment slashes, you have the first line of the test code. You must make four similar lines of the code for the operators - * / %.
By the way, the reason why we need to comment the line with the operation x + y is that without the code to read the value of y (that you will supply) the compiler detects the fact that y is an uninitialized variable and issues a warning. If you want to see this, remove the comments before you enter the code to read y.
Run your program several times with different values for x and y and look at the results.
SimpleGraphicsTask
In SimpleGraphicsTask, a red rectangle, blue oval, and white line are already drawn. Your task is to read the code and then write the code to draw the shapes requested: another white line, a green rectangle, and a magenta oval.
Turn in: A diskette with your project (warmup.mu) and your source code (warmup.cp),
and a printout of your source code.