Displaying various C++ data types code sample

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info:

To do: Displaying various data types in C++ programming using console output, cout

To show: How to print various C++ data types using cout method in C++ programming

 

// using cout from iostream header file

#include <iostream>

using namespace std;

 

int main(void)

{

cout<<"Hello there.\n";

cout<<"Here is 7: "<<7<<"\n";

 

// other than escape sequence \n used for new line is endl

cout<<"\nThe manipulator endl writes a new line to the screen.\n"<<endl;

cout<<"Here is a very big number:\t" << 10000 << endl;

cout<<"Here is the sum of 10 and 5:\t" << (10+5) << endl;

 

// simple or auto type casting, from int to float

cout<<"Here's a fraction number:\t" << (float) 7/12 << endl;

 

// another type casting, from int to double

cout<<"And a very very big number:\t" << (double) 7000 * 7000<< endl;

cout<<"\nDon't forget to replace existing words with yours...\n";

cout<<"What a lame programmer!\n";

 

return 0;

}

 

Output example:

 

Hello there.

Here is 7: 7

The manipulator endl writes a new line to the screen.

Here is a very big number: 10000

Here is the sum of 10 and 5: 15

Here's a fraction number: 0.583333

And a very very big number: 4.9e+007

Don't forget to replace existing words with yours...

What a lame programmer!

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index