CS 101 Lecture 2

Download Report

Transcript CS 101 Lecture 2

CS 101
Lecture 2
1
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”;
}
2
• 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
3
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
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.
4
• #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( ) ;
}
5
• 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<iostream.h>
because #include<fstream.h> allows us to type cin or
cout.
6
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
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.
7
• #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();
}
8
• 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 oneline 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.
9
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.
10
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
11
Quiz 1 Solution
• #include<fstream.h> //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
12
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.
13
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+1
is 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.
14
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+1
is 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.
15
if and if else Program
• #include<iostream.h> // 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 ) x = x + 4; // x < y is true , so x is now 7
y = y - 3; // y is now 5
cout << x << ‘ ‘ << y << endl;//print x and y to the screen
if ( x > y ) cout << x ; // x > y is false so x is not printed
else cout << y; // x > y is false so y is printed
cout << endl; // in either case print a newline
} // end program
16