Академический Документы
Профессиональный Документы
Культура Документы
BSE 1
Joddat Fatima
1
joddat.fatima@gmail.com
Department of C&SE
Bahria University Islamabad
INPUT OUTPUT
An input stream is data for the program to use
Typically originates
At the keyboard
At a file
2
BASICS
4
STANDARD OUTPUT (COUT)
By default, the standard output of a program is the screen, and the C++
stream object defined to access it is cout.
cout is used in conjunction with the insertion operator, which is written as
<< (two "less than" signs).
The insertion operator (<<) may be used more than once in a single
statement
cout << "Hello, " << "I am " << "a C++ statement";
Hello, I am a C++ statement on the screen
Assuming the age variable to contain the value 24 and the zipcode
contain 44000. 6
SHOWING DECIMAL PLACES
To specify fixed point notation
setf(ios::fixed)
To specify that the decimal point will always be shown
setf(ios::showpoint)
To specify that two decimal places will always be shown
precision(2)
EXAMPLE
cout.setf(ios::fixed); \\scientific
cout.setf(ios::showpoint);
cout.precision(2);
cout << "The price is " << price << endl;
7
STANDARD INPUT (CIN)
The operator must be followed by the variable that will store the data that is
going to be extracted from the stream.
For example:
int age;
cin >> age;
The first statement declares a variable of type int called age, and the second
one waits for an input from cin (the keyboard) in order to store it in this 8
integer variable.
STANDARD INPUT (CIN)
cin can only process the input from the keyboard once the
RETURN key has been pressed.
You must always consider the type of the variable that you are
using as a container with cin extractions
9
READING MULTIPLE DATA ITEMS
You can also use cin to request more than one datum input from
the user:
cin >> a >> b;
Is equivalent to:
cin >> a;
cin >> b;
In both cases the user must give two data, one for variable a and
another one for variable b.
Separating them by any valid blank separator: a space, a tab
character or a newline.
10
EXAMPLE I/O
#include <iostream>
int main()
{
int integer1; // first number to be input by user
int integer2; // second number to be input by user
int sum; // variable in which sum will be stored
integer1 45
integer2 72
sum = integer1 + integer2;
sum 117
12
CIN AND STRINGS
14
STRING STREAM
This way we can perform extraction or insertion operations from/to strings, which is
especially useful to convert strings to numerical values and vice versa.
For example
string mystr;
float price=0;
int quantity=0;
cout << "Enter price: ";
getline (cin,mystr);
stringstream(mystr) >> price;
cout << "Enter quantity: ";
getline (cin,mystr);
stringstream(mystr) >> quantity; 15
cout << "Total price: " << price*quantity <<endl;
EXAMPLES
16
EX # 1
#include <iostream>
using namespace std;
int main()
{
float length, width, area;
return 0; 17
}
EX # 2
#include <iostream>
using namespace std;
int main()
{
float base, height, area;
return 0; 18
}
EX # 3
#include <iostream>
using namespace std;
int main()
{
float radius, area;
return 0; 19
}
EX # 4
#include <iostream>
using namespace std;
int main()
{
float celsius;
float fahrenheit;
cout << "Enter Celsius temperature: ";
cin >> celsius;
fahrenheit = (9/5) * (celsius + 32);
cout << "Fahrenheit = " << fahrenheit << endl;
return 0; 20
}
EX # 5
#include <iostream>
using namespace std;
int main()
{
float celsius;
float fahrenheit;
cout << "Enter Fahrenheit temperature: ";
cin >> fahrenheit;
celsius = (5/9) * (fahrenheit - 32);
cout << " Celsius = " << celsius << endl;
return 0; 21
}