Sunteți pe pagina 1din 10

CS 220 Tutorial USING DEV-C++ To Create, Compile, & Execute C++ Programs by Dr.

William Muellner Introduction


The Dev-C++ 4.0 software development 'package' provides a 'built-in' editor you can use to write a C++ source program and then save a copy of the program in a file on disk. Following this, you can use other components of Dev-C++ software to compile, link, and execute this program. The Dev-C++ editor is similar to a word processor and allows you to type statements that are temporarily stored in a 'work space buffer' in the computer. It you're writing a C++ program, these would be the C++ statements that make up your source code. After entering your code into the work space buffer, you normally would next save the content of the buffer in an ASCII text file on disk (this is called the source file or source code file). For a C++ program, the name of the source-code file always has a .cpp extension. For example, you might save the C++ statements you've typed in a disk file titled myprog1.cpp. In Dev-C++, you must supply a name for the source file and the editor will then automatically append the .cpp extension for you. Once you have saved your C++ source 'code' in a source file, you next compile this code using the Dev-C++ compiler. The compiler checks for syntax errors in your source code and will indicate the lines containing these if any are found. If no syntax errors are found, the compiler (which is actually a compiler/linker) has the linker check the output of the compiler for linker errors. If any of these are found, appropriate messages are supplied to you. If no linker errors are found, the linker then generates a machine code executable file (that has a .exe extension). For example, if you had named your source file myprog1.cpp, after compiling and linking the executable file myprog1.exe is created on disk. You have the option of executing the 'machine code program' in this file either from Dev-C++, or, you can exit Dev-C++ and execute the program from Windows or from MS-DOS. The application you'll generate is actually an MS-DOS application, and can thus be run in a 'DOS window' using the Windows operating system or by invoking MS-DOS and running the program under this operating system. Starting Up Dev-C++ Dev-C++ is started from Windows. Assuming you've powered up your computer and are now in Windows, you can start Dev-C++ by clicking on the Dev-C++ icon (if one is present). If no icon is showing, start Dev-C++ by doing the following: click Start click Programs and find the Dev-C++ entry on the pop-up menu that appears click Dev-C++ In the Dev-C++ menu that appears, click Dev-C++

Creating A C++ Source File Once Dev-C++ is started, you should see the Dev-C++ 4 'main window' (note the title bar with Dev-C++ at the top of this window). Below the title bar is a set of menu selections. Here, we want to create a new source file. 1. To do this, click File in the pop-up menu that appears, click New Source file you should now see an 'editor' window open having the title Untitled and containing the code:

#include <iostream.h> #include <stdlib.h> int main() { system("PAUSE"); return 0; }

If you use the more recent Bloodshed you are not going to be given the template code above. You will have to enter it yourself as important few lines in order to compile. Use instead of #include <iostream.h> the line #include <iostream>. This will work also for the less recent Dev-C++4 This is what you need to have:

#include <iostream> #include <stdlib.h> int main() { system("PAUSE"); return 0; }

Previous version of Dev-C++ supplies this 'template' code automatically to you when you create a new source file. You can alter or erase this code if you choose, or simply add your own code to it. The purpose of each template statement is indicated below: #include <iostream> => Includes an I/O header file that allows input and output statements like cout and cin to be used in your program. #include <stdlib.h> => Includes the 'standard library' header file that allows functions like system( ) to be used in your program int main( ) => When your C++ application is executed, the computer begins execution at the main function. At a minimum, every C++ program has a main function. { and } => The opening and closing braces of your main function. The statements to be executed in the program are listed between these braces. When the closing brace is reached during program execution, control of the computer returns to the operating system. system("PAUSE"); => This statement makes a 'function call' to the 'standard library' function system. The code for this function is specified in the header file stdlib.h. This file and iostream already exist on disk and the code taken from these is combined with your code by the actions performed by the Dev-C++ compiler/linker . The system( ) function, when 'called' with argument "PAUSE", causes the computer to pause and wait for the user to press a key on the keyboard before continuing. NOTE: All statements that are found within the braces of function main must be terminated with a semicolon ( ; ), the C++ delimiter. return 0; => A function in C++ can return a value and this is indicated by listing the 'type' of the value to be returned before the function's name. By writing int main( ), we indicate the function returns an integer value. The statement return 0; carries out this 'return' activity by returning the value zero. For many of the programming assignments you'll be working on, you can use the template code supplied for you by Dev-C++. In most cases, you'll write your program by simply adding statements BEFORE the call to the system("PAUSE"); statement. By doing this, your

statements will be executed and then the computer will pause so you can examine the output and/or effects of your statements on the screen. 2. Position the Dev-C++ editor-window cursor (a vertical bar) on the line immediately preceding the statement system("PAUSE"); and then type the statements: cout << "Hello there."; cout << "How are you?"; 3. Save your program by doing the following: click File. You should see the Save File window appear. The Save in box indicates the directory in which the file will be saved. You can enter another directory name into this box if you want to save the file in a directory other than the 'default' one currently shown. type the name myprog1 in the File name text box. This will be the name of your source file. NOTE: You might have typed myprog1.cpp, but you can let the editor add the .cpp extension automatically to your source file name. Click the Save pushbutton to save the contents of the work-space buffer (i.e. your source program) in the file. Exiting Dev-C++ and Opening an Existing Source File You'll now exit Dev-C++, then restart Dev-C++ and open an existing source file (the file you just created). 1. To exit Dev-C++: Click File Click Exit Dev-C++ 2. Restart Dev-C++ using steps in the Starting Up Dev-C++ section. 3. Now open an existing file (i.e. myprog1.cpp) using the following steps: Click File Click Open project or file. The Open file window appears. If the directory containing the

file to be opened is not the one listed in the Look in: text box, change the contents of the box to the directory with the file. Type the name of the file to be opened (i.e. type myprog1.cpp) in the File name: text box. Or Simply double click on the file name in the Open file window. You should notice that an 'editor window' opens and that the title bar of this window contains Dev-C++ 4 - followed by the path name of the opened file. Linking and Compiling the Source Program
1. Compiling and Linking your source code:

Click Execute Click Compile After clicking Compile, the window Please wait while compiling appears. After the compiling process completes, this window disappears and the Compilation completed window is displayed on the screen. This window indicates how many, if any, compilation error occurred, and the number of bytes in the 'output' file that is created by the compilation. If any errors did occur, the locations (line numbers) of these are listed in a table near the bottom of the Dev-C++ 4 window. ASSUMING no compilation errors occurred (if any did, fix these), click the Continue button in the Compilation completed window. NOTE: Clicking Compile also causes the output of the compiler to be linked with other files so that an executable file is produced (this file will have the name myprog1.exe). However, if a linker error occurs during linking the .exe file is not produced and appropriate "linker error" messages are displayed. These will then have to be corrected before the linking process can produce the .exe file.
Executing the Program from Dev-C++ 4

The executable version of your program (stored in myprog1.exe) can be executed 'from within' Dev-C++ or by exiting Dev-C++ and executing it 'from the operating system' (that is, either from Windows or from MS-DOS).

1. To execute the program from within Dev-C++, click Execute click Run NOTE: As the program executes, a "DOS window" opens and any output produced by the program is displayed in this window. The presence of the system("PAUSE"); statement causes the program to 'pause'; if this statement weren't in the program, the DOS window would appear for a short period of time, show the output, and then disappear when the program terminated. 2. The DOS window should display the text Hello therehow are you? Press any key to continue The cout statements are responsible for displaying the first part of the output; the system("PAUSE") statement displays the 'Press any key to continue' text. 3. Press any key. Note what happens. 4. 'Comment out' the system("PAUSE"); statement in your source program. To do this, place two forward slashes in front of this statement. That is, have the statement read: // system("PAUSE"); This is now a 'comment' statement and not an executable one that calls a function. 5. "RUN" the program again. What happens now? What is the effect of having the system("PAUSE"); statement in your program??? Executing the Program from Windows 1. Exit Dev-C++ 4. 2. Assuming you saved your source file on your flash drive the compiler thus saved the executable version of the program in the same directory on that drive. Use Windows Explorer to examine the files on your drive. Is the program file myprog1.exe on your disk? You should notice that the program is listed by Explorer as a 'window' icon, indicating it is executable from Windows.

3. Close Windows Explorer and then: Click the Start pushbutton in the 'main' window. Click Run in the pop-up menu. If the Open text box doesn't contain the name of your executable file (myprog1.exe), type the name here (that is, type e:\myprog1.exe assuming the file is in the root directory on drive e) or use the Browse key to browse for this file. Once you have the file name in the text box, click OK to execute the program. Fixing Compiler Errors 1. Enter Dev-C++ 4 and open the file myprog1.cpp. Edit the main function so that it appears as follows:

using namespace std; int main() { int a 14 = a; cout << a system("PAUSE"); return 0; }
2. Compile the program by clicking Execute and then Compile. a. How many compiler errors occur? What are the line numbers of these? (Refer to the table at the bottom of the Dev-C++ window .) The editor will highlight a line containing an error if you double click on the error message in the table for that line. What error messages do you see? Write them down by hand.

b. The compiler is 'flagging' two errors (there are actually more but this is all it currently recognizes). To fix these, add semicolons after the statements on lines 7 and 9 so that code in main now reads:

using namespace std; int a; 14 = a; cout << a;

system("PAUSE"); return 0;
c. Compile the program again.. What happens now? d. Replace the line 14 = a; with the statement a = 14; , then compile the program again. What happens?

Fixing Linker Errors Linker errors occur when the Linker cannot find code that is to be linked with your code. For example, this might happen if you use an incorrect function name in your program and the linker cannot find the code for this function in your source file nor in any library or other 'external' file. 1. Create a source file with the following code (call this file myprog2.cpp):

#include <iostream> #include <stdlib.h> #include <math.h> float sqroot(float); using namespace std; int main() { float ans; ans = sqroot(25.0); cout << "the square root of 25 is: " << ans << endl; system("PAUSE"); return 0; }
This program attempts to use a math.h library file called sqroot( ) to find the square root of 25.0. Compile Program. What happens? You should note that there are no compiler syntax errors. However, since sqroot is not a legitimate math.h library function, a linker error does occur.

2. Replace the word sqroot in the statements float sqroot(float); and ans = sqroot with the word sqrt and compile the program again. What happens now?

3. Run the program and observe its output. Note the effect of using endl in the cout statement. Printing Source Code and Program Output In the assignments you'll be doing for this class, you will be required to submit printed copies of your source code programs and the text output of these programs. You can generate the printout of your source code by using a word processor such as Microsoft Word to open the file with this code and then simply do a 'print file' command. Before doing this, however, you can also copy the output that was produced by your program in the DOS Window on the screen into Word document. The following steps demonstrate how to do this: Start up Microsoft Word and then 'open' the source file myprog2.cpp. The code in this file should now appear within Word's 'editor' window. With Dev-C++ up and running, compile and run program myprog2.cpp . You should see the DOS window open and the output: the square root of 25 is 5 press any key to continue is displayed within this window. Move the mouse cursor to the 'dashed square' symbol on the menu bar at the top the DOS window and click. Next, move the cursor to the beginning of the text being displayed in the DOS window, hold down the left mouse button, and drag the cursor to the end of this text. Note that as you do this, the text is displayed in black on a white background. After all of the 'output text' is highlighted in this fashion, release the mouse button. Move the mouse cursor to a location that follows the copy of your source code being displayed within the Microsoft Word editor window and click the mouse. You should see the 'output text' that is being displayed in the DOS window now appear in the Word editor

window. You can now print the contents of the Word editor window to obtain your 'printout' of your source program and the output produced by this program.

S-ar putea să vă placă și