File: Main - CPP Author: A K Created On April 28, 2018 / #Include
File: Main - CPP Author: A K Created On April 28, 2018 / #Include
File: Main - CPP Author: A K Created On April 28, 2018 / #Include
/*
* File: main.cpp
* Author: A K
* Created on April 28, 2018
*/
#include <iostream>
int main()
{
cout<<"This is my first C++ Program.";
cout<<endl<<"and its very easy to learn";
}
The above example has been used to print text on the screen.
Let’s take a look into various parts of the above C++ Program.
/* Comments */ Comments are a way of explaining what makes a program. The compiler ignores
comments and used by others to understand the code.
or
This is a comment block, which is ignored by the compiler. Comment can be used
anywhere in the program to add info about program or code block, which will be
helpful for developers to easily understand the existing code in the future.
#include <iostream.h> This is a preprocessor directive. It tells the preprocessor to include the contents of
iostream header file in the program before compilation. This file is required for input-
output statements.
Int void Int void is a return value, which will be explained in a while.
main() The main() is the main function where program execution begins. Every C++ program
must contain only one main function.
or
This is a main function, which is the default entry point for every C++ program and the
void in front of it indicates that it does not return a value.
Braces Two curly brackets "{…}" are used to group all statements.
or
Curly braces which shows how much the main() function has its scope.
void main()
{
int g;
cin>>g;
cout << "Output is: "<< g;
}
put() and get() functions
The classes istream and ostream defines two member functions get() and put() respectively to handle
single character input/output operations.
Get() function is of two types:
1. get(char *)
2. get(void)
Both of them can be used to fetch a character including blank space, tab or new-line character.
Code snippet
char ch;
cin.get(ch);
while(ch != '\n')
{
cout<<ch;
cin.get(ch);
}
Similarly, the function put(), a member of a stream class can be used to output a line of text character
by character.
Example:
cout.put ('g');
char ch;
cout.put(ch);
getline() and write()
You can read and display lines of text more efficiently using the lie oriented input/output functions. They
are:
getline()
write()
The getline() function reads the entire line of texts that ends with a newline character. The general form
of getline() is:
cin.getline (line, size);
The write() function displays the entire line of text, and the general form of writing this function is:
cout.write (line, size);
Manipulators are operators used in C++ for formatting output. The data is manipulated by the
programmer's choice of display.
Some of the more commonly used manipulators are given below:
endl Manipulator
endl is the line feed operator in C++. It acts as a stream manipulator whose purpose is to feed the
whole line and then point the cursor to the beginning of the next line. We can use \n (\n is an escape
sequence) instead of endl for the same purpose.
setw Manipulator
This manipulator sets the minimum field width on output.
Syntax:
setw(x)
Example:
/*
* File: main.cpp
* Author: Gautam
* Created on October 16, 2011, 12:58 PM
*/
#include <iostream>
#include <iomanip>
using namespace std;
int main() {