Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions, Projects, and C++ I/O Streams Dr. Nancy Warter-Perez June 4, 2003.

Similar presentations


Presentation on theme: "Functions, Projects, and C++ I/O Streams Dr. Nancy Warter-Perez June 4, 2003."— Presentation transcript:

1 Functions, Projects, and C++ I/O Streams Dr. Nancy Warter-Perez June 4, 2003

2 6/4/03Functions, Projects, and I/O Streams2 Functions func_name (arg1_typ arg1_name, …, argN_typ argN_name) { function body } Func_name – name of the function main – all programs must start with this function Return_type – type of value returned by function Arguments call-by-value – arguments are inputs to function that can’t be modified by function Function prototype (used in header files [*.h]) func_name (arg1_typ, …, argN_typ); Library functions – commonly used functions iostream.h, string.h, stdlib.h, math.h (to name a few)

3 6/4/03Functions, Projects, and I/O Streams3 Projects Separate Compilation Compile Link Executable *.exe Library functions *.a … File1.cpp FileN.cpp Object files *.obj

4 6/4/03Functions, Projects, and I/O Streams4 Steps to Creating a Visual C++ Project (Step 1) To create a project Under File, select New Under Projects Select Win32 Console Application Assign a Project name and Location for your project Select Create new workspace When prompted for type of console application, select empty project Click Finish Click OK

5 6/4/03Functions, Projects, and I/O Streams5 Steps to Creating a Visual C++ Project (Step 2) To add a C or C++ source file to the project Under File, select New Under Files Select C++ Source File Select Add to project (Project will be set to the current project) Assign a File name (use the default location determined by the project) Click OK (the source file will be displayed in the editor window)

6 6/4/03Functions, Projects, and I/O Streams6 Steps to Creating a Visual C++ Project (Step 3) Enter your program in the editor Notice that the editor has a color coding Comments Key words Everything else Also notice that it automatically indents Don’t override!! If doesn’t indent to proper location – indicates bug

7 6/4/03Functions, Projects, and I/O Streams7 Steps to Creating a Visual C++ Project (Step 4) To build your program Under Build Select Build project_name.exe In case of compile time errors or warnings, they will be listed in the bottom window (scroll up) Double click on error or warning to find in program After fixing error (bug), rebuild following same steps

8 6/4/03Functions, Projects, and I/O Streams8 Steps to Creating a Visual C++ Project (Step 5) To execute your program First, create any necessary input files Under File, select New Under Files, select Text File Assign File name and Location (default ok) It is OK to add to project (default) Click OK To run your program (can click ‘!’ icon, or) Under Build, select Execute project_name.exe

9 6/4/03Functions, Projects, and I/O Streams9 C++ I/O streams - input Standard I/O input stream: cin Ex: int x; char c1, c2, c3; cin >> x >> c1 >> c2 >> c3; If the following input is typed: 23 a b c Then, x = 23, c1 = 'a', c2 = 'b', c3 = 'c' (will ignore white spaces)

10 6/4/03Functions, Projects, and I/O Streams10 C++ I/O streams - output Standard I/O output stream: cout Ex: int x = 454; char c1 = 'L'; cout << "Bioinformatics:\tCHEM " << x << c1 << endl; The following output is displayed: Bioinformatics: CHEM 454L

11 6/4/03Functions, Projects, and I/O Streams11 I/O Streams Usage Must include iostream header file #include There are ways to format the output to specify parameters such as the width of a field, the precision, and the output data type

12 6/4/03Functions, Projects, and I/O Streams12 File I/O – Fstream (1) Preprocessor directive #include Need a file object to keep track of file access information fstream fin, fout; // can use any variable names Need to open a file fin.open("in_file.dat", ios::in); read-only fout.open("out_file.dat", ios::out); write-only

13 6/4/03Functions, Projects, and I/O Streams13 File I/O – Fstream (2) To read from a file: int x; fin >> x; //will read an integer value from in_file.dat into x To write to a file: fout << x; To close a file: fin.close(); fout.close(); To detect the end of a file: fin >> x; while (!fin.eof()) // eof will be set when end-of-file reached fin >> x;


Download ppt "Functions, Projects, and C++ I/O Streams Dr. Nancy Warter-Perez June 4, 2003."

Similar presentations


Ads by Google