0% found this document useful (0 votes)
120 views5 pages

Lesson 5 - C++ Cin & Strings

C++ allows input and output through streams. The cin object represents standard input and is used to accept input from the keyboard. The extraction operator (>>) is used with cin to receive input which is then stored in a variable. The getline() function can also be used with cin to get an entire line of input as a string. Stringstreams allow strings to be treated as streams, enabling extraction of data from strings in the same way as from cin.

Uploaded by

jhi1medina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
120 views5 pages

Lesson 5 - C++ Cin & Strings

C++ allows input and output through streams. The cin object represents standard input and is used to accept input from the keyboard. The extraction operator (>>) is used with cin to receive input which is then stored in a variable. The getline() function can also be used with cin to get an entire line of input as a string. Stringstreams allow strings to be treated as streams, enabling extraction of data from strings in the same way as from cin.

Uploaded by

jhi1medina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Lesson 5 - Cin and strings

C++ comes with libraries that provide us with many ways for performing input and
output. In C++ input and output are performed in the form of a sequence of bytes or
more commonly known as streams.

 Input Stream: If the direction of flow of bytes is from the device(for example,
Keyboard) to the main memory then this process is called input.
 Output Stream: If the direction of flow of bytes is opposite, i.e. from main
memory to device( display screen ) then this process is called output.

 iostream: iostream stands for standard input-output stream. This header file
contains definitions to objects like cin, cout, cerr etc.

What is cin?

The cin object in C++ is an object of class istream. It is used to accept the input from
the standard input device i.e. keyboard. It is associated with the standard C input
stream stdin.

cin declaration

extern istream cin;

The cin object is used along with the extraction operator (>>) in order to receive a

stream of characters. The general syntax is:

cin >> varName;

The cin object can also be used with other member functions such as getline(), read(),
etc.

Example 1: cin with extraction operator:

#include <iostream>

using namespace std;

int main()
{
int x, y, z;
/* For single input */
cout << "Enter a number: ";
cin >> x;

/* For multiple inputs*/


cout << "Enter 2 numbers: ";
cin >> y >> z;

cout << "Sum = " << (x+y+z);


return 0;
}

When you run the program, a possible output will be:

Enter a number: 9
Enter 2 numbers: 1 5
Sum = 15

Example 2: cin with member function:

#include <iostream>

using namespace std;

int main()
{
char name[20], address[20];

cout << "Name: ";


cin.getline(name, 20);

cout << "Address: ";


cin.getline(address, 20);

cout << endl << "You entered " << endl;


cout << "Name = " << name << endl;
cout << "Address = " << address << endl;

return 0;
}

When you run the program, a possible output will be:

Name: Sherlock Holmes


Address: Baker Street, UK

You entered
Name = Sherlock Holmes
Address = Baker Street, UK

The extraction operator can be used on cin to get strings of characters in the same
way as with fundamental data types:

1 string mystring;
2 cin>>mystring;

However, cin extraction always considers spaces (whitespaces, tabs, new-line...) as


terminating the value being extracted, and thus extracting a string means to always
extract a single word, not a phrase or an entire sentence.

To get an entire line from cin, there exists a function, called getline, that takes the
stream (cin) as first argument, and the string variable as second.

Example 3:

1 // cin with strings What's your name? Homer Simpson


2 #include <iostream> Hello Homer Simpson.
3 #include <string> What is your favorite team? The Isotopes
4 using namespace std; I like The Isotopes too!
5
6 int main ()
7{
8 string mystr;
9 cout<< "What's your name? ";
10 getline (cin, mystr);
11 cout<< "Hello " <<mystr<< ".\n";
12 cout<< "What is your favorite team? ";
13 getline (cin, mystr);
14 cout<< "I like " <<mystr<< " too!\n";
15 return 0;
16 }

Notice how in both calls to getline, we used the same string identifier (mystr). What
the program does in the second call is simply replace the previous content with the
new one that is introduced.

stringstream
The standard header <sstream> defines a type called stringstream that allows a
string to be treated as a stream, and thus allowing extraction or insertion operations
from/to strings in the same way as they are performed on cin and cout. This feature is
most useful to convert strings to numerical values and vice versa. For example, in
order to extract an integer from a string we can write:

1 string mystr ("1204");


2 intmyint;
3 stringstream(mystr) >>myint;

This declares a string with initialized to a value of "1204", and a variable of


type int. Then, the third line uses this variable to extract from
a stringstream constructed from the string. This piece of code stores the numerical
value 1204in the variable called myint.

In order to use the string data type, the C++ string header <string> must be included
at the top of the program. Also, you’ll need to include using namespace std; to make
the short name string visible instead of requiring the cumbersome std::string. (As a
side note, std is a C++ namespace for many pieces of functionality that are provided in
standard C++ libraries. For the purposes of this class, you won't need to otherwise
know about namespaces.) Thus, you would have the following #include's in your
program in order to use the string type.

Example 4:

1 // stringstreams Enter price: 22.25


2 #include <iostream> Enter quantity: 7
3 #include <string> Total price: 155.75
4 #include <sstream>
5 using namespace std;
6
7 int main ()
8{
9 string mystr;
10 float price=0;
11 int quantity=0;
12
13 cout<< "Enter price: ";
14 getline (cin,mystr);
15 stringstream(mystr) >> price;
16 cout<< "Enter quantity: ";
17 getline (cin,mystr);
18 stringstream(mystr) >> quantity;
19 cout<< "Total price: " << price*quantity <<endl;
20 return 0;
21 }

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 (mystr), 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, such as multiplying them to obtain a total price.

You might also like