Introducing myself - A beginner, beginning.

Pages: 12
Hi everyone.

I thought it polite to introduce myself, before making posts. I couldn't find an "introduce yourself" thread anywhere, so I guess here is a good place? If not, feel free to move this and / or tell me off. :)

I have have decided to get back into programming, just for fun, and C++ seems a good choice. I have some programming experience from way back (Z80 Assembly), so I am not new to programming concepts.

As I am learning C++ from the "ground up", my goal is not to just learn C++, but also good coding practices, Including code / memory efficiency and easy to read commenting.

My Immidiate goal is to write code that can be compiled on any system without modification ( Console programs ). I know this will limit what I can do, for this goal, but the idea is again, to get good programming prctices.

The 3 Systems all my code should compile on are :

1)Windows - ( MS Visual C++ )
2)Linux - (Gcc)
3)Android - (C4Droid) - I do know that this is not native for Android so cant write "Apps" with it.

So far, I have clocked up a whole 8 or 9 hours coding in C++ (in total, EVER) and seems to be going ok.

If anyone can think of any BEGINNING goals, I should add, I would love to hear them.

Thank You for your time, and this seemingly awesome community,
and I hope to, one day, be a useful member.
I would like to post my learning experiences, as I go. Would I do this here in the forums, or make a blog site and link to it, so I don't fill your forum with "Junk". I'm not sure of you'r policies to linking to a blog site (No adverts, or anything, purely personal site), so let me know.

Any code I post, that is my own, anybody can use for any reason. If any is not my own, I will give full, unreserved credit for.

Thank You, again
Many Regards,
Dave
Hi Dave!

I'm also new around here (and new to c++) but I'd like to say welcome anyway! Best of luck in your programming endeavors! Perhaps we can compare notes some time.
Hi,

Qt is a good system that can cross compile for the 3 system that you mentioned. Download the Qt SDK.

I would recommend running it from Linux.

Hope all goes well !!

Just remember to post code with code tags the <> button on the bottom for a new post, and on the right for a reply.

Also post your compiler output if you are having problems. If you do these things, then I, for one will be happy.

Finally the reference section on this site is a really good start, and wiki, and Google

digitalwarfare wrote:
I would like to post my learning experiences, as I go


Do this on the lounge page.
Last edited on
im new to, ive done a few of the beginner exercises...maybe when you done them we can compare codes :D

i made a tic tac toe game to begin with, it was good practice for the basics...

took about nine hours study and nine hours practice, then was ready.

it is a profound thing though, creating something with a language...what is moreunusual? a language that creates an abstract thing or a thing created by something completley abstract like a language?

you seem polite enough not to anger the dream in code guys as well, they kicked me off their site and they hate me (they very serious) but they put time into helping too

(though they like to give you 'bad rep' points, just dont complain, and dont tell the you know me because they will think your me and kick you, tried to rejoin on more than one comp, they 'just know' i guess)
Last edited on
Thanks Everyone, for the welcome.

Qt is a good system that can cross compile for the 3 system that you mentioned



I will look into QT immediately after i get home from work.

I would love to compare notes with anybody.

you seem polite enough not to anger the dream in code guys as well, they kicked me off their site and they hate me (they very serious) but they put time into helping too


I don't intend to upset anyone, but I may ask a dumb question now and again.

Learning C++ for me, seems to be as simple as learning the syntax, and observing good programming practice.

If I may, I will post my 2nd piece of code I wrote. This was done straight after my "hello world", after looking at many different references for a few hours and getting to grips with the syntax.

This is an old program that I wrote on my spectrum, about 20 years ago. I dug it out and Wrote the C++ version.

I would really appreciate some feedback on what I'v done and my methods. also, are my comments too little? Too much? The program compiles and runs just fine. (I know it's a really simple idea for a program)

Please remember, this is my 2nd ever program. I set a task, and created this solution.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Bubblesort.cpp : main project file.

/* This program will pick 6 random numbers, then sort them in to numerical order, Lowest to the left, highest to the right. It works by bubbling through the
	numbers and swapping them in pairs 
	Designed and coded by David  Around 3 hours from conception to final working code*/


					//Global Defines
#include <iostream>
#include <time.h>			//Uses Time.h so a call an be able to use the the time function to initialise a random seed
#include <stdlib.h>											

using namespace std;

int Numbers[6] = {0};			//Define a 6 item integer array, and fill them with 0
bool sort_needed = {true};		//Define a true/false as to weather a sort action has been done. This will tell the program weather a sort operation has been performed
int sort_count_cycles = {0};		//Define a variable to store the amount of sort cycles compleated 


int display_numbers ()			// A function that will display the numbers on the screen
	{
	cout << " Numbers: "<< Numbers[0]<<" , " << Numbers [1] << " , " << Numbers[2] << ", " << Numbers[3] << " , " << Numbers[4] << " , " << Numbers[5] << ". Sort Count:" << sort_count_cycles << "  Sort needed:" << sort_needed << "\n" ;

		return 0;
	}

//**********************************************************************************************************************************


int generate_random_number ()					// Function to load random numbers into array from 1 to 49
	{
	int loop_count = {0};					// initialise the loop counter	
	srand ( time(NULL));					// Initialise random number generator
	
	do 
		{
			Numbers[loop_count] = rand () % 49+1;	// Load Random Number into Array box
			++loop_count;				// Go to the next array box...
		}

	while (loop_count <= 5);				// Unless All boxes are filled

	return 0;
	}

//**********************************************************************************************************************************



int sort_numbers()			// Function to sort numbers
	
	{		
		int left_number, right_number = {0};	//Variables to store 2 numbers
		int number_pointer = {0};		//Which pair of numbers to sort
		int sort_temp = {0};			//Somewhere to temporarily store a number if swap is needed

		sort_needed = false;			//Set the sort needed to false (hasn't yet found the need to sort)
	
	do {
			left_number=Numbers[number_pointer];		//get number
			right_number=Numbers[number_pointer+1];		//and the number to the right of it

			if (left_number > right_number)		// Compare the left and right number.
			{					// If the left number is bigger than the right number...
				sort_needed = true;		// Set the sort_needed flag to true ( A sort is required )
				sort_temp = left_number;	// Swap the numbers over
				left_number = right_number;	// so that
				right_number = sort_temp;	// The lowest number is to the left of the bigger number

				Numbers[number_pointer] = left_number;	// Place the numbers back in the array
				Numbers[number_pointer+1]= right_number;// with the lowest number to the left of the right number
			}
			
		++number_pointer;				// move to the next pair of numbers
		}

		while ( number_pointer <= 4);			// Unless at the end of the pairs ( 5 pairs in total of 6 numbers)

		return 0;
	
	}

//**********************************************************************************************************************************


		
int main()				//Main Program
	{
	
	generate_random_number();	// Generate 6 Random numbers
	display_numbers();		// Display Random Numbers

	do 
		{
		++sort_count_cycles;	// make this the 1st sort cycle
		sort_numbers();		// See if a pair of numbers need sorting, and if they are sort them
		display_numbers();	// Display the numbers
		
		}
	while (sort_needed==true);	// But only sort, if rquired
	

	
	
    return 0;				//exit
	}

//**********************************************************************************************************************************





Last edited on
Ok, some comments about your code:

Try to avoid using namespace std; it brings heaps of stuff into the global namespace, polluting it, potential causing conflicts with other namespaces. It is easier on the compiler if you only refer to the specific things you need. There are several options:

1. put these statements instead of the using namespace std;

1
2
3
4
using std::cin;
using std:: cout;
using std::endl;
//same for any other std:: thing 


2. Leave out the using namespace std; and put std:: before each std thing throughout the code.

I do a mixture of those, option 1 for things that are used a lot like cin, cout etc. Option 2 for things that are not used much like std::find say.

Avoid using global variables, you could have put those variables in main and send to to functions that require them as arguments, or use pointers so you can change the value of several variables.

Declare your functions before main, and provide definitions for them after main. That way we don't have to scroll all the way to the bottom to see what main does.

There is a bit of rule that code should not exceed 80 chars per line. This is so we don't have tpo scroll sideways all the time. The main culprit or this is the cout statements, they don't have to be on one line - you can build them up in stages, like this:

1
2
3
cout << " Numbers: "<< Numbers[0]<<" , " << Numbers [1] << " , " << Numbers[2];
cout  << ", " << Numbers[3] << " , " << Numbers[4] << " , " << Numbers[5];
cout << ". Sort Count:" << sort_count_cycles << "  Sort needed:" << sort_needed << "\n" ;


You can also put comments over several lines, before the line that it refers to.

The other thing is indenting. I am not sure whether it is the way that this pages code formatter does things, or whether it is the users settings in their IDE. Most people use indenting of 4 spaces, I think the problem might arise where users have indenting with the tab length set to 4 as opposed to tabs being translated to 4 spaces.

With functions & variable names, I avoid the underscores because it makes the name too long.I also capitalise each word in the name like this :

GenerateRandomNumber

You can abbreviate a bit - a length of 10 chars is a reasonable goal, as long as it doesn't cause confusion:

GenRandNum

Larger programs, might have longer names, so that there is some sort of reasonable uniqueness about them.

Wth this:
int Numbers[6] = {0};

That is not quite the correct way to initialise an array.

int Numbers[6] = {0,0,0,0,0,0};

Try to avoid magic numbers like 6:
1
2
const unsigned SIZE = 6;
int Numbers[SIZE] = {0,0,0,0,0,0};


The variable SIZE can be used in a for loop condition.

With this:

int loop_count = {0};

The braces aren't needed, braces are only required for initialisation of arrays.

int LoopCount = 0;

Your display_numbers function could be written with a for loop:

1
2
3
4
5
6
7
8
cout << "Numbers ";

for (int Count = 0;Count < SIZE;Count++) {
cout << Numbers[Count] << " , "

}
cout << "\n. Sort Count:" << SortCountCycles << "  Sort needed:" << SortNeeded << endl ;


Now, I personally hate do loops - I only use one If I really have to, which is very rare. Do loops can almost always be rewritten as a while or for loop. With your generate_random_number function, this should be a for loop, because there is a numerical limit. When processing arrays, I always use for loops.

In main, I think this is a bad use of a do loop:

while (sort_needed==true); // But only sort, if rquired

Do loops always execute once, which is counter to the comment you have.

Hope all this helps, and has been instructive.

Happy to help with any other queries. Cheers.











Hi. Thank You.

This is the kind of advice I'm looking for.

I'm glad you feel it worth taking the time to look over what I am doing.
The idea of me coming here, is not for me to get someone to "do it for me" but point me in the right direction, so I can learn myself, which is exactly what you have done

Avoid using global variables, you could have put those variables in main and send to to functions that require them as arguments, or use pointers so you can change the value of several variables.


I did read somewhere, that global variables are quite bad, as they are potential memory leaks.

I am currently looking into pointers at the moment

The concept of pointers seems straightforward enough.
very similar to the Z80 command

1
2
3
4
// **This is not C++ Code!
ld hl,0010h
ld a,(HL)
halt


where it would load the register A with the value at memory address pointed to with the value of HL registers. in this case, whatever the value in memory address 0010h.

Anyhoo...
Ill let you know how I get on.

Regards, Dave
Last edited on
Hi.
Just a progress report onto learning about pointers.
The point of learning pointers now, (no pun intended) is to being able to avoid making global variables, whenever possible, as was advised by TheIdeasMan.

avoid using global variables, you could have put those variables in main and send to to functions that require them as arguments, or use pointers so you can change the value of several variables.


To learn pointers, I wrote a piece of what may seem stupid, and long code that does not a lot at all.
This is because I learn best by doing something, and getting an understanding of it, than just looking at a piece of code and fooling myself into thinking I'v understood it.

The code PURPOSEFULLY does not have any other functions, or loops. It does everything step by step, one thing at a time, and displays it, so I can see how it works.

Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
 /********************************************************************
** Pointerstest.cpp : main project file.                           **
** Testing the use of pointers to make sure I know how they work   **
**A dumb Program Really                                            **
*********************************************************************/
#include <iostream>


// ****************************************************************************
// Main Function
// ****************************************************************************


int main()
{
	int *pointer;
	int Array[3] = {11,22,33};	//define a 3 nunber array that is not 
					//global and load array with 3 different number
	pointer = &Array[0];		//Point the pointer at the first number

	//Display Condition of array and pointer
	std::cout << "\n" << Array[0]<<","<< Array[1] << "," << Array[2] << ", "; 
	std::cout << pointer <<"\n\n\n\n";	
	
	//Move the pointer up the array and display each location and contents
	//without referring to the array directly

	std::cout << "Address:" << pointer << " Contains:" << *pointer << "\n";
	++pointer;
	std::cout << "Address:" << pointer << " Contains:" << *pointer << "\n";
	++pointer;
	std::cout << "Address:" << pointer << " Contains:" << *pointer << "\n\n\n";

	//Now move the pointer back to the beginning of the array
	--pointer;
	--pointer;

	//Alter the array, using the pointer only

	*pointer = 20;
	++pointer;

	*pointer = 40;
	++pointer;

	*pointer = 60;

	//Again, return to beginning of the array
	--pointer;
	--pointer;
	
	//again move the pointer up the array and display each location and contents
	//without referring to the array directly, checking to see if the alterations
	//are correct

	std::cout << "Address:" << pointer << " Contains:" << *pointer << "\n";
	++pointer;
	std::cout << "Address:" << pointer << " Contains:" << *pointer << "\n";
	++pointer;
	std::cout << "Address:" << pointer << " Contains:" << *pointer << "\n\n\n";
	
	//Return pointer to beginning of array one last time

	--pointer;
	--pointer;

	//Display Condition of array and pointer by checking the array directly
	//To see if we get what we expect (20,40,60 and the beginning address of
	//The array
	std::cout << "\n" << Array[0]<<","<< Array[1] << "," << Array[2] << ", "; 
	std::cout << pointer <<"\n\n\n\n";	

	
	std::cin.get(); // Hold the Console window open DEBUG ONLY
	return 0;
}


From this, I can see how pointers are valuable. I can pass a whole array from one function to another, by just passing 3 or 4 arguments to the next function, and avoids making it a global array. I guess I could pass a very long array to another function, by passing over the pointer of the beginning of the array, the length of the array, and maybe even where i would like to start reading the array.

I'll start work on that now. Any comments welcome,please?
Last edited on
Any comments welcome,please?


With regards to pointers: prefer to use references. Code using pointers has more opportunity to land you in undefined behavior land.

Raw arrays are one place where using references are awkward. You should consider, when using arrays, whether it may be better to use a std::container type. (You may consider using std::array for stack-allocated arrays as yours is above if your compiler has C++11 support: http://www.cplusplus.com/reference/stl/array/ )
Damn, I missed 1 word in my reply, as cire pointed out references are much better.

Given your background with assembler, it may be beneficial to do some (not too much) C programming, then move to C++. I am not sure how much you know about C already. In any case this is still a good reference, IMO.

Here is a link to the C Programming by Kernighan & Ritchie, who invented C.

http://zanasi.chem.unisa.it/download/C.pdf


It may be worth your while to go through the first 4 or 5 chapters, quickly. Especially Chapter 5 which pointers and arrays - I think it is very worthwhile to have a knowledge of pointers regardless, because C++ uses them.

My background was in C ( I taught myself from that book in 1987) and I did a little assembler as well, plus a variety of other things too.

It took me awhile to figure out "The C++ ways of doing things", which is rather different.

Maybe you could try both ways to compare. That is, using an array versus a std::vector, vs a std::list, say.

Just one thing about your code:

pointer = &Array[0];

In C and C++, the array name is a pointer, so you could do this:

pointer = Array;

This is because I learn best by doing something, and getting an understanding of it, than just looking at a piece of code and fooling myself into thinking I'v understood it.


Excellent, this is quite refreshing compared to some (not all) of the suspected Y gen's I have been dealing with.

what? i do that too.
TheIdeasMan wrote:
some (not all)
yeah but i think i do actually
Sorry I'v not been reporting. Managed to get an eye infection. In the last 2 days I'v had off work, I'v taken up the personal challenge of writing a "TIC-TAC-TOE" game as my 3rd ever C++ project. my code is about 80% from the first 'Alpha' version. I'd like to compare notes with devonrevenge. Should I post my code here, or on another thread.

I'd also like to thank TheIdeasMan for putting me onto QT Creator. This is much easier to drive than the MS offing.

It seems to me that learning C++ is much easier than learning how to drive the IDE.

Regards, Dave.
Normally, one starts a new thread for each question, to avoid confusion. If the questions a very closely related, you can keep them in the same thread.

What I mean is, all things to do with the Tic-Tac-Toe program should be in the same thread, any questions about a different project, goes in another thread.

Qt is great isn't it? There are so many possibilities of what you can do with it. Are you running it under Windows or Linux or Mac?

I do recommend spending quite a bit of time reading the Qt documentation, if you are going to do GUI aps, otherwise stick to plain old console apps for now.

I recommend Linux as it has lots of quality stuff that is free. So much programming stuff - 10 lifetimes worth. 20,000 general apps - all free.
With the Tic-Tac-Toe program, lots of people have written that sort of program, some are good and some are bad.

I would advise to use a 2d array for the board, because you can easily use for loops to check rows, columns and diagonals, rather than differing combinations of numbers with a 1d array. I know there are others who disagree, but I would counter that the 2d solution is scalable to bigger boards, like in a "Connect4" game with a 8 by 8 board. All the code I have seen with the 1d solution seems to have long lists of if else's to cover all the number combinations.

Anyway that was my opinion, I look forward to seeing your code.
yeah let me dig i t up :) actualy got a newcomp since, ihave to email it to myself
@devonrevenge

What? This is what I was talking in my previous post about communication. We are not telepathic you know.
Qt is great isn't it? There are so many possibilities of what you can do with it. Are you running it under Windows or Linux or Mac?


Yes. Having much fun with it. Iv installed it on both Win and Lin. I use the Linux one remotely on my headless Linux Box via SSH and X11 Server, to keep all my stuff in one place.

All the code I have seen with the 1d solution seems to have long lists of if else's to cover all the number combinations.

I suspect my approach to the writing of the game is somewhat "Unique"

I haven't used a 2d array. Its just basically a summary of what I have learned so far. It also has been coded to be as "Modular" as possible, so I don't need to completely rewrite lots of code to fix a bug or add features.

When done, I will post my code in a new thread for you to pick apart and laugh at. :)

We are not telepathic you know.


My missus seems to expect me to be telepathic. But that's an Entirely different subject.. LOL
Last edited on
actually my code is proly more ridiculous, plus is the product of lots of help from these guys seeing i asked a question olmost at each stage of development,

have you discovered the bunny challenge yet?

also dont rush onto an api theres a bit more to go i think
Pages: 12