C++ tutorial-print text on the screen


Printing text on the screen


The cout command can be used to print the values of variables on the screen. If you want to print a string value, you need to place the string value in dobule quotes. To print a variable, you won't need to place the variable in double or single quotes. You can print many values or variables with a single cout statement. Each value or variable must be preceeded by the << sign. See the example below.

Example:
char *s = "Dara";
cout << "Your name is " << s;

Reading values from keyboard

The cin command is used for reading values that are entered by the user. When you read a value you must store it in a variable. The below example prompts the user to enter his/her name. The s variable is used to store the characters of the name. You can use a single line of cin statement to input many values. Each variable that stores the value must be proceeded by the >> sign.


Example:
char *s;
cout << "Enter your name: ";
cin >> s;

Constant Variable

A constant variable is a variable whose value cannot be changed later by the code in the program. The value of a constant must always be set when it is declared. There are two ways to declare a constant in C++ language. The first is to use const keyword before the variable.


Example:
const PI = 3.14;

The second way is to use #define to create a constant.

Example:
#define PI 3.14




Comments

Harrison comment

 Harrison

Some compilers don't like const PI = 3.14;
some require const float PI = 3.14;


2017-05-16
bilal  comment

 bilal

help me in c++ plzzzz


2015-06-29
Troy comment

 Troy

for more C++, C exercises,
please visit http://ccphplusplus.blogspot.com/


2015-05-26



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.