Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Lecture 2 - Basic Input and Output

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lecture 2 - Basic Input and Output

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Lecture 2

Basic Input and Output


Objectives
• Explain how to communicate to the system while
entering data and displaying output.

• Write simple programs that write information on the


screen.

• Explain how to read a single character and a string.

• Clarify how to convert strings to numerical values and


vice versa.

• Explain how to use the gets() and puts() functions a


programs.
2
Basic Input and Output
• C++ uses streams to perform input and output
operations.

• A stream is an entity where a program can


either insert or extract characters to/from.

• The standard input and output streams are cin


and cout.

3
Standard output - cout
Screen

Result

Identifier

cout << value

object
Insertion Operator

4
Standard output - cout
• cout is the C++ stream object which is defined
to access the standard output.

• cout is used together with insertion operator


<<.

• The library #include<iostream> should


be included in the program to use cout and
cin.

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.

• If the text is not enclosed with double quotes, it is


interpreted as an identifier and its value is printed.

• Example: the following two statements have different


results.
– cout<<“welcome”;
– welcome = “welcome to C++ programming class”;
– cout<<welcome;

8
Standard output - cout
• Multiple insertion operations (<<) may be chained in
a single statement.
• Example:
– cout << "This " << " is a " << "single
C++ statement";

• Chaining insertions is useful to mix literals and


variables in a single statement
• Example:
– int num = 3;
– cout << “The value” << num << “ is an integer”;

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.

• New-line character \n can be used to create line


breaks.
• Example:
– cout<<“This is the first line of text\n”;
– cout<<“This is the second line of text”;
10
Standard output - cout
• Alternatively, the endl manipulator can also be used
to break lines.
• Example:
– cout<<“This is the first line of text”<<endl;
– cout<<“This is the second line of text”;

• 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

standard input stream

12
Standard input - cin
• The C++ stream object defined to access the
standard input (keyboard) is cin.

• cin is used together with the extraction


operator >> followed by a variable where the
extracted data is stored.

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

• The program will wait for the user to enter some


sequence with the keyboard.

• Press enter key to transmit the value to the


program.
14
Standard input - cin
• The extraction operation on cin uses the type of the variable after
the >> operator to determine type of value.

• If the data type is integer, the format expected is a series of digits

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

• cin extraction always extract a single word.


• cin extraction always considers spaces as terminating the value
being extracted.
19
cin and strings
• getline() function can be used to get an entire line
from cin.
• It takes the stream cin as first argument, and the string
variable as second.
• Example:
#include <iostream>
#include <string>
using namespace std;

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.

• It collects a string of characters terminated by


a new line.

• gets()allows input strings to contain certain


white space characters.

• gets()returns when it encounters a new line;


everything up to the new line is copied into the
parameter. 21
puts() function
• This function can be used to display a string.

• puts()function copies the null-terminated


string of the identifier used as an argument.

• The header file #include<stdio.h>


should be include in the program when using
the gets()and puts()function.

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.

• It allows extraction operations from strings in


the same way as they are performed on cin.

• This feature is useful to convert strings to


numerical values and vice versa.
24
stringstream
• Example - to extract an integer from a string
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

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;

cout << "Enter price: ";


getline (cin,mystring);
stringstream(mystring) >> price;
cout << "Enter quantity: ";
getline (cin,mystring);
stringstream(mystring) >> quantity;
cout << "Total price: " << price*quantity;
}
26
Standard input and output
#include <iostream>
using namespace std;

int main ()
{
float price=0;
int quantity=0;

cout << "Enter price: ";


cin >> price;

cout << "Enter quantity: ";


cin >> quantity;
cout << "Total price: " << price*quantity;
}

27
stringstream
• In this example, we acquire numeric values from the
standard input indirectly.

• Instead of extracting numeric values directly from cin,


we get lines from it into a string object (mystring), and
then we extract the values from this string into the
variables price and quantity.

• Once these are numerical values, arithmetic operations


can be performed on them.

• This allows the input process to be what the user


expects, and gaining more control over the
transformation of its content into useful data by the
program. 28

You might also like