Getting User Input Using cin in C++

By: Fazal Emailed: 1769 times Printed: 2516 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

The global object cin is responsible for input and is made available to your program when you include iostream.h. In most of the examples in this site, you used the overloaded extraction operator (>>) to put data into your program's variables. How does this work? The syntax, as you may remember, is the following:

int someVariable;
cout << "Enter a number: ";
cin >> someVariable;

The global object cout is discussed later today; for now, focus on the third line, cin >> someVariable;. What can you guess about cin?

Clearly it must be a global object, because you didn't define it in your own code. You know from previous operator experience that cin has overloaded the extraction operator (>>) and that the effect is to write whatever data cin has in its buffer into your local variable, someVariable.

What may not be immediately obvious is that cin has overloaded the extraction operator for a great variety of parameters, among them int&, short&, long&, double&, float&, char&, char*, and so forth. When you write cin >> someVariable;, the type of someVariable is assessed. In the example above, someVariable is an integer, so the following function is called:

istream & operator>> (int &)

Note that because the parameter is passed by reference, the extraction operator is able to act on the original variable. Listing below illustrates the use of cin.

cin handles different data types.

1:     //character strings and cin
3:     #include <iostream.h>
5:     int main()
6:     {
7:        int myInt;
8:        long myLong;
9:        double myDouble;
10:       float myFloat;
11:       unsigned int myUnsigned;
13:       cout << "int: ";
14:       cin >> myInt;
15:       cout << "Long: ";
16:       cin >> myLong;
17:       cout << "Double: ";
18:       cin >> myDouble;
19:       cout << "Float: ";
20:       cin >> myFloat;
21:       cout << "Unsigned: ";
22:       cin >> myUnsigned;
24:       cout << "\n\nInt:\t" << myInt << endl;
25:       cout << "Long:\t" << myLong << endl;
26:       cout << "Double:\t" << myDouble << endl;
27:       cout << "Float:\t" << myFloat << endl;
28:       cout << "Unsigned:\t" << myUnsigned << endl;
29:     return 0;
30: }

Output: int: 2
Long: 70000
Double: 987654321
Float: 3.33
Unsigned: 25

Int:    2
Long:   70000
Double: 9.87654e+08
Float:  3.33
Unsigned:       25

Analysis: On lines 7-11, variables of various types are declared. On lines 13-22, the user is prompted to enter values for these variables, and the results are printed (using cout) on lines 24-28.

The output reflects that the variables were put into the right "kinds" of variables, and the program works as you might expect.

C++ Home | All C++ Tutorials | Latest C++ Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


1. View Comment

pls sed total informatio about c++

View Tutorial          By: anu at 2009-07-22 11:13:07
2. View Comment

Dear admin,
your Recaptcha design is not good because captcha code is calling some another css class from .php page .just find the class name from where you have used this google recaptcha and remove that or rename with other name(if required).

thanks and regards
surendra kumar sharma

View Tutorial          By: surendra kumar sharma at 2013-02-02 05:58:35

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by Fazal
The Tag Life Cycle with Attributes in JSP
Using Shift Operators in C
IncludeAction in Struts
typedef example program in C
Arithmetic Evaluation Using the Expression Language in JSP
Tutorial on Complicated Declarations in C
Static Variables in C
getPosition() the right strategy in getting GPS
Precedence and Order of Evaluation in C
File Copying in C
JSF Life Cycle
Getting User Input Using cin in C++
UDP Datagram sample in J2ME
GUI components and menu based J2ME Applications.
A Serialization Example in Java

More Tutorials in C++
Two-Dimensional Array Manipulation in C++
Calculate average using Two-Dimensional Array in C++
Compute the square root of the sum of the squares of an array in C++
Matrix using nested for loops in C++
Sorting an array of Strings in C++
Calculating total based on the given quantity and price in C++
Compiling and Linking Multiple Source Files in C++
Enumerations in C++
Program to add two numbers in C++
Comments in C++
while loop in C++
for loop in C++
Programming errors a compiler will detect in C++
if in C++
Using the Built-in Arithmetic Types in C++

More Latest News
Most Viewed Articles (in C++ )
Getting User Input Using cin in C++
Using Comments in a C++ Program
do...while Loops in C++
Using switch Statements in C++
The Stack and the Free Store in C++
Converting Pointer Parameters in C++ to Java
Converting C++ Multiple-Inheritance Hierarchies to Java
Operator Precedence in C++
assert() example program in C++
cin.ignore() in C++
Implementing Pure Virtual Functions in C++
Vector variables in C++
Sum of 1 to N in C++
Multi-Line Literals in C++
Enumerations in C++
Most Emailed Articles (in C++)
Demonstrating global and local variables in C++
C++ and Object-Oriented Programming
Using cout in C++
Use of Conditional (Ternary) Operator in C++
const Member Functions in C++
Classes with Other Classes as Member Data in C++
What Is a Pointer in C++?
Manipulating Data by Using Pointers
Stray or Dangling Pointers in C++
C++ Destructors Versus Java Finalization
atoi(), itoa() in C++
strcat() and strncat() sample program in C++
Using #define, The Preprocessor and the Compiler in C++
Getting User Input Using cin in C++
Pass by Reference in C++ functions