DIFFERENCE BETWEEN CIN AND COUT COMMAND IN C++ (Syntax, Getting input and Usage) !!!

in #programming8 years ago

In my last blog, I mentioned about using COUT and CIN commands !
I described the use of COUT and promised to tell the difference about CIN and COUT in my next blog !

Here is a link that'll lead you towards my last blog :-
https://busy.org/programming/@frstwalket/initiating-the-body-of-of-c-program-our-first-program

cin cout.jpg

What we will be covering today includes :-

  1. Difference between CIN and COUT.
  2. Syntax of CIN and COUT.
  3. Using CIN and COUT in one program.

Difference:-

  • CIN
    CIN stands for (console input) and as it is specified by it's name, it is used for getting input from the user or from within the program. The CIN command used two brackets pointing towards the name of the variable in which the commands or data is saved ! The best way to keep this in mind is to remember that the CIN command is passing in the value to the variable written after the brackets and the brackets are passing the data to the path specified by the brackets, the path specified is towards the variable !

Cin.jpg

SYNTAX :- cin>> variable name;

Variable name can be any letter, word including numeric digits except special characters like "!@#%^".
Every C++ program must be closed with a Semi-Colon.

  • Cout
    The cout command as we all know is used to display data on the computer screen, this data can include any sort of message, numbers or characters enclosed in quotation marks. The brackets in cout command are always facing towards the keyword "COUT" which is the exact opposite of CIN. It can be remembered by knowing that data is written in quotation marks is being transmitted to the cout command and is being displayed on the computer screen on a path specified by the brackets, the path is towards the cout keyword

cout.jpg

SYNTAX :- Cout<<"Text goes in here";

Text can be any sort of message, instrustion e.t.c.
Every C++ command is closed by a semi-colon.

Using CIN and COUT in the same program :-

As we already know how to make a program with the help of our last blog, we will start by adding our headers and the body !

The Headers and body go like this :-

Explained in last blog

#include< iostream >

Ignore the space between brackets and iostream

#include< conio.h >

Ignore the space between brackets and conio.h

using namespace std;
int main(){
}

c++ body.JPG

Here is an Actual screenshot !

After writing this code, we are going to initiate a variable. If you want it to store numeric data you have to use int before variable name, If you want to store alphabetic data into the variable, you have to use char keyword before the name of variable !

Syntax of initiating variables

Variable type variable name = value;
Variable type can be INT or CHAR, where variable name can be any sort of name including characters, numbers, uppercase and lowercase letters

There are a lot of variable type but in this tutorial, we are going to stick to CHAR and INT variable types. My next writing will be about variable types and their uses !

The Value of the variable can be specified as soon as it is initiated an it can also be described somewhere else in the program

I am using the int variable because i want the user to enter his/her age !

Writing code:-

#include < iostream >
#include< conio.h >
using namespace std;
int main() {
int age;

Age is the name of interger type variable which will hold numeric data !

cout<<"Please enter your age\n";

The message inside Quotation Marks is displayed as it is on the computer screen, hence user know that he/she has to enter his age. \n is used inside quotation marks because it makes the input jump on the next line, it pretty much does the same as the ENTER key does for documents

cin>> age;

The Cin command tells the computer that it has to take input from the user and store it in the variable written after the brackets.

cout<< "Your age is " << age;

The cout command here displays the value of age, the reason it is not written is quotation marks is because we don't want the word age to be printed on the screen but we want the value of age to be printed/displayed. Similarly "Your age is " written in quotation marks because we want out computer to display it as it is on the screen

return 0;

}

Program output

Please enter your age
18

I have supposed that user has entered 18

Your age is 18

Screenshot

Verify your code and output with the screenshot below

Program Output.JPG

Sort:  

Rather than doing

cout  <<  "Hello world.\n";

to put the newline character at the end of the line

I always used to use endl instead (technically std::endl I guess) like this:

cout  <<  "Hello world." << endl;

It always felt cleaner, and felt less like I was writing c.

Yeah that's a nice substitute 😄

Coin Marketplace

STEEM 0.13
TRX 0.33
JST 0.034
BTC 110716.57
ETH 4295.49
USDT 1.00
SBD 0.82