Department of Computer Science
University of Minnesota, Duluth
(Last Revised 1/7/00)
Introduction
The software used in your CS-I and CS-II classes to create C and/or C++ programs is called CBuilder. This tutorial takes you through the process of creating a simple C program using CBuilder. Once you have finished, you will be ready to move on to more complicated ones. CBuilder is available on the UMD lab server and can be accessed from every Windows PC lab on campus.
Where to start
During your first lab, the teaching assistant will show you how to find CBuilder on the lab server. CBuilder runs programs from the directory C:\Myfiles. Your TA will show you how to save your work to this directory. When you have finished your work in the lab, don't forget to copy your program from C:\Myfiles to a floppy disk in the A: drive so you can take it with you. Programs left in C:\Myfiles will be removed daily so if you forget to make a copy to your floppy disk you may not be able to get it later.
If you questions regarding the operation of your lab computer please ask the teaching assistant or the lab consultants. Remember to save your work at regular intervals and to back up your disk so that you never lose your program by accident.
This tutorial assumes that you have found CBuilder and started it up. The initial screen looks like the one at the top of the next page. Do not be intimidated by all the menus, lists, icons and windows. CBuilder is a very powerful software package, but we will need to master only a small number of its features for our programming.
To create a new program, select NEW from the FILE menu.
Then select 'Console Wizard' from the dialog box and
click OK.
The Console Wizard will assist you in the creation of your programs. To do this, it must first know what type of program you are going to create. The next dialog box is where you give it this information. You should set up your program as a Windows Console Application that is executable (.EXE). It is important to leave all other choices blank.
When you have finished, the Console Application Wizard assigns your programming project a number and creates a window with a program already constructed for you. You may not understand much about the program it has created, but you will use it as the basis for constructing your own programs by adding and deleting lines as necessary.
In this example, add the line "printf("Hello world!"); to your program as shown below.
Now that you see how easy it is to edit your program, there are several changes you should make to it. When finished, your program should look like the one at the top of the next page.
The most important change you made was the line that says
"getchar();". This command waits for the user to hit a key on the
keyboard. After your program gets the character it will end and the
output window will disappear. If you do not put the "getchar();"
command in, your program will run but the output window will
flash into existence briefly and then disappear.
You will need to put this line into every program you write.
To further insure that the window will stay until the user sends
a signal for it to go away, many programmers insert the line:
fflush(stdin);
into their code before the "getchar();" line. You may or may not
find this necessary. It has been omitted from this example.
Now save your program to the C:\Myfiles directory. CBuilder is set up to use files from C:\Myfiles and no other location. This means that each time you begin programming you should copy your files to that subdirectory. When you are finished you should back them up on your floppy disk AND erase them from C:\Myfiles so that you are not giving your program away to someone else by accident. You will probably also want to rename the file (i.e. Lab1, Lab2, Lab3, etc.). The TA or lab consultant can help you with this if you are unfamiliar with saving and renameing files.
After you have saved it, it is time to run your program. To run your program, click on the green arrow. If everything is OK with your program the output window will come up. If there are errors, they will be listed below your program. You should fix them and then attempt to run the program again.
Your output is displayed in a separate MSDOS window (see below). This window will exist until you hit any key on your keyboard.
Printing your lab
For every programming assignment that you do you must turn in a copy of your program and of it's output. To print a copy of your program when it displays in the project window simply select PRINT from the FILE menu.
Printing the output from your program is a little more difficult. Since the output window is a program that is running, you cannot select PRINT until it is done. But, of course, when your program is done, the output window disappears. So, how do we get around this 'Catch 22'? To get around this problem, you will need to mark an area in the output window for copying, then copy it, terminate the program, open up Notepad and paste what you copied into it. From Notepad you can then print your pasted output. The process looks something like this:
First you mark the output. To do this, click on the Mark button.
When you have selected Mark, drag the cursor through the area you wish to copy.
With the area marked, you can now click on the copy button to save it to memory.
After ending the program (getting rid of the output window), you can open up Notepad and select PASTE from the EDIT menu. If you are unfamiliar with Notepad ask the TA or lab consultant to demonstrate. This pastes your output selection into a new Notepad document. To print it select PRINT from the FILE menu.