Lesson # 3 Working with cout and cin

Lesson # 3

WORKING WITH COUT AND CIN
cout is a function from iostream (input output stream) used to display something on the screen, we always put << sign after cout function, which indicates output. We use Special Characters with cout, in order to enhanced its functionality.

Endl
Writes new line move the cursor to next line. e.g. cout<<endl;

\n
Writes a new line or move the cursor to next line, and \n can be used within a string e.g. cout<< “hello \n world”;

\t
Inserts five spaces, just like tab key on keyboard e.g. cout<< “Hello\tworld”;

Let’s make it clear with an example
C ++
Explanation
using namespace std;
#include<iostream>
main()
{

cout<<"Hello World";
Displays the whole string on the screen
cout<<endl;
Inserts a new line. endl is a special command used in c++.

cout<<"My name is...";
Display the whole string on the screen
cout<<"\nHello World\nMy name is ...";
 \n at the beginning of the string  (i.e. \nHello World) inserts a new line before the string.
 \n in the middle of the strings (i.e.  Word\nMy) inserts a new line between Hello world and My name is …”.
Mind it, \n is a special character from C programming language.

cout<<"\nHello World\tMy name is ...";
\n at the beginning of the string  (i.e. \nHello World) inserts a new line before the string.
 \t in the middle of the strings (i.e.  Word\tMy) inserts a five spaces between Hello world and My name is …”.

cout<<"\n";
Insert a new line.
Remember you can use these special characters many times in a single string such as  cout<< “\t \t My name is…”
system("pause");
}


Output
Hello World
My name is...
Hello World
My name is ...
Hello World     My name is ...
Press any key to continue . . .

CODE IN ACTION 



PROBLEMS WITH COUT
The problem with cout is that it can display four digits precision for example cout<<545.645444 will be displayed as 545.654 in the output. To tackle this problem. we use setprecision function from iomanip library. First we have include the library using #include(iomanip). Here is an example how to do it.

C ++
Explanation
using namespace std;

#include<iostream>

#include<iomanip>
Includes the iomanip (input output manipulation) library
main()

{

double x=545.645444;
Declares and initialize a double variable with value of  545.645444
cout<<x;
Displays the variable with default digits precision
cout<<endl;

cout<<setprecision(10);
Sets precision as 10 digits by calling setprecision methods.
cout<<x;
displays the variable x with defined precision i.e. setprecision(10)
cout<<"\n";

system("pause");

}

Output
545.645
545.645444
Press any key to continue . . .

CODE IN ACTION


cin is also a function from header file iostream and is used to get data (input) from the keyboard. We always put >> sign after cin function, which indicates input to be enter from keyboard.
C ++
Explanation
using namespace std;
#include<iostream>
main()
{

int y;
declares  an integer variable y
cin>>y;
gets input from keyboard and store it in y variable
cout<<y;
displays  y variable on the screen.
cout<<"\n";

system("pause");

}

Output
88
88
Press any key to continue . . .
  
CODE IN ACTION
 



PROBLEMS WITH CIN
We can also use cin for strings but it can work only with a single variable with no spaces. For example, while entering from keyboard if we type Hello world, it will store only Hello and will ignore world because cin works with a string without spaces. C++ provides a better way to handle this problem, getline function can enforce cin to get string with spaces between. Here is an example how to do it.

C ++
Explanation
using namespace std;

#include<iostream>

main()

{

string fullname;
declares a string variable fullname
getline(cin,fullname);
enforces cin to get string with spaces, two parameters are passed to getline function; cin and the name of the string i.e. fullname.
cout<<fullname;
displays fullname string on the screen
cout<<"\n";

system("pause");

}

Output
Amir Saleem
Amir Saleem
Press any key to continue . . .

CODE IN ACTION
 CODE 


Comments: Note the input string from keyboard which is Amir Saleem but cout dispalys Amir only , it is because of cin stores only amir as single string.

C++ provides a better way to handle this problem, getline function can enforce cin to get string with spaces between. Here is an example how to do it.
 using namespace std;
#include<iostream>
main()
{
string fullname;
getline(cin,fullname);
cout<<fullname;
cout<<"\n";
system("pause");
}

Let’s do a little discussion on code.
using namespace std;
#include<iostream>
main()
{
string fullname;                                //declares a string variable fullname
getline(cin,fullname);    // enforces cin to get string with spaces, two parameters are passed to getline function; cin and the name of the string i.e. fullname.
cout<<fullname;              // displays fullname string on the screen
cout<<"\n";
system("pause");
}

 Code & output:


No comments:

Post a Comment