CSCI 201 Lab 6 -- Loops and Files

In this lab you'll modify a small C++ programs. The program uses a while loop to read and then draw "shapes" from a file.

Cleaning up

If there is any thing in C:\FILES delete it.

The Task

First, create a project of type Win32 Application in C:/FILES called FileRead. Add the EzWindows library to the project and, if necessary, modify the compiler options to use the EzWindows include directory.

Add a new C++ Source called FileRead.cpp to your project. Initialize it with a program outline written by the CSCI 107 instructors.

In this lab you will write a program that reads a description of a drawing from a file and displays it in a window. Each line of the file will describe a shape using six components:

  1. X co-ordinate of the center point
  2. Y co-ordinate of the center point
  3. Width
  4. Height
  5. Color
  6. Type -- rectangle, ellipse, triangle
After these six items an optional "comment" may appear on the line. Here are a few lines that describe a couple of red rectangles and a blue ellipse
 5.0  6.0 3.0 2.0 r r
 7.8  3.0 4.0 3.0 r r please ignore the comment
10.0 10.0 2.0 2.0 b e

Colors and shapes are described by one letter. The encoding for colors is given in the comment to the C++ function letter2color. The three shapes are encoded by the letters r, e, and t.

There are three places where you need to modify your program. First, insert your name into the comment on the first line.

Second, look for the comment line that read

// Fill in here to read the line
This is where you add four more ">> variable" into the while loop condition to read in the other items of the line.

Third, do the hard part. Look for the line

// Fill in here to draw the shape
This is where you add the code that creates an appropriate shape and draws it to the screen. The minimal requirement for this part of the assignment is that you draw a rectangle of the appropriate dimensions and color. If you are unsure about how to use the function letter2color to transform the input letter into an EzWindows color, you should ask your lab instructor for help.

If you are able to complete the minimal requirement with little trouble, modify your program to vary the types of shapes that it draws.

Sample input files

Your program contains the line
ifstream fin("C:\\Files\\Lab06.txt") ;
Consequently, you will need to have a drawing description stored in C:\Files\Lab06.txt on your computer.

We have provided a sample file for cut-and-paste to help you out here. This file is a "translation" of the C++ face program written by Michael Maher.

Return to CSCI 201 page
CSCI logo Return to the UNCA Computer Science home page