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