Wednesday, September 4, 2013

HOW TO INPUT AND OUTPUT IN C++

Posted by Unknown On 8:55 AM No comments

In this tutorial we will look at how to input and output using the iostream libraryin which we will get input from keyboard(reading) and also printing on the screen (outputting)

Data Handling

Basic Data Types

C++ supports a large number of data types. The built in or basic data types supported by C++ are integer, floating point and character. These are summarized in table along with description and memory requirement
TypeByteRangeDescription
int2-32768 to +32767Small whole number
long int4-2147483648 to +2147483647Large whole number
float43.4x10-38 to 3.4x10+38Small real number
double81.7x10-308 to 1.7x10+308Large real number
long double103.4x10-4932 to 3.4x10+4932Very Large real number
char10 to 255A Single Character

Variables

It is a location in the computer memory which can store data and is given a symbolic name for easy reference. The variables can be used to hold different values at different times during the execution of a program.
To understand more clearly we should study the following statements:
Total = 20.00; In this statement a value 20.00 has been stored in a memory location Total.
Declaration of a variable
Before a variable is used in a program, we must declare it. This activity enables the compiler to make available the appropriate type of location in the memory.
float Total;
You can declare more than one variable of same type in a single single statement
int x,y;
Initialization of variable
When we declare a variable it's default value is undetermined. We can declare a variable with some initial value.
int a = 20;

Input/Output (I/O)

C++ supports input/output statements which can be used to feed new data into the computer or obtain output on an output device such as: VDU, printer etc. The following C++ stream objects can be used for the input/output purpose.
cin console input
cout console output
cout is used in conjuction with << operator, known as insertion or put to operator.
cin is used in conjuction with >> operator, known as extraction or get from operator.
cout << “My first computer";Once the above statement is carried out by the computer, the message "My first computer" will appear on the screen.
cin can be used to input a value entered by the user from the keyboard. However, the get from operator>> is also required to get the typed value from cin and store it in the memory location.
Let us consider the following program segment:
int marks; 
cin >> marks; 
In the above segment, the user has defined a variable marks of integer type in the first statement and in the second statement he is trying to read a value from the keyboard.

Standard output(cout)

by using the incertion operator (<<) we can print a message on the screen
for an example 
#include<iostream>
int main()
{
cout<<" hello universe";
return 0;
}

standard input(cin)

In most cases the standard input device is the keyboard. With the cin and >> operators it is possible to read input from the keyboard.eg
#include<iostream>
int main()
{
int i;// declaring an integer
cout<<" enter an integer";// outputing it on the screen
cin>>i;// reading the integer
cout<<"the entered integer is "<<i;
return 0;
}
// is only a statement it is not included in the program

Variables



If you declare a variable in C++ (later on we will talk about how to do this), you ask the operating system for

a piece of memory. You (can) give this piece of memory a name and you can store something in that piece of memory (for later use).


The name of a variable is called an identifier. You can give a variable any name you want, as long as it is a valid identifier.

Valid identifier



A valid identifier is a sequence of one or more letters, digits or underscores and the identifier must begin with a letter. (It is not possible to start an identifier with a number.) It is also not possible to use punctuation marks, symbols or spaces in an identifier. Compiler specific keywords or external identifiers usually begin with an underscore. (It possible to use an underscore at the beginning of your identifier, but it is not recommended).



The C++ language is a “case sensitive” language. This means that an identifier with capital letters is not the same as with normal letters.

For example: myvar, Myvar, MyVar and MYVAR are four different variable identifiers.

The C++ language also uses a set of names that are reserved. It is not allowed to use these keywords as variable identifiers (variable names). The standard reserved keywords are:
asm, auto, bool, break, case, catch, class, char, const
const_cast, continue, default, delete, double, do, dynamic_cast
enum, else, explicit, extern, export, float, false, for, friend, goto
int, if, inline, long, mutable, namespace, new, operator, private
protected, public, register, reinterpret_cast, return, short, switch
signed, sizeof, static, static_cast, struct, template, this, throw
true, try, typedef, typeid, typename, union, unsigned, using, void
virtual, volatile, while, wchar_t
Data types
There are three types of variables: numeric-integer, numeric-real and character.
  • Numeric variables can either be of the type integer (int) or of the type real (float). Integer (int) values are whole numbers (like 10 or -10.) Real (float) values can have a decimal point in them. (Like 1.25 or
    -1.25.)
  • Character variables are letters of the alphabet, ASCII characters or numbers (0-9). If you declare a character variable you must always put the character between single quotes (like so ‘a’). So remember a character between single quotes is not the same as a character with single quotes.
The basic fundamental data types in the C++ language are (Note: size* are in bytes):

Constants

A number which does not change its value during execution of a program is known as a constant. Any attempt to change the value of a constant will result in an error message. A constant in C++ can be of any of the basic data types, const qualifier can be used to declare constant as shown below:
const float pi = 3.1415;
The above declaration means that Pi is a constant of float types having a value 3.1415.
Examples of valid constant declarations are:
const int rate = 50;
const float pi = 3.1415;
const char ch = 'A';

0 comments:

Post a Comment