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

Slides:



Advertisements
Similar presentations
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Advertisements

Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
CS-212 C++ I/O Dick Steflik. C++ I/O Modeled after UNIX’s concept of a “stream” –conceptionally a stream is a continuous flow of characters/bytes from.
Simplest program and Data Output Sen Zhang. The simplest program looks like a single person company! void main() { // this starts a comment line // here.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Using Visual C++ and Pelles C
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
Program Input and the Software Design Process ROBERT REAVES.
KEAN UNIVERSITY Visual C++ Dr. K. Shahrabi. Developer studio Is a self-contain environment for creating, compiling, linking and testing windows program.
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - HelloWorld Application: Introduction to.
A1 Visual C++.NET Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Quick Introduction The following.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Creating your first C++ program
1 CS 101 Lecture 2. 2 Input from the Keyboard Here is a program to accept input from the keyboard and put into into two variables. #include main(){ cout.
A Review of C++ Dr. Nancy Warter-Perez June 16, 2003.
Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)
File I/O ifstreams and ofstreams Sections 11.1 &
Computer Programming I Hour 2 - Writing Your First C Program.
Data Structures and Debugging Dr. Nancy Warter-Perez June 18, 2003.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Using Microsoft Visual Studio C++ Express 2005 Name: Dr Ju Wang Ashwin Belle Course Resource:
Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
C++ FILE I/O.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT11: File I/O CS2311 Computer Programming.
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
Chapter 2 part #1 C++ Program Structure
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
Vectors One-Dimensional Containers. Problem A file contains a sequence of names and scores: Ann92 Bob84 Chris89... Using OCD, design and implement a program.
Chapter -7 Basic function of Input/output system basics and file processing Stream classes : I/O Streams. A stream is a source or destination for collection.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
1 CS 192 Lecture 4 Winter 2003 December 8-9, 2003 Dr. Shafay Shamail.
Lecture 3: Getting Started & Input / Output (I/O)
Chapter 1.2 Introduction to C++ Programming
ifstreams and ofstreams
Chapter 1.2 Introduction to C++ Programming
Topic Pre-processor cout To output a message.
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Chapter 1.2 Introduction to C++ Programming
CS-103 COMPUTER PROGRAMMING
Beginning C++ Programming
Computer Science 210 Computer Organization
Lab 1 Introduction to C++.
Standard Input/Output Streams
Standard Input/Output Streams
File I/O with Records Lesson xx
Today’s Lecture I/O Streams Tools for File I/O
when need to keep permanently
الوحدة الرابعة البرمجة وصياغة حل المسائل البرمجة وأهميتها أهداف الدرس الأول مفهوم البرمجة. الفرق بين المبرمج ومستخدم البرنامج. الحاجة إلى البرامج.
Lab 1 Introduction to C++.
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Chapter 3 Input output.
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
Programs written in C and C++ can run on many different computers
Lecture 2 Fall 2011 September 13-15, 2011 Ghufran Ahmed
Fundamental Programming
Reading from and Writing to Files
Functions Imran Rashid CTO at ManiWeber Technologies.
ifstreams and ofstreams
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Reading from and Writing to Files
Presentation transcript:

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

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)

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

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

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/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

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

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

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)

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

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

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

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;