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.

Slides:



Advertisements
Similar presentations
Introduction to C++ September 12, Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules.
Advertisements

File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files * A computer file n is stored on a secondary storage device (e.g., disk) n is permanent.
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files (Review) * A computer file n is stored secondary storage devices (hard drive, CD) n can.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
File I/O. COMP104 File I/O / Slide 2 Using Input/Output Files * A computer file n is stored on a secondary storage device (e.g., disk) n is permanent.
CS150 Introduction to Computer Science 1
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 CS 101 Lecture 4 Repetition. 2 pre and post increment and decrement If i=i+1; we may write i++ to mean the same thing, and is called the postincrement.
 Wednesday, 10/16/02, Slide #1 CS106 Introduction to CS1 Wednesday, 10/16/02  QUESTIONS??  Today:  Return and discuss Test #1  Input from and output.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Program Input and the Software Design Process ROBERT REAVES.
Basic Elements of C++ Chapter 2.
Arithmetic Operators Operation Operator Example Addition = 9 Subtraction = 1 and = -1 Multiplication * 5 * 4 = 9 Division (integer)
Introduction to C++ Programming
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CSC 107 – Programming For Science. Today’s Goal ALL  Understand why ALL I/O is file I/O  Common bugs to avoid when coding with files in C++  Get a.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Unit 3, Lesson 3 Math Operations, Assignment Operators, and Arithmetic Operators Mr. Dave Clausen La Cañada High School
File I/O ifstreams and ofstreams Sections 11.1 &
1 CS161 Introduction to Computer Science Topic #13.
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
First steps Jordi Cortadella Department of Computer Science.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT11: File I/O CS2311 Computer Programming.
Overview Go over parts of quiz? Another iteration structure for loop.
Loops and Files. 5.1 The Increment and Decrement Operators.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
1 CS 101 Fall 2001 Lecture 3. 2 Boolean expressions The expressions that are allowed to be in the parentheses following an if can be of the form x>y,
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
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.
Arithmetic, Functions and Input 9/16/13. Arithmetic Operators C++ has the same arithmetic operators as a calculator: * for multiplication: a * b – Not.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 CS101 Fall 2001 Lecture 1 In order to write a program, you must first telnet to your pegasus account and login either from a Rutgers computer in a lab,
 Data Streams  Numeric Output  Numeric Input  Multiple Numeric Output  Multiple Numeric Input  Character Output  Character Input  String Output.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
ifstreams and ofstreams
Chapter Topics The Basics of a C++ Program Data Types
What Actions Do We Have Part 1
Basic Elements of C++.
Variables, Expressions, and IO
Data Streams.
Basic Elements of C++ Chapter 2.
File I/O with Records Lesson xx
Introduction to C++ Programming
Today in CS161 Week #3 Learn about… Writing our First Program
Alternate Version of STARTING OUT WITH C++ 4th Edition
Today’s Lecture I/O Streams Tools for File I/O
CS150 Introduction to Computer Science 1
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Lecture 2 Fall 2011 September 13-15, 2011 Ghufran Ahmed
Arithmetic Operations
What Actions Do We Have Part 1
Reading Data From and Writing Data To Text Files
CS 101 First Exam Review.
ifstreams and ofstreams
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

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 > x >> y ; cout << “The sum of “ << x << “and “ << y << “is “ << x + y << “.\n”; }

3 When the person who is running the program is prompted to enter two integers, the two integers must be separated by one or more spaces and/or carriage returns. Before the person enters the first integer, the screen blinks with a prompt, and after the first integer is entered, but before the second integer is entered, the screen is still blinking with a prompt to enter another integer. Hitting a space or return does not stop the blinking, only typing an integer and enter will stop it. >> is called the extraction operator. It extracts from the keyboard stream

4 Writing Output to a File In order to write output to a file, we must give the preprocessor directive #include Inside the program, we give the one-line command ofstream fout(“output.txt”); This connects fout with the file named output.txt If this file doesn’t exist, it is created, and whatever is written to it is saved. It it already exists, whatever is in it is completely erased and new characters are written in it. Here is a program using file output.

5 #include main(){ cout > x >> y ; ofstream fout(“output.txt”); fout << “The sum of “ << x << “and “ << y << “is “ << x + y << “.\n”; fout.close( ) ; }

6 If you run this program and enter 5 6 you can the type emacs output.txt and you will see that The sum of 5 and 6 is 11. Has been written to the file output.txt as expected. Instead of the one-line command ofstream fout(“output.txt”); you can give the equivalent two lines of commands ofstream fout; fout.open(“output.txt”); Although not absolutely necessary, it is safe to type fout.close( ); It was not necessary to type #include because #include allows us to type cin or cout.

7 Input from a File In order to read input from a file, we must give the same preprocessor directive as file output #include Inside the program, we give the one-line command ifstream fin(“input.txt”); This connects fin with the file named input.txt This file must already exist. If it doesn’t, a program using the line above won’t compile Here is a program using file input.

8 #include main(){ int x,y; ifstream fin(“input.txt”); fin >> x >> y ; cout << “The sum of “ << x << “and “ << y << “is “ << x + y << “.\n”; fout.close(); }

9 There must already exist a file named input.txt and it must have 2 integers written it, separated by one or more spaces and/or returns. Instead of the one- line declaration ifstream fin(“input.txt”); we could have written the two lines of commands ifstream fin; fin.open(“input.txt”); Of course the names fin and fout could have been fileinput or fileoutput as well.

10 Arithmetic Operators and Expressions If x,y,z are 3,5,2 respectively, then evaluation of the math formula x + yz yields 13. This is because in the absence of any parentheses, multiplication is stronger than addition, i.e., y is multiplied by z before addition of x. In C++, if we write x + y*z, then multiplication is stronger than addition. If we want addition to be done before multiplication, use parentheses as follows: (x + y) * z. Quiz 1 dealt with this.

11 Quiz 1 Suppose that there is a file called input.txt in the same directory as a C++ program you are writing. Suppose also that this file has three integers in it, separated by spaces. Write a program which will read these three integers from this file, multiply the first two together, add this product to the third integer and write this resulting sum to a third file called output.txt

12 Quiz 1 Solution #include //necessary for fin and fout main(){ //begin the program int x,y,z; //declare three int variables ifstream fin(“input.txt”); //connect fin to the file fin>>x>>y>>z; //read 3 integers into x,y,z fin.close(); //close the file ofstream fout(“output.txt”); //connect fout to the file fout<<x * y + z; // write xy+z to the file fout.close(); //close the file } //end the program

13 Assignment Statements A statement of the form x = 7; is called an assignment statement. Whatever value the variable x had before the assignment statement, it has the value 7 after the assignment statement is executed. If the variable x has the value 5 and the variable y has the value 13, then the statement x = y; is also called an assignment statement. After this statement, the variable x has the value 13. The variable y also still has the value 13.

14 if Statements If we have a statement of the form if ( x = the value of y, then the statement x = x + 1; is not executed, but the statement y = y + 1; is executed.

15 if else Statements If we have a statement of the form if ( x = the value of y, then the statement x = x + 1; is not executed, but the statement y = y + 1; is executed and then the statement x = y; is executed.

16 if and if else Program #include // necessary for cin and cout main(){ // begin program int x = 3, y = 8; /* declare the int variables x,y and initialize them to 3 and 8, respectively.*/ if ( x y ) cout y is false so x is not printed else cout y is false so y is printed cout << endl; // in either case print a newline } // end program