Home > Projects > Hello World



C++


Eclipse IDE for C/C++ Developers, Helios Service Release 2 with MinGW was used for the examples.


Below is an example of a simple C++ program.

See C++ Programming/Examples/Hello world for a detailed analysis of the syntax.


 
// "Hello, World!" program 
#include <iostream>

int main()
{
	std::cout << "Hello, World!\n";
	return (0);
}


Hello World Ascii Variation


Define two functions, with a for loop statement that repeatedly prints the "#" or "space" characters, until the specified number of repetitions evaluate to false. The parameter specifying the number of times to repeat the "#" or "space" is named a.

Two other functions are defined for when a '\n' is needed. These are named drawLastPound() and drawLastSpace()

#include <iostream>

void drawPound (int a)
{
	for(int i=1; i <= a; i++) {
		std::cout << "#";
	}

}

void drawSpace (int a)
{
	for(int i=1; i <= a; i++) {
	    std::cout << " ";
    }
}

void drawLastPound ()
{
	std::cout << "#\n";
}

void drawLastSpace ()
{
	std::cout << " \n";
}

int main()
{
	drawPound(1); drawSpace(3); drawPound(1); drawSpace(1); drawPound(5); drawSpace(1); drawPound(1); drawSpace(5); drawPound(1); drawSpace(6); drawPound(3); drawSpace(5); drawPound(1); drawSpace(7);
	drawPound(1); drawSpace(2); drawPound(3); drawSpace(2); drawPound(4); drawSpace(2); drawPound(1); drawSpace(5); drawPound(4); drawSpace(3); drawLastPound(); drawPound(1); drawSpace(3); drawPound(1);
	drawSpace(1); drawPound(1); drawSpace(5); drawPound(1); drawSpace(5); drawPound(1); drawSpace(5); drawPound(1); drawSpace(3); drawPound(1); drawSpace(4); drawPound(1); drawSpace(3); drawPound(1);
	drawSpace(3); drawPound(1); drawSpace(1); drawPound(1); drawSpace(3); drawPound(1); drawSpace(1); drawPound(1); drawSpace(3); drawPound(1); drawSpace(1); drawPound(1); drawSpace(5); drawPound(1);
	drawSpace(3); drawPound(1); drawSpace(2); drawLastPound(); drawPound(5); drawSpace(1); drawPound(5); drawSpace(1); drawPound(1); drawSpace(5); drawPound(1); drawSpace(5); drawPound(1); drawSpace(3);
	drawPound(1); drawSpace(4); drawPound(1); drawSpace(3); drawPound(1); drawSpace(3); drawPound(1); drawSpace(1); drawPound(1); drawSpace(3); drawPound(1); drawSpace(1); drawPound(4); drawSpace(2);
	drawPound(1); drawSpace(5); drawPound(1); drawSpace(3); drawPound(1); drawSpace(2); drawLastPound(); drawPound(1); drawSpace(3); drawPound(1); drawSpace(1); drawPound(1); drawSpace(5);
	drawPound(1); drawSpace(5); drawPound(1); drawSpace(5); drawPound(1); drawSpace(3); drawPound(1); drawSpace(5); drawPound(1); drawSpace(2); drawPound(1); drawSpace(2); drawPound(1); drawSpace(2);
	drawPound(1); drawSpace(3); drawPound(1); drawSpace(1); drawPound(1); drawSpace(2); drawPound(1); drawSpace(2); drawPound(1); drawSpace(5); drawPound(1); drawSpace(3); drawPound(1); drawSpace(2);
	drawLastSpace(); drawPound(1); drawSpace(3); drawPound(1); drawSpace(1); drawPound(5); drawSpace(1); drawPound(5); drawSpace(1); drawPound(5); drawSpace(2); drawPound(3); drawSpace(2);
	drawPound(1); drawSpace(4); drawPound(1); drawSpace(2); drawPound(1); drawSpace(5); drawPound(3); drawSpace(2); drawPound(1); drawSpace(3); drawPound(1); drawSpace(1); drawPound(5); drawSpace(1);
	drawPound(4); drawSpace(3); drawLastPound(); drawSpace(29); drawPound(1); drawSpace(38); drawLastSpace();
	return (0);
}

Here is the output from the above C++ program:


C:\HelloWorldAscii.exe


#   # ##### #     #      ###     #       #  ###  ####  #     ####   #

#   # #     #     #     #   #    #   #   # #   # #   # #     #   #  #

##### ##### #     #     #   #    #   #   # #   # ####  #     #   #  #

#   # #     #     #     #   #     #  #  #  #   # #  #  #     #   #   

#   # ##### ##### #####  ###  #    #  #     ###  #   # ##### ####   #

                             #