Lecture 2 - Basic Input and Output
Lecture 2 - Basic Input and Output
3
Standard output - cout
Screen
Result
Identifier
object
Insertion Operator
4
Standard output - cout
• cout is the C++ stream object which is defined
to access the standard output.
5
Standard output - cout
#include<iostream>
using namespace std;
int main()
{
// prints the string between the quotes
cout<<"This is a string output";
// prints the number 50
cout<<50;
int number = 34;
// prints the value of number variable
cout<<number;
}
6
Standard output - cout
• The << operator inserts the data that follows it into the stream that
precedes it.
• Example:
– cout<<"This is a string output";
• In this example, it inserted the literal in double quotes into the
standard output stream cout.
• Example:
– cout<<50;
• In this example, it inserted the number 50 into the standard output
stream cout.
• Example:
– int number = 34;
– cout<<number;
• In this example, it inserted the value of variable number into
the standard output stream cout.
7
Standard output - cout
• When the text is enclosed in double quotes, it is
printed literally.
8
Standard output - cout
• Multiple insertion operations (<<) may be chained in
a single statement.
• Example:
– cout << "This " << " is a " << "single
C++ statement";
9
Standard output - cout
• cout does not crate line breaks automatically.
• Example:
– cout<<“This is the first line of text”;
– cout<<“This is the second line of text”;
• The output of the above two statements will be a
single line.
• Example:
– cout<<“This is the first line of text”;
– cout<<endl<<“This is the second line of
text”;
11
Standard input - cin
object Identifier
cin >> value
Extraction operator
Keyboard
12
Standard input - cin
• The C++ stream object defined to access the
standard input (keyboard) is cin.
• Example:
– int number;
– cout<<“Please enter a number ”;
– cin>>number;
13
Standard input - cin
• In the previous example:
– The first statement declares a variable of type int
called number.
– The second statement displays a message.
– The third statement extracts from cin a value to be
stored in it.
#include <iostream>
using namespace std;
int main()
{
int num;
cout << "Please enter an integer value: ";
cin >> num;
cout << "The value you entered is " <<num;
}
15
Standard input - cin
• Extraction on cin can be chained to get more
than one data items in a single statement.
• Example:
– cin >> number1 >> number2;
• The above statement can be written as two
separate statements.
• Example:
– cin >> number1;
– cin >> number2;
• In both cases, user has to input two values, one
for each variable.
• A space is used to separate two consecutive input
operations.
16
Standard input - cin
• Example: Reading a single character
#include<iostream>
using namespace std;
int main()
{
char name;
cout<<"Enter the first name ";
cin>>name;
cout<<"Your name is "<<name;
}
17
Standard input - cin
• Example: Reading a string
#include<iostream>
using namespace std;
int main()
{
char name[6];
cout<<"Enter the first name ";
cin>>name;
cout<<"Your name is "<<name;
}
18
cin and strings
• The extraction operator >> can be used on cin to get strings of
characters.
• Example:
#include<iostream>
using namespace std;
int main()
{
string fname;
cout<<"Enter the first name ";
cin>>fname;
cout<<"First name is " <<fname;
}
int main ()
{
string fullname;
cout << "What is your name? ";
getline (cin, fullname);
cout << "My name is " << fullname;
}
20
gets() function
• Function is used to get a string.
22
Example 3: Reading a String
• Example: Reading a string
#include <iostream>
#include<stdio.h>
using namespace std;
int main ()
{
char name[20];
cout<<"Please enter a name “;
gets(name);
puts(name);
}
23
stringstream
• The standard header #include<sstream>
defines a type called stringstream that allows a
string to be treated as a stream.
int main ()
{
string myid ("000008945");
int studid;
stringstream(myid) >> studid;
cout<<studid;
}
25
stringstream - extract an integer from a string
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main ()
{
string mystring;
float price=0;
int quantity=0;
int main ()
{
float price=0;
int quantity=0;
27
stringstream
• In this example, we acquire numeric values from the
standard input indirectly.