November 03, 2009

Learn C++ (Console Application) - Part 2 -

Input and Output in C++

C language have some routines for I/O (Input/Output) which is declared with header stdio.h. This version is still usable at C++. But anyway, C++ introduce a new technique for I/O process, which is named iostreams or streams. Almost all function on stdio.h could be converted into streams. The pros about streams compare to I/O on C is their flexibility for formatting I/O and their ability to support overloaded operator concept.

What is Streams ?

Streams is abstraction to handle data flow from one source (producer) to another destination (consumer). Either source or destination could be act as buffer in memory, string, file, or monitor screen. C++ declare class for stream in some header files, i.e :
[*] iostream.h (general I/O)
[*] constrea.h (console I/O)
[*] strstrea.h (string I/O)
[*] fstream.h (file I/O)

Stream for Output
To do output process, operator << is used, with general syntax stream_object << data.

stream_object is one of the object from stream class.
data is your output.

#include

void main()
{
cout << "Learn C++";
}

Above statement will write string "Learn C++" to cout's stream. Cout is a stream for output to your monitor screen. You could also use << in leveled, i.e :

#include

void main()
{
cout << "Learn" << " C++" << 'n';
}

Identifier which had scalar data type could be written to output with stream. ex :

#include

void main()
{
int x=10;
float y=20.0;
cout << "x = " << x << " y = " << y;
}

output for above program : x = 10 y = 20

output stream could also write identity address, ex :

#include

void main()
{
int x;
cout << &x; //write variable x's address
}

output for above program : 0x8f98fff4

Format Output with Function
We can format stream output just like output with printf function. To do that, you need to change some bit flag on ios class. There are different kinds of bit flag on ios class, like :

Flag Bit Statement
skipws 0 : ignore white space while input process
left 1 : left justify
right 2 : right justify
internal 3 : implied output with padding character after positive or base sign
dec 4 : show output in decimal base
oct 5 : show output in octal base
hex 6 : show output in hexadecimal base
showbase 7 : show output base
showpoint 8 : show point (.) on real output
uppercase 9 : capitalize hexadecimal output
showpos 10 : show (+) if the output is more than zero
scientific 11 : exponential mode for real output (format : XXXE()xxx)
fixed 12 : decimal format for real output
unitbuf 13 : empty all stream
stdio 14 : prevent problem occurred if using stream and stdio together

Flash about Flag
Flag is bits, where every bit epitomize certain condition. Simplest example is flag 1 bit only epitomize condition true ("1") or false ("0").

Another Format Output
The most applied function for formatting, i.e :
[*] int width (int) : change output width, ex :

C :
#include
void main()
{
int x=10;
printf("x = %4d", x);
}

C++ :
#include
void main()
{
int x=10;
cout.width(4);
cout << "x = " << x;
}

output for above programs :
x = 10

[*] char fill (char) : to change padding character. padding character is one of ASCII character which will be written if format output is bigger than stated width. ex :

C :
#include
void main()
{
int x=10;
printf("x = %04d", x);
}

C++ :
#include
void main()
{
int x=10;
cout.fill('0');
cout.width(4);
cout << "x = x << x;
}

output for above programs :
x = 0010

You can change 0 fill with another character, such as X, so the output would be XX10.

[*] int precision (int) : adjust precision degree of real output (floating point). ex :

C :
#include
void main()
{
float y=123.45;
printf("y = %10.2f\n", y);
}

versi C++ :
#include
void main()
{
float y=123.45;
cout.width(10);
cout.precision(5);
cout << "y = " << y;
}

output for above programs :
y = 123.45

to be continued..

November 02, 2009

Learn C++ (Console Application) - Part 1 -

How to Start:

1. open your application.
2. select file, new, under Projects tab choose Win32 Console Application
3. insert your project's name and where you want to save it in right pane
4. choose "An empty projet" radio button.
5. click finish (click OK for the next dialog box that pops out).

now you will see your development environment.

6. find Project on top menu bar, select Add to Project - New
7. under Files tab, select C++ Source File
8. type in file name and press OK
9. and now you are ready to typing your c++ console application