Pages

Categories

C++

C++
Published on 19 October, 2011
Well I’ve started college and we’re learning C++. So recently I’ve been playing around creating many toy applications to get to grip with the language. What I particularly enjoy about the language is that it’s so easy to compile code for on the go. Compared to when I made simple games in C# using XNA, I needed to have Visual Studio installed on the machine along with XNA framework and if I wasn’t making games, I needed the .NET framework to make C# applications. However with C++ I can carry code::blocks around on a memory stick and be able to compile a program on any computer.
I’ve only being toying with the standard library, and like a good boy I try to avoid limiting my programs to one platform (windows) by not including the windows.h header. I still don’t completely understand how the standard library files are laid out, and don’t know a fraction of the available members I can use, but I guess that only takes time.
For a bit of content in this post I give you a simple pyramid ascii maker program. Input a number and it outputs a pyramid of “*”.

The source code is very simple, just getting the number of lines as an input, then using string.append() to format the pyramid.
Selec All Code:
#include <iostream>

using namespace std;

int main()
{
int numberOfLines;
int numberOfSpacesMade;

string stringInject;

cout << “How many lines to create: “;
cin >> numberOfLines;

numberOfSpacesMade = numberOfLines;

for ( int i = 0; i < numberOfLines; i++ )
{
stringInject = “”;

stringInject.append( numberOfSpacesMade, ‘ ‘ );
stringInject.append( ( numberOfLines – numberOfSpacesMade + 1 ) * 2, ‘*’ );

cout << stringInject << endl;

numberOfSpacesMade–;
}

return 0;
}
Filed in Uncategorized | No replies