Today we are gonna talk a little bit about different "Operators" in C++. Operators are anything, which are coded to do something. We already have talked about one operator : the output operator. Actually, the output ( cout ) operator, is the part of a set of operators called input-output (I/O) operators, which also contains many other similar operators.
Next important operator we are gonna learn from I/O operator is the basic input operator. Similar to "cout", the code for input operator is "cin". The header file needed for cin is <iostream.h> . The general format of this operator is :
Next important operator we are gonna learn from I/O operator is the basic input operator. Similar to "cout", the code for input operator is "cin". The header file needed for cin is <iostream.h> . The general format of this operator is :
'a' can be any variable you want to insert from the user. This can be a bit difficult to digest. But this is very simple... Lets do a practical. Suppose you want to insert a number from the user. First thing you want to do is to declare that a variable (a number which is yet to be known) is present in the program.
Declaration of a variable means, letting the compiler know that a variable is present in the program, so as to run it. To declare anything in C++, you want to inform the program it's datatype.
Now a datatype means the type of the thing you want to add to the program. There are two types of datatypes. They are Basic Datatypes and Derived datatypes. Any program which involves integer operations, should have the "int main()" function. i.e. we have to replace the "void main()" function of the last program with "int main()".
We will learn more datatypes later. Now all you have to know is, an integer number (eg: 0, 2, 13, 63, -4, -7, -10) is of the datatype "int". The second thing to know about variable declaration is that, a variable has to be given a name. Since it is an unknown number, it is appropriate to name it an alphabet like 'x', or a, just like in algebra, where we name unknowns 'x'. This pseudo-name we give to unknown things like a character, or a number is called Identifiers Now lets name our number to be input by our user 'u'. (An identifier need not to be of a single character, we may also name our number 'unknownnumber' or something, but note that white spaces are not allowed in between the text of an identifier, and also it is for simplicity, we are naming our number 'u'). To letting the program know that a variable 'u' exists, you should insert this code just after opening the braces of the main() function:
Now that your program knows it involves an unknown number, next thing you will need to do to input a number from user, is, you should inform the user that the program is expecting a number from him/her. To do this, we just have to output a message saying to input the number, just like this:
Now as the user reads the message he will try to enter the number, but will fail miserably, because *tadaaa* there is no input operator. This is where "cin" comes into play. Now you should enter this code just after the message to user:
Now the user will be able to enter the number, and as soon as he presses enter, the value he types get stored under the identifier 'u'. If we call 'u' again, the value of 'u' will be used. To understand this let us include this code to output 'u'.:
No comments:
Post a Comment