1 / 16

CS 101 Lecture 2

CS 101 Lecture 2. Input from the Keyboard. Here is a program to accept input from the keyboard and put into into two variables. #include&lt;iostream.h&gt; main(){ cout &lt;&lt; “Please enter two integers.<br>”; int x,y; cin &gt;&gt; x &gt;&gt; y ; cout &lt;&lt; “The sum of “ &lt;&lt; x &lt;&lt; “and “ &lt;&lt; y &lt;&lt; “is “ &lt;&lt; x + y &lt;&lt; “.<br>”; }.

bonita
Download Presentation

CS 101 Lecture 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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<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”;}

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

  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( ) ;}

  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.

  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.

  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();}

  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.

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

  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 formif ( 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.

  15. if else Statements • If we have a statement of the formif ( 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.

  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

More Related