Ye Olde General C++ Discussion/Advice Thread

Foxhound5366

Honorary Master
Joined
Oct 23, 2014
Messages
11,250
Reaction score
4,974
Location
Big Smoke
Rather optimistic after the success of my random number generator thread, I reckon it would be really useful to have a general C++ discussion thread :)

To start off with, if you're a C++ programmer, what do you use the language to create? This is just a hobby for me, but when I told the web-developers at my work that I was starting to teach myself C++, they laughed and said that it was an old-fashioned language that people only used to create advanced software like banking applications.

I've previously coded in Java, but I just didn't like it that much ... it was easy enough to use, but frankly I've found C++ exactly as easy to learn (at least the basics anyway). Apparently C# is meant to be even easier to learn, but it can teach you some bad habits you have to unlearn if you move across to C++?
 
Did some g++ & QT ages ago. Loved the power you have with pointers, and pointers to pointers, double dereferencing etc.
 
This is gonna be fun. I'm currently doing C# but started C++ training recently. Gonna keep an eye on this thread. :)
 
To start off with, if you're a C++ programmer, what do you use the language to create? This is just a hobby for me, but when I told the web-developers at my work that I was starting to teach myself C++, they laughed and said that it was an old-fashioned language that people only used to create advanced software like banking applications.

C and C++ have always been my language of choice for production code, with Python being my choice for quick scripts, prototypes, quant work and system "glue". The web developers at your work are right in that it's not the modern way to do web dev, but for many applications it is a good choice, and very widely used - I would go so far as to say that it is the primary language in use by Silicon Valley tech companies.

My biggest complaint with C++ is that with templating (especially the new variadic templates), stl and boost (in particular the template meta programming) it is possible to write the most unreadable code imaginable. Personally, I avoid boost, and avoid features that inhibit code readability (by this I mean that function is obscured, not merely aesthetics) - I have found this relatively easy to do, and in general hit people at the back of the head when they think that they have managed to write a clean interface, but get error messages that are 500 lines long when you misuse it.
 
Also use a lot of C and C++ at work, mostly for machine learning algorithms. Use Python for research and testing, and calling down to C libraries for heavy lifting. Personally I prefer C for portability, especially C89/90. Have had a lot of issues using other libraries and compiling them after each gcc update.
 
To start off with, if you're a C++ programmer, what do you use the language to create? This is just a hobby for me, but when I told the web-developers at my work that I was starting to teach myself C++, they laughed and said that it was an old-fashioned language that people only used to create advanced software like banking applications.
They would. Most web developers have a tendency to think their microcosm is the be all and end all of programming when it is really just a fraction of programming. The biggest code base including the systems that enable their web programming are written primarily in C/C++ so the joke is really on people like this that think themselves to be part of some elite.
 
Qt is a nice environment. A bit more work when you need to incorporate someone else's libraries.

I use Xcode and Visual Studio. I prefer Xcode for no real reason. Visual Studio's intellisence feature is the best. But I find it easier to write the code in Xcode so that it can be easily ported to other platforms. But that's probably just me. I also find adding 3rd party libraries easier with Xcode than visual studio. These are personal preferences though.

The nice thing about Qt is that it forces you to look at the linking process more than Xcode and VS. Which makes it a better learning tool.
 
Qt is a nice environment. A bit more work when you need to incorporate someone else's libraries.

I use Xcode and Visual Studio. I prefer Xcode for no real reason. Visual Studio's intellisence feature is the best. But I find it easier to write the code in Xcode so that it can be easily ported to other platforms. But that's probably just me. I also find adding 3rd party libraries easier with Xcode than visual studio. These are personal preferences though.

The nice thing about Qt is that it forces you to look at the linking process more than Xcode and VS. Which makes it a better learning tool.

Well said!
 
Rather optimistic after the success of my random number generator thread, I reckon it would be really useful to have a general C++ discussion thread :)

To start off with, if you're a C++ programmer, what do you use the language to create? This is just a hobby for me, but when I told the web-developers at my work that I was starting to teach myself C++, they laughed and said that it was an old-fashioned language that people only used to create advanced software like banking applications.

I've previously coded in Java, but I just didn't like it that much ... it was easy enough to use, but frankly I've found C++ exactly as easy to learn (at least the basics anyway). Apparently C# is meant to be even easier to learn, but it can teach you some bad habits you have to unlearn if you move across to C++?

It's bs that "a language can teach you bad habits". It's the rubbish tutorials and so called experts on the Internet that are mostly responsible for bad habits. To be fair to most tutorial authors, the purpose is it illustrate a point in as little space as possible. For example in a C++ game loop tut, you see lots of code in the main method or you see the main method instantiate an instance of Game class and the entire game is the Game class.
 
Wow, nice to see all the participation here!!! :)

I must admit I'm in a bit over my head, but that's always been the best way for me to learn.

I downloaded QT and promptly died ... will save that for a very rainy day, lol.

In the meantime, I DID manage to do a complete re-work of my lottery number generator, and after countless hours wasted (yes - hours - don't laugh) I finally got it to all work like I'd hoped:

Code:
#include <iostream>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <fstream>

using namespace std;

int main() 
{ // START OF MAIN PROGRAM

	cout << "WELCOME TO THE SUPER-HONEST LOTTERY GENERATOR" << endl;
	cout << "V2.0 February 2015" << endl;
	cout << endl;
	
	int lottoNums[6][1];
	int totalDraws = 1;
	string userRequest;

	cout << "How many lines of lottery numbers do you want? (enter in digits)" << endl;
	getline(cin, userRequest);
	int linesNeeded = atoi(userRequest.c_str());
	cout << endl;

	cout << "Here we go, for " << linesNeeded << " lines. Good luck!" << endl;
	cout << endl;

	srand(time(NULL));
	
	while (linesNeeded > 0) {  // MAIN WHILE LOOP. RUNS WHILE LINES ARE STILL NEEDED.

		int ballNumber = 1; // Tracks the ball number
		int drawNumberCounter; //Counts down through array after synchronising with number of draws
		int newBall = 0; //Holds the new random number
		bool noDuplicates = true;
		cout << "Lottery set " << totalDraws << endl;
		cout << endl;
	
		for (ballNumber; ballNumber < 7; ballNumber++)  { // START OF DRAWING SIX NUMBERS
			
			drawNumberCounter = ballNumber;
			newBall = (rand() % 48) + 1;

			while (drawNumberCounter >= 0) { //START OF UNIQUE NUMBER CHECKER

				
				if (lottoNums[(drawNumberCounter - 1)][totalDraws] != newBall) {
						noDuplicates = true;
						lottoNums[(ballNumber - 1)][totalDraws] = newBall;
						drawNumberCounter--;
				} else {
				cout << "CHECK: Number drawn is a duplicate" << endl;
				cout << "Duplicate number: " << newBall << endl;
				ballNumber--;
				noDuplicates = false;
				cout << endl;
				break;
				}
	
			} // END OF UNIQUE NUMBER CHECKER

			if (noDuplicates) { // ACTION ONCE UNIQUE NUMBER IS SAVED
				cout << "Lucky ball number " << ballNumber << "." << endl;
				cout << "Ball result: " << lottoNums[(ballNumber-1)][totalDraws] << endl;
				cout << endl;
				noDuplicates = true; //resets Duplicate status

			} // END OF ACTION ONCE UNIQUE NUMBER IS SAVED
			
		}  // END OF DRAWING SIX NUMBERS
		
		

		// FINAL ACTIONS IN WHILE LOOP, DECREASING LINES STILL NEEDED AND INCREASING TOTAL DRAW NUMBER
		linesNeeded--;
		totalDraws++;
		cout << endl;

		cout << "Your final lucky numbers for Line " << totalDraws-1 << " are: | ";
		for (int resetArray = 0; resetArray < 6; resetArray++)  { //Prints out and resets array after each draw
			cout << lottoNums[resetArray-1][totalDraws] << " | ";
			lottoNums[resetArray-1][totalDraws] = 0;
		}
		cout << endl;
		cout << endl;

	} //END OF MAIN WHILE LOOP

	cout << endl;
	
	cout << "Press Enter again to exit." << endl;
	getline(cin, userRequest);


	return 0;

} // END OF MAIN PROGRAM

Does anybody see anything above that's considered bad coding practise in C++? I'm ridiculously proud of having progressed to multidimensional arrays, and that little checker to throw out duplicate lottery numbers!

Biggest lesson learned: For loops like this one increment immediately (i.e. before the code inside even runs once), not only upon completion!
Code:
for (ballNumber; ballNumber < 7; ballNumber++)
 
Biggest lesson learned: For loops like this one increment immediately (i.e. before the code inside even runs once), not only upon completion!
Code:
for (ballNumber; ballNumber < 7; ballNumber++)
Nope they only increment after executing the body. If you think they increment before that there's some other quirk in your code.
 
Nope they only increment after executing the body. If you think they increment before that there's some other quirk in your code.

*shrugs* The codes all there for you to check Swa, dunno ... all I know is the code's working like expected now, with everything matched.

Code:
cout << lottoNums[resetArray-1][totalDraws] << " | ";
It only started doing that after I added -1 above, although I've initialised resetArray at 0 already. If it incremented AFTER running once, I wouldn't have had to do that.
 
*shrugs* The codes all there for you to check Swa, dunno ... all I know is the code's working like expected now, with everything matched.

Code:
cout << lottoNums[resetArray-1][totalDraws] << " | ";
It only started doing that after I added -1 above, although I've initialised resetArray at 0 already. If it incremented AFTER running once, I wouldn't have had to do that.
I'm not sure what you're saying. Did you actually test the value of ballNumber through printout or in a debugger? Because from what I can tell you set it to 1 and then it only increments to 2 after the first iteration.
 
I'm not sure what you're saying. Did you actually test the value of ballNumber through printout or in a debugger? Because from what I can tell you set it to 1 and then it only increments to 2 after the first iteration.

When I tried that without the '-1', it only printed out the values contained from the second part of the array (i.e. block 1, not block 0). That implies it had already been incremented once, despite being initialized to zero.

I don't know enough to use the debugger ... I've been running and re-running it.
 
When I tried that without the '-1', it only printed out the values contained from the second part of the array (i.e. block 1, not block 0). That implies it had already been incremented once, despite being initialized to zero.

I don't know enough to use the debugger ... I've been running and re-running it.
Ok, I think I know what might be happening. I was a bit confused as you quoted a different part of code than the one giving the problem. From the looks of it you are using the array out of bounds. An array variable references the first byte of allocated memory. The first index of the array (0) is the first variable of allocated memory. By referencing (-1) you are actually writing to unallocated memory before the allocated block.
 
A few problems in your code

Code:
lottoNums[6][1];
Here you create a multi-dimensional array of which one dimension is essentially redundant as it only contains a single element.

Code:
int ballNumber = 1; // Tracks the ball number
This creates the variable for use in the loop but it isn't used anywhere outside the loop so it would be more clear to declare it inside the loop like this
Code:
for (int ballNumber=1; ballNumber < 7; ballNumber++)

Code:
if (lottoNums[(drawNumberCounter - 1)][totalDraws] != newBall) {
lottoNums[(ballNumber - 1)][totalDraws] = newBall;
You are referencing the array created earlier through the variable totalDraws but your array dimension only has one element allocated for it. I'm not sure why you want a multi array though as you're not using it in a way that adds any practical value.
 
You are referencing the array created earlier through the variable totalDraws but your array dimension only has one element allocated for it. I'm not sure why you want a multi array though as you're not using it in a way that adds any practical value.
That all sounds very true, but at least it works the way I was hoping lol.

For my third program I'll go for something that works and is MEANT to work at least :D
 
How do I add the person's name inside this square? If I use name() it gives me numbers. Can those be converted to letters? Tx

Code:
#include <iostream>
#include <string>
using namespace std;
int main()

{
	cout << "Hello. What is your name?" << endl;
	string text;
	cin >> text;
	int width = text.size() + 6;
	int height = 5;
	//int length = text.size() + 6;

	for (int i = 0; i < height; ++i)
	{
		if (i == 0 || i == (height - 1))
		{
			for (int j = 0; j < width; ++j)
			{
				cout << '*';
			}
			cout << endl;
		}
		else
		{
			for (int j = 0; j < width; ++j)
			{
				if (j == 0 || j == (width - 1))
				{
					cout << '*';
				}
				else
				{
					cout << ' ';
				}

			}
			cout << endl;
		}
	}
}
 
Last edited:
It's not a cube. You need 6 equal faces. You will never anything inside this "cube". :)
 
Top
Sign up to the MyBroadband newsletter
X