c++ - very basic programme for me to take apart

Joined
12 Feb 2006
Posts
17,639
Location
Surrey
been learning c++ now for ages from a book but finding it hard to read it because im not geting to use the code to do anything.

Im looking for a very very begginers programme that i can look at the code to take it apart so i can see how it works.

thanks
 
Unless you have previous coding experience I would advise you too write a lil program yourself from start, as looking at other peoples code can be intimidating to start with.
 
addy_010 said:
been learning c++ now for ages from a book but finding it hard to read it because im not geting to use the code to do anything.

Im looking for a very very begginers programme that i can look at the code to take it apart so i can see how it works.

thanks

Code:
#include <iostream>

int main()
{
   std::cout << "Hello World!" << std::endl;
}
 
eriedor said:
Unless you have previous coding experience I would advise you too write a lil program yourself from start, as looking at other peoples code can be intimidating to start with.
Kew-eff-tee.

The absolute best way to learn a programming language is by actually using it to writing your own programs and experimenting with it. I can't think of any small programs you could write atm, but I'm sure you can come up with some ideas :p
 
You could write a program that takes in an integer from user input and then displays an x*x box of asterix's dependant on the entered integer. This was one of the first things I wrote and gets you used to input/output and loops.

If you fancy a go at it post any problems you have and ill help you. I have the complete solution if youd rather that though but id recommend trying it for yourself.
 
Hi, I have built upon my program to play mastermind from the other day and will post the updated version for you here. Feel free to compile it and play to gain an understanding of the code. A lot of the basics are covered within it.

Code:
#include "stdafx.h"
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>

using namespace std;

int* newGame(int gameDifficulty)
{
	srand ( time(NULL) );

	int* gameBoard = new int[gameDifficulty];
	for (int i = 0; i < gameDifficulty; i++)
	{
		gameBoard[i] = (rand() % 10);
	}
	return gameBoard;
}

void printArray(int* arrayToPrint, int gameDifficulty)
{
	cout << "The solution is : ";
	for ( int i = 0; i < gameDifficulty; i++ )
	{   
		cout << *(arrayToPrint+i) << " ";
	}
	cout << endl;
}


int main(int argc, char* argv[])
{
	int quit = 0;

	while(quit != 1)
	{
		char playAgain = NULL;
		char answer = NULL;
		int correctPlaces = 0;
		int wrongPlaces = 0;
		int won = 0;
		int gameDifficulty = 0;

		
		cout << "What difficulty would you like to play at? (0-5)" << endl;
		cin >> gameDifficulty;

		if (gameDifficulty > 5)
		{
			cout << "Difficulty selected was too high! 5 selected!" << endl;
			gameDifficulty = 5;
		}

		gameDifficulty += 4;

		cout << endl;
		int* gameNumbers = newGame(gameDifficulty);
		int* enteredNumbers = new int[gameDifficulty];

		while(answer != 'y' && answer != 'n')
		{
			cout << "Would you like to see the solution? (y/n)" << endl;
			cin >> answer;

			switch (answer)
			{
				case 'y' : printArray(gameNumbers,gameDifficulty); break;
				case 'n' : break;
				default : cout << "Please enter y or n!" << endl; break;
			}
		}

		while (won != 1)
		{
			cout << "Guess " << gameDifficulty << " numbers pressing enter after each entry" << endl;
			
			for ( int k = 0; k < gameDifficulty; k++)
			{
				cin >> enteredNumbers[k];
			}

			for (int i = 0; i < gameDifficulty; i++)
			{
				if (enteredNumbers[i] == gameNumbers[i])
				{
					correctPlaces += 1;
				}

				for (int j = 0; j < gameDifficulty; j++)
				{
					if (enteredNumbers[j] == gameNumbers[i] && enteredNumbers[i] != gameNumbers[i] && enteredNumbers[j] != gameNumbers[j])
					{
						wrongPlaces += 1;
						break;
					}
				}
			}

			cout << "C = " << correctPlaces << "  W = " << wrongPlaces << endl;

			if (correctPlaces == gameDifficulty)
			{
				won = 1;
			}

			correctPlaces = 0;
			wrongPlaces = 0;
		}

		cout << "Congratulations you have won!" << endl;
		cout << endl << endl << endl;
		delete [] gameNumbers;
		delete [] enteredNumbers;
		
		while(playAgain != 'n' && playAgain != 'y')
		{
			cout << "Would you like to play again? (y/n)" << endl;
			cin >> playAgain;
			cout << endl;

			switch (playAgain)
			{
				case 'n' : quit = 1; break;
				case 'y' : break;
				default : cout << "Please enter y or n!" << endl << endl;
			}
		}
	}

	return 0;
}
 
Work on making a simple Guess the Number game. It covers basic conditional statements, using the pseudo-random number generator and simple input/output.

Could then do something using functions along with some Maths... Just build your way up, adding a little bit each time.
 
Phyre said:
Work on making a simple Guess the Number game. It covers basic conditional statements, using the pseudo-random number generator and simple input/output.

Could then do something using functions along with some Maths... Just build your way up, adding a little bit each time.
Yup, this is the way to do it. The 'guess the number' game is good as an intro to programming as well. :)
 
Last edited:
I need to learn C++ at some point, but doing small programs like that seems pointless since I already know C, and they don't really use the extra features provided by C++.
 
Psyk said:
I need to learn C++ at some point, but doing small programs like that seems pointless since I already know C, and they don't really use the extra features provided by C++.
You'd be better off with a non-beginner's book then really. Could try re-writing some of your C programs in C++ with the extra features.
 
Psyk said:
I need to learn C++ at some point, but doing small programs like that seems pointless since I already know C, and they don't really use the extra features provided by C++.

What I do when learning a new language now is just rewrite all my old programs using it and just find some reference material. Especially if your going from C to C++ and know an OO language as well. You might not even need a book really something like http://www.cppreference.com/ might be enough.
 
Last edited:
The problem is none of my old programs are big enough to really benefit from the OO features of C++.

Maybe when I have time, I'll write a small game in C++ since that way I could learn a bit of DirectX too.
 
Back
Top Bottom