1 cs 101 lecture 2. 2 input from the keyboard here is a program to accept input from the keyboard...

16
1 CS 101 Lecture 2

Upload: marilynn-wilcox

Post on 13-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

1

CS 101 Lecture 2

Page 2: 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

2

Input from the Keyboard• Here is a program to accept input from the

keyboard and put into into two variables.#include<iostream.h>main(){cout << “Please enter two integers.\n”;int x,y;cin >> x >> y ;cout << “The sum of “ << x << “and “ << y << “is “ << x + y << “.\n”;}

Page 3: 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

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

Page 4: 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

4

Writing Output to a File• In order to write output to a file, we must give

the preprocessor directive #include<fstream.h>Inside the program, we give the one-line commandofstream 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.

Page 5: 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

5

• #include<fstream.h>main(){cout << “Please enter two integers.\n”;int x,y;cin >> x >> y ;ofstream fout(“output.txt”);fout << “The sum of “ << x << “and “ << y << “is “ << x + y << “.\n”;fout.close( ) ;}

Page 6: 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

6

• If you run this program and enter 5 6 you can the type emacs output.txt and you will see thatThe 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 commandsofstream fout;fout.open(“output.txt”);Although not absolutely necessary, it is safe to type fout.close( );It was not necessary to type #include<iostream.h> because #include<fstream.h> allows us to type cin or cout.

Page 7: 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

7

Input from a File

• In order to read input from a file, we must give the same preprocessor directive as file output #include<fstream.h>Inside the program, we give the one-line commandifstream 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.

Page 8: 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

8

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

Page 9: 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

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 commandsifstream fin;fin.open(“input.txt”);Of course the names fin and fout could have been fileinput or fileoutput as well.

Page 10: 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

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.

Page 11: 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

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

Page 12: 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

12

Quiz 1 Solution

• #include<fstream.h> //necessary for fin and foutmain(){ //begin the programint x,y,z; //declare three int variablesifstream fin(“input.txt”); //connect fin to the filefin>>x>>y>>z; //read 3 integers into x,y,zfin.close(); //close the fileofstream fout(“output.txt”); //connect fout to the filefout<<x * y + z; // write xy+z to the filefout.close(); //close the file} //end the program

Page 13: 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

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.

Page 14: 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

14

if Statements• If we have a statement of the form

if ( x < y ) then x = x + 1;y = y + 1;then if the value of the variable x is less than the value of the variable y, the statement x=x+1is executed and then the statement y = y + 1; is executed. On the other hand if the value of x is not less than the value of y , i.e., if the value of x is >= the value of y, then the statement x = x + 1; is not executed, but the statement y = y + 1; is executed.

Page 15: 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

15

if else Statements• If we have a statement of the form

if ( x < y ) then x = x + 1;else y = y + 1;x = y;then if the value of the variable x is less than the value of the variable y, the statement x=x+1is executed and then the statement x = y; is executed without executing x = x + 1; On the other hand if the value of x is not less than the value of y , i.e., if the value of x is >= 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.

Page 16: 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

16

if and if else Program

• #include<iostream.h> // necessary for cin and coutmain(){ // begin programint x = 3, y = 8; /* declare the int variables x,y and

initialize them to 3 and 8, respectively.*/if ( x < y ) x = x + 4; // x < y is true , so x is now 7y = y - 3; // y is now 5cout << x << ‘ ‘ << y << endl;//print x and y to the screenif ( x > y ) cout << x ; // x > y is false so x is not printedelse cout << y; // x > y is false so y is printed cout << endl; // in either case print a newline} // end program