Topics

6/recent/ticker-posts

Object Oriented programming language introduction (Basics)



      Basic Input

                                    and Output:

  1. C++ uses a convenient abstraction called streams to perform input and output operations in sequential media such as the screen, the keyboard or a file. A stream is an entity where a program can either insert or extract characters to/from.

  2. There is no need to know details about the media associated to the stream or any of its internal specifications. 

  3. All we need to know is that streams are a source/destination of characters, and that these characters are provided/accepted sequentially (i.e., one after another).


The standard library defines a handful of stream objects that can be used to access what are considered the standard sources and destinations of characters by the environment where the program runs:


stream description:

======================

cin   | standard input stream

===============================

cout  | standard output stream

cerr   | standard error (output) stream

clog  |      standard logging (output) stream


We are going to see in more detail only cout and cin (the standard output and input streams); cerr and clog are also output streams, so they essentially work like cout, with the only difference being that they identify streams for specific purposes: error messages and logging; which, in many cases, in most environment setups, they actually do the exact same thing: they print on screen, although they can also be individually redirected.


Standard output (cout):

=======================


cout << "Output sentence"; // prints Output sentence on screen

cout << 120;               // prints number 120 on screen

cout << x;                 // prints the value of x on screen  

Standard input (cin):

=====================

int age;

cin >> age; //Store the age in the cin input 

Example for the basic input and output:

======================================

// inputoutputexample.cpp


#include <iostream>

using namespace std;

int main ()

{

  int i;

  cout << "Please enter an integer value: ";

  cin >> i;

  cout << "The value you entered is " << i;

  cout << " and its double is " << i*2 << ".\n";

  return 0;

}

cin and strings: 

================

The extraction operator can be used on cin to get strings of characters in the same way as with fundamental data types:



1. string mystring;

2. cin >> mystring;

 //cinwithstrings.cpp ------------------------------------> Program 

#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 team? ";

  getline (cin, mystr);

  cout << "I like " << mystr << " too!\n";

  return 0;

}

Video available here

Please follow the notes given here and Share this Blog. For  any comments or any mistakes report or comment in comment section of this blog.

ALL THE BEST!!!


Post a Comment

1 Comments