Ye Olde General C++ Discussion/Advice Thread

Learned coding using Pascal and C.

The last real application I worked on was an embedded system using Watcom C about 7 years ago which was fun.

I code in C# these days and get very grumpy when I see youngsters who have never touched a managed language write leaky code in C#.

Very grumpy.
 
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

How about this? I hacked it a little, but it kinda works (a growing trend here lolol):

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 = 3;
	//int length = text.size() + 6;
	bool runOnce = false;


	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 if (!runOnce)
				{
					
				cout << "  " << text << "  ";
				runOnce = true;
				}

			}
			cout << endl;
		}
	}
}
 
I don't know what OS/compiler you're using to make your apps with? But these days you can get Visual Studio Community Edition for free, which pretty much includes everything in the suite. With it you can build apps in C/C# etc.

To debug (in Visual Studio at least) check your menu bar at the top, just click on Debug->Step into/over. It will start running your program a step at a time. It's a lot easier to debug programs interactively than it is by running the whole thing and seeing what happens.

Switching to basic C# from basic C/C++ is pretty easy. A lot of the concepts like loops etc. work exactly the same.


I haven't used C in ages, but I'd change your program to roughly this:

Code:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

string CreateStars(int width)
{
	string s = "";
	for (int i = 0; i < width; i++)
	{
		s.push_back('*');
	}
	s.push_back('\n');

	return(s);
}

int main()
{
	string text;
	int height = 3;

	cout << "Hello. What is your name?" << endl;
	cin >> text;

	string stars = CreateStars(text.size() + 6);

	for (int i = 0; i < height; i++)
	{
		if (i == 0 || i == (height - 1))
		{
			cout << stars;

		} else
		{
			cout << "*  " << text << "  *" << endl;
		}
	}
	
	return (0);
}


- Stay away from --i if you can use i++, at least until you're 100% sure you know what each is doing
- You didn't need to make a method to replace the for loop to make the long stars line (like I did), but it doesn't hurt either. Learning to break up code into nice little methods is a good skill to learn early on
- I replaced the 2nd for loop that creates the middle name line with a single line of cout. You didn't need to add unneccesary complexity with another for loop

Keep at it!
 
Last edited:
Yes, I don't know about Visual Studio but most IDEs (Integrated development environments) you also have to add variables to monitor them.
 
A good practice in C++ is also never do anything in main. Create a class in seperate .hpp and .cpp files and put your code in it. In main, only create an instance of the class and invoke the method todo whatever you need to do. And don't put the logic in the class constructor either. The class constructor is there to, well, construct. I.e. Initialise stuff in the class.

Get used to doing this now.

example MyCube.hpp:
Code:
#ifndef MYCUBE_HPP
#define MYCUBE_HPP
#include <iostream>
#include <string>

class MyCube
{
public:
	MyCube(void);
	~MyCube(void);

	void print();

private:
	std::string CreateStars(int width);
};

#endif

MyCube.cpp
Code:
#include "MyCube.hpp"


MyCube::MyCube(void)
{
}


MyCube::~MyCube(void)
{
}

void MyCube::print()
{
	std::string text;
	int height = 3;

	std::cout << "Hello. What is your name?" << std::endl;
	std::cin >> text;

	std::string stars = CreateStars(text.size() + 6);

	for (int i = 0; i < height; i++)
	{
		if (i == 0 || i == (height - 1))
		{
			std::cout << stars;

		} else
		{
			std::cout << "*  " << text << "  *" << std::endl;
		}
	}

	
}

std::string MyCube::CreateStars(int width)
{
	std::string s = "";
	for (int i = 0; i < width; i++)
	{
		s.push_back('*');
	}
	s.push_back('\n');

	return(s);
}

main.cpp
Code:
#include "MyCube.hpp"

int main()
{
	MyCube* cube = new MyCube();
	cube->print();


}

This main.cpp also works. Real fun learning the difference :)

Code:
#include "MyCube.hpp"

int main()
{
	MyCube cube;
	cube.print();


}
 
Last edited:
A good practice in C++ is also never do anything in main. Create a class in seperate .hpp and .cpp files and put your code in it. In main, only create an instance of the class and invoke the method todo whatever you need to do. And don't put the logic in the class constructor either. The class constructor is there to, well, construct. I.e. Initialise stuff in the class.
I don't agree. That is forced compartmentalisation which is also a bad practice. You don't create a function to execute just one line of code once and likewise the same for classes.
 
I don't agree. That is forced compartmentalisation which is also a bad practice. You don't create a function to execute just one line of code once and likewise the same for classes.

"forced compartmentalisation" -- Thanks for bringing the term to light, I am most definitely guilty of this when I try to implement modular code and some OOP principles in a bit of an unnatural/forced manner. In fact a thread to discuss good coding technique my be apt.
 
I don't agree. That is forced compartmentalisation which is also a bad practice. You don't create a function to execute just one line of code once and likewise the same for classes.

Thats not 1 line of code.

I'm illustrating a concept. The purpose of the thread is to learn techniques etc.

If you are only going to use main, then there really is no point in coding anything C++. use C or even a scripting language.

If you arent going to need a Class, dont waste your time with C++

edit: another advantage of this approach is that while learning, you put all you stuff in other modules and just keep changing main to call whatever you want.
 
Last edited:
Thats not 1 line of code.

I'm illustrating a concept. The purpose of the thread is to learn techniques etc.

If you are only going to use main, then there really is no point in coding anything C++. use C or even a scripting language.

If you arent going to need a Class, dont waste your time with C++

edit: another advantage of this approach is that while learning, you put all you stuff in other modules and just keep changing main to call whatever you want.
That isn't what you said. You said to never put anything in main which would result in putting single lines of code in their own function or class. It's not just single lines of code. If something is only going to be used once there's no reason to put it into a function or class even if there's multiple lines of code.
 
That isn't what you said. You said to never put anything in main which would result in putting single lines of code in their own function or class. It's not just single lines of code. If something is only going to be used once there's no reason to put it into a function or class even if there's multiple lines of code.

I forget with you, discussions always degenerate into this. Never mind.
 
That isn't what you said. You said to never put anything in main which would result in putting single lines of code in their own function or class. It's not just single lines of code. If something is only going to be used once there's no reason to put it into a function or class even if there's multiple lines of code.

Functionally speaking it doesn't matter, but the standard use of main is for use as a loop, since most programs do more than simple arithmetic with no inputs or outputs. If you are planning to program professionally, you should learn about the main loop, and interrupt methods such as events and callbacks.
 
I forget with you, discussions always degenerate into this. Never mind.
Degenerate into what?

Functionally speaking it doesn't matter, but the standard use of main is for use as a loop, since most programs do more than simple arithmetic with no inputs or outputs. If you are planning to program professionally, you should learn about the main loop, and interrupt methods such as events and callbacks.
What do you mean? Main is simply as entry point into your program. It's a function that's automatically called for you. How you use it is up to you.
 
What do you mean? Main is simply as entry point into your program. It's a function that's automatically called for you. How you use it is up to you.

Some people do implement their own main thread, but then you cannot use managed code such as COM classes and ATL (which is important if you are writing a proprietary library) or Quartz. Linux and OpenFrameworks and the like are more flexible.
 
Some people do implement their own main thread, but then you cannot use managed code such as COM classes and ATL (which is important if you are writing a proprietary library) or Quartz. Linux and OpenFrameworks and the like are more flexible.

Indeed. Thats exactly what winmain does.
 
A good practice in C++ is also never do anything in main. Create a class in seperate .hpp and .cpp files and put your code in it. In main, only create an instance of the class and invoke the method todo whatever you need to do. And don't put the logic in the class constructor either. The class constructor is there to, well, construct. I.e. Initialise stuff in the class.

Get used to doing this now.

example MyCube.hpp:
Code:
#ifndef MYCUBE_HPP
#define MYCUBE_HPP
#include <iostream>
#include <string>

class MyCube
{
public:
	MyCube(void);
	~MyCube(void);

	void print();

private:
	std::string CreateStars(int width);
};

#endif

MyCube.cpp
Code:
#include "MyCube.hpp"


MyCube::MyCube(void)
{
}


MyCube::~MyCube(void)
{
}

void MyCube::print()
{
	std::string text;
	int height = 3;

	std::cout << "Hello. What is your name?" << std::endl;
	std::cin >> text;

	std::string stars = CreateStars(text.size() + 6);

	for (int i = 0; i < height; i++)
	{
		if (i == 0 || i == (height - 1))
		{
			std::cout << stars;

		} else
		{
			std::cout << "*  " << text << "  *" << std::endl;
		}
	}

	
}

std::string MyCube::CreateStars(int width)
{
	std::string s = "";
	for (int i = 0; i < width; i++)
	{
		s.push_back('*');
	}
	s.push_back('\n');

	return(s);
}

main.cpp
Code:
#include "MyCube.hpp"

int main()
{
	MyCube* cube = new MyCube();
	cube->print();


}

This main.cpp also works. Real fun learning the difference :)

Code:
#include "MyCube.hpp"

int main()
{
	MyCube cube;
	cube.print();


}

Do you like creating memory leaks?
 
Functionally speaking it doesn't matter, but the standard use of main is for use as a loop, since most programs do more than simple arithmetic with no inputs or outputs. If you are planning to program professionally, you should learn about the main loop, and interrupt methods such as events and callbacks.

Holey moley Jans, when did you get around to be a programming guru on the side??? Very impressed.

And thanks for that detailed explanation Zippy. Just two quick questions for you:
1: Just so that I can be sure to use the file types you displayed accurately, what is the difference between the code in a .hpp and .cpp file?
2: Can you please look at my first real C++ program, and advise if there's anything I could have done differently to make it more efficient? I'm proud of actually using a for loop to write and read from an array, but I think I'm hacking it a little bit (especially my use of 'break;' to get it to stop at one point).
 
Holey moley Jans, when did you get around to be a programming guru on the side??? Very impressed.

And thanks for that detailed explanation Zippy. Just two quick questions for you:
1: Just so that I can be sure to use the file types you displayed accurately, what is the difference between the code in a .hpp and .cpp file?
2: Can you please look at my first real C++ program, and advise if there's anything I could have done differently to make it more efficient? I'm proud of actually using a for loop to write and read from an array, but I think I'm hacking it a little bit (especially my use of 'break;' to get it to stop at one point).

hpp or h doesnt really matter.

h was for C and hpp for C++ but I dont think anyone cares. For me its habit really. Just be consistent.
 
Holey moley Jans, when did you get around to be a programming guru on the side??? Very impressed.

And thanks for that detailed explanation Zippy. Just two quick questions for you:
1: Just so that I can be sure to use the file types you displayed accurately, what is the difference between the code in a .hpp and .cpp file?
2: Can you please look at my first real C++ program, and advise if there's anything I could have done differently to make it more efficient? I'm proud of actually using a for loop to write and read from an array, but I think I'm hacking it a little bit (especially my use of 'break;' to get it to stop at one point).
.hpp contains the headers for the functions and classes you use. In C++ you can declare a function or classes without implementing them. The .cpp files then contain your actual code and can be compiled separately. The linker will then put everything together. This can save a lot of time in not having to recompile code that hasn't changed.
 
2: Can you please look at my first real C++ program, and advise if there's anything I could have done differently to make it more efficient? I'm proud of actually using a for loop to write and read from an array, but I think I'm hacking it a little bit (especially my use of 'break;' to get it to stop at one point).

backstreetboys example is fine.

There are many ways of doing it. One alternative is to use vector of a vector. But for what you are doing its overkill, but it might be good way of learning some of the more advanced things in the standard template library (STL). std::vector

look at http://www.codeguru.com/cpp/cpp/cpp...ial-A-Beginners-Guide-to-stdvector-Part-1.htm

It has many advantages over arrays. Arrays are more C than C++ :)
 
Top
Sign up to the MyBroadband newsletter
X