C++ is a powerful, complex programming language particularly well-suited to solving sophisticated problems. It may not feature the elegant simplicity high-level programming languages like Java and PHP enjoy, but it is a must-have tool for developing operating systems, software drivers, generalizable web servers, and embedded systems.
As a mature, performance-based coding language, it can run nearly anywhere and benefits from a large community of developers. It's the language of choice when creating large-scale commercial applications – huge systems designed for fast, efficient performance.
Before getting to those systems, beginning C++ coders need to become deeply familiar with some of the language's main functions. CIN (pronounced see-in) is one of them.
CIN combines with COUT (see-out) under the standard header <iostream> to allow programs to input and output data. Using CIN and COUT to become acquainted with the way C++ works is an important step for any future programmer.
In order to use this tutorial, you will need to use Microsoft Visual Studio for Windows or XCode for OS X. These are the compilers you will use to execute the C++ code examples below.
Writing Simple Code Using CIN in C++

When your code compiler sees CIN, C++ expects to see an input. Usually, the best option for inputting value to a C++ program is the computer keyboard. This can change with more advanced use cases, but for the purpose of this guide, the keyboard is all you need.
One of the simplest possible CIN functions is assigning a numerical value to an integer. Most C++ students will write and compile a program like this one at some point in their introductory courses:
#include <iostream>
using namespace std;
int main()
{
int x;
cout <<"Enter a number"; //prompts the user to input a value
cin >> x; //assigns that value to the variable x
cout << endl << "You entered: " << endl;
cout << x << endl; //Shows the value of x as assigned
return 0;
}
The most important thing to know about this barebones snippet of C++ code is what happens after int main(). The code that says int x defines the variable x so that you can you can use it later on. In this case, you are using it to define whatever the program's user inputs as x.
You'll notice that CIN directly takes the user input and assigns it to the variable without interpreting or qualifying it. The extraction operator >> tells the program to read the following non-blankspace characters. If you want to treat user inputs in a more sensible, human-readable way, you need to use CIN.get and CIN.getline.
Writing Code with CIN.getline in C++
Here is a simple program that uses the member function to define the variable the CIN is supposed to get:
#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;
}
In this case, the program defines the user input as part of a member function, defined by char name[20] and address[20]. When someone starts this program, the command line will prompt the user to input a name and then input an address. It will then display the name and address entered.
The ability to define inputs as member functions allows you to use cin.get and cin.getline to treat user input as parts of a function. This is a simple and straightforward way to get input from a user and then manipulate it through programming.
What Is the Difference Between CIN.get and CIN.getline?
CIN.get and CIN.getline in very similar ways. Both of them interpret input data as part of the defined member function. However, there is a subtle difference in the way each one works.
- CIN.get includes white-space characters. This can include delimiters and newline characters, like the Enter/Return button.
- CIN.getline restricts its function to the line in question. It removes the terminating character of the line, giving you a neat line that you can then manipulate further inside the program.
Since most people expect to terminate input by hitting Enter/Return, beginning programmers should use CIN.getline for user input. The only time you would want to use CIN.get is when you want the include terminating characters, or if you want to use a more specific means to identifying what part of the user's input the program will use.
For instance, if you define a member function using char line[25] and then use cin.get( line, 25 ), your program will terminate anything beyond the 24th character the user inputs.
CIN.get and CIN.getline exist to allow programmers to qualify user inputs. If you build a program that calls directly from CIN, you run the risk of the program crashing every time the user makes a tiny mistake. This is poor program behavior – people expect programs to function predictably regardless of whatever input they give it.
One More CIN Function: CIN.Ignore
If you want to extract a certain amount of characters from a user input and want to include blankspace characters in the content, you can use CIN.ignore to do so. This function looks like this:
cin.ignore( int nCount = 1, int delim = EOF );
Where nCount refers to the number of characters to extract and delim defines what the delimiter character is. The default delimiter is the EOF, which stands for end-of-file. Here is an example how to use CIN.ignore in a simple program:
// istream::ignore example
#include <iostream>
int main ()
{
char first, last;
std::cout << "Please, enter your first name followed by your surname: ";
first = std::cin.get(); // get one character
std::cin.ignore(256,' '); // ignore until space
last = std::cin.get(); // get one character
std::cout << "Your initials are " << first << last << '\n';
return 0;
}
This program will take ask the user for a full name and surname in one space, and then define two variables based on where the user puts a space. The text before the space is the first name and the text after the space is the last name.
Introducing Strings and Stringstream
If you combine Stringstreams with CIN, C++ compilers can treat strings of characters just like the other fundamental types of data that you're used to working with.
This requires the use of CIN.getline. If you use CIN.get, the spaces that a user puts between words will tell the program to end the input – you will only ever get a single word, rather than a phrase or sentence.
In order to use strings, you will have to include the string stream standard header and then define the CIN input using (mystr). Consider this example:
// cin with strings
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string mystr;
cout << "What's your name? ";
getline (cin, mystr);
cout << "Hello " << mystr << ".\n";
cout << "What is your favorite sports team? ";
getline (cin, mystr);
cout << "I like " << mystr << " too!\n";
return 0;
}
In this example, the program will greet the user using his or her name and then say it likes the same sports team the user defines. In both cases, the program uses the same variable to refer to the input variables.
During the second call of the string identifier (mystr), the program simply uses the most recent input string. If you use
If you use the standard header <sstream>, you can define a stringstream in such a way as to treat a user input string as a stream. This allows you to perform extraction or insertion operations with strings the same way you do with CIN and COUT.
The stringstream is exactly what it sounds like. It's a stream of user-input strings that is created by telling the program to throw away the previous input and replace it with a new one.
This behavior allows you to write programs that handle inputs in a more organized and less heavy-handed way than identifying every single variable that a user may input. Consider this example:
// stringstreams
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main ()
{
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;
cout << "Total price: " << price*quantity << endl;
return 0;
}
This program gets numeric values directly from user input and multiplies them. Instead of using CIN directly, it uses getline to manipulate user input prices and treat them as variables, defined as price and quantity.
Without stringstream functionality, writing a simple program like this would require defining each input integer individually as x or y and then instructing the program to manipulate each one in particular. This can become unwieldy when dealing with a large number of integers in a complex program.
This allows the programmer to separate the process of obtaining user input from the process of interpreting that input as data. This way, the user gets the experience he or she expects, while the programmer enjoys greater control over the way the program manipulates and transforms data according to its algorithms.