Input Statement on C++

a. CIN( )
In C + +, cin command is used to input a value from a input device (keyboard) for further processed by the program.
Syntax is cin>> variable;
example:

court <<"Enter a number:";
cin>> bil;
court <<"You entered number" <<bil "\ n";
b. GETCH ( )
Command getch () function just like cin (command inputs), but getch () specifically for input in the form of characters. Besides, getch () to read input form a space or a tab, whereas cin can not.
syntax is: Variable = getch ();
This function can also be used if the suppression of unwanted ENTER when character data input.

Getche command () uses the same as getch (), the difference is:
- getch () does not display characters diiinput
- getche () displays the inputted character
function getch () and getche () are both needed header files conio.h
Here are examples of the use of getch () and getche ()

char character;
court <<"enter a character:";
character = getch ();
court <<"You typed characters:" <<character;
court <<"enter a character:";
character = getche ();
court <<"You typed characters:" <<character;

Operator and Statements

Operator is a symbol commonly involved in the program for perform an operation or manipulation, for example to:
- add two values
- assign value to a variable (assignment)
- comparing the similarity of two values​​.
a. Arithmetic Operator
This operator is used for basic arithmetic calculations. These operators include:
- Multiplication (*)
- Distribution (/) - Divide
- Addition (+)
- Reduction (-)
- Modulus (%)
b. Relational Operator 
This operator is used to compare two values​​. Here's many operator of this type.
- Equal (==)
- Not equal (!=)
- Smaller (<)
- Bigger (>)
The result of this operator is true or false statements. Look this sample program:
value_1 = 3 > 2;
value_2 = 15 == 16;
cout << “Result of value 1 = ”<< value_1 << “\n”;
cout << “Result of value 2 = ”<< value_2 << “\n”;
c. Logical Operator
This operator is used to connect 2 or more statements. Usually statement is linked is a relational operation. Logical operators as well logic value true or false.
- AND (&&)
- OR (||)
- NOT (!)


Look this sample program.
number1 = (3 > 2) && (4 < 10);
number2 = !(15 == 15);
cout << “Result of number1 = ”<< number1 << “\n”;
cout << “Result of number2 = ”<< number2 << “\n”;

C++ Elements

a. Identifier
Identifier is a name commonly used in programming for declare variables, constants, data type, and function. Rules for writing the identifier together with the rules in pascals, among others:
- It should not be initiated by non-letter characters
- There should be no spaces
- Not allowed to use the characters  ~! @ # $% ^ & * () + `- = {} []:"; '<>?,. /
- Not allowed to use reserved words on c++

b. Data type
Data types used in c + + programming include:
Rounded number data type:
- char
- int (integer)
- short (short integer)
- long (long integer) 
Real number data type:
- float (real)
- double (real double)
- long double
 
Unsigned data type:
- unsigned char
- unsigned int (integer)
- unsigned short (short integer)
- unsigned long (long integer)

Unsigned data types which are not similar to unsigned. The difference is the data type unsigned does not recognize negative numbers (its value is always positive).
c. AssignmentAssignment process is the process of giving value to a variable that has been declared.
Here are examples of assignments:
Number = 10;
Price_unit= 23 456;
James= 'B';
Dean= '2 ';

 
Here's an example program that illustrates the declaration of variables and assignment.
#include <iostream.h>
#include <conio.h>
void main()
{
int var1, var2, var3;
char karakter;
var1 = 10;
var2 = 5;
var3 = var1 + var2;
karakter = ‘D’;
cout << “Value of var3 = ” << var3 << “\n”;
cout << “Value of character  = ” << karakter;
getch();
}
For next example, we can create program with precision statement
#include "conio.h";
#include "iostream.h";
#include "iomanip.h";
void main()
{
clrscr();
double real;
real = 182.2182713674821746;
cout << setprecision(12);
cout << "Value of real = " << real;
getch();
}



Declaration of Constanta (const)

It is simple in concept, variables declared with ‘const’ added become constants and cannot be altered by the program, but, in the way is has to be used to bodge in a substitute for one of the missing features of C++, it gets horridly complicated and frustratingly restrictive. The following attempts to explain how 'const' is used and why it exists.

Const gives you the ability to document your program more clearly and actually enforce that documentation. By enforcing your documentation, the const keyword provides guarantees to your users that allow you to make performance optimizations without the threat of damaging their data. For instance, const references allow you to specify that the data referred to won't be changed; this means that you can use const references as a simple and immediate way of improving performance for any function that currently takes objects by value without having to worry that your function might modify the data. Even if it does, the compiler will prevent the code from compiling and alert you to the problem. On the other hand, if you didn't use const references, you'd have no easy way to ensure that your data wasn't modified.

Such constants are useful for parameters which are used in the program but are do not need to be changed after the program is compiled. It has an advantage for programmers over the C preprocessor ‘#define’ command in that it is understood & used by the compiler itself, not just substituted into the program text by the preprocessor before reaching the main compiler, so error messages are much more helpful.

It also works with pointers but one has to be careful where ‘const’ to determine whether the pointer
or what it points to is constant or both. For example look statement below:
 
const int myconstant = 10       //An int which can't change value. Similar to #define in C but better.
const int * myconstant            //Variable pointer to a constant integer.
int const * myconstant            //Same as above.
int * const myconstant            //Constant pointer to a variable integer.
int const * const myconstant   //Constant pointer to a constant integer.

Creating Variabel

A variable in C++ is a name for a piece of memory that can be used to store information. You can think of a variable as a mailbox, or a cubbyhole, where we can put and retrieve information. All computers have memory, called RAM (random access memory), that is available for programs to use. When a variable is declared, a piece of that memory is set aside for that variable.

In order to declare a variable, we generally use a declaration statement. Here’s an example of declaring variable x as an integer variable (one that can hold integer values):

char name_unit [20];

In C++, variables are also known as l-values (pronounced ell-values). An l-value is a value that has an address (in memory). Since all variables have addresses, all variables are l-values. They were originally named l-values because they are the only values that can be on the left side of an assignment statement. When we do an assignment, the left hand side of the assignment operator must be an l-value. Consequently, a statement like 5 = 6; will cause a compile error, because 5 is not an l-value. The value of 5 has no memory, and thus nothing can be assigned to it. 5 means 5, and it’s value can not be reassigned. When an l-value has a value assigned to it, the current value is overwritten. The opposite of l-values are r-values. An r-value refers to any value that can be assigned to an l-value. r-values are always evaluated to produce a single value. Examples of r-values are single numbers (such as 5, which evaluates to 5), variables (such as x, which evaluates to whatever number was last assigned to it), or expressions (such as 2+x, which evaluates to the last value of x plus 2).

Here is an example of some assignment statements, showing how the r-values evaluate:
int y;      // declare y as an integer variable
y = 4;      // 4 evaluates to 4, which is then assigned to y
y = 2 + 5;  // 2 + 5 evaluates to 7, which is then assigned to y

int x;      // declare x as an integer variable
x = y;      // y evaluates to 7, which is then assigned to x.
x = x;      // x evaluates to 7, which is then assigned to x (useless!)
x = x + 1;  // x + 1 evaluates to 8, which is then assigned to x.

Typing a Comment

Typically, comments should be used for three things. At the library, program, or function level, comments should be used to describe what the library, program, or function, does. For example:

All of these comments give the reader a good idea of what the program is trying to accomplish without having to look at the actual code. The user (possibly someone else, or you if you’re trying to reuse code you’ve already written in the future) can tell at a glance whether the code is relevant to what he or she is trying to accomplish. This is particularly important when working as part of a team, where not everybody will be familiar with all of the code.

Second, within the library, program, or function described above, comments should be used to describe how the code is going to accomplish it’s goal.

/* To calculate the final grade, we sum all the weighted midterm and homework scores and then divide by the number of scores to assign a percentage.  This percentage is used to calculate a letter grade. */

Typically, comments should be used for three things. At the library, program, or function level, comments should be used to describe what the library, program, or function, does. For example:

// To generate a random item, we're going to do the following:
// 1) Put all of the items of the desired rarity on a list
// 2) Calculate a probability for each item based on level and weight factor
// 3) Choose a random number
// 4) Figure out which item that random number corresponds to
// 5) Return the appropriate item

These comments give the user an idea of how the code is going to accomplish it’s goal without going into too much detail. At the statement level, comments should be used to describe why the code is doing something. A bad statement comment explains what the code is doing. If you ever write code that is so complex that needs a comment to explain what a statement is doing, you probably need to rewrite your code, not comment it.

My first program

Now, we will create simple program and we can compile that. But we should know about structure program in C++.Let's started, becouse I can hold anymore.

First, although our programs will be written inside .cpp files, the .cpp files themselves will be added to a project. Some IDEs call projects “workspaces”, or “solutions”. The project stores the names of all the code files we want to compile, and also saves various IDE settings. Every time we reopen the project, it will restore the state of the IDE to where we left off. When we choose to compile our program, the project tells the compiler and linker which files to compile and link. It is worth noting that project files for one IDE will not work in another IDE.

Second, there are different kinds of projects. When you create a new project, you will have to pick a project type. All of the projects that we will create in this tutorial will be console projects. A console project means that we are going to create programs that can be run from the dos or linux command-line. By default, console applications have no graphical user interface (GUI) and are compiled into stand-alone executable files. This is perfect for learning C++, because it keeps the complexity to a minimum.

Traditionally, the first program programmers write in a new language is the infamous hello world program, and we aren’t going to deprive you of that experience! You’ll thank us later. Maybe.

A quick note about examples containing code

Starting with this lesson, you will see many examples of C++ code presented. Most of these examples will look something like this:

#include <iostream>

int main()

{
        using namespace std;
        cout << "Hello world!" << endl;
        return 0;

}


Now we have a simple program to display a text like "Hello World", but we have compile thats first. If you select the code from these examples with your mouse and then copy/past it into your compiler, you will also get the line numbers, which you will have to strip out manually. Instead, click the “copy to clipboard” link at the top of the example. This will copy the code to your clipboard without the line numbers, which you can then paste into your compiler without any editing required.



Recent Entries