Saturday, July 12, 2008

C++ Programming Puzzle
some source code for a class to manage planets in an astronomy program. A planet is created with co-ordinates (assume those are correct- the values are not the problem) and a name. So I've added Earth. This code works BTW, it prints out Earth. But it's flawed. How many bugs can you spot?
#include #include using namespace std;class Planet { private: float x,y,z; char *PlanetName; public: Planet(float X, float Y, float Z, char * NewName ) ; void SetPlanetName( char * NewName ) ; char * GetPlanetName() {return PlanetName;}; };Planet::Planet(float X,float Y, float Z, char * NewName ): x(X),y(Y),z(Z) { SetPlanetName( NewName ) ; }void Planet::SetPlanetName( char * NewName ) { PlanetName = new char[strlen(NewName) + 1]; strcpy(PlanetName, NewName) ; }int main(int argc, char* argv[]) { Planet Earth(10789.0f,102345.4f,10234.23f,"Earth") ; cout << Earth.GetPlanetName() << endl; return 0; }
Answer on Wednesday.



Grace - Writing C++ In a more Modern Way
Sunday July 6, 2008
I can't see C++ purists really taking to this but those who have been learning other languages first then C++ might consider it. Grace is an attempt to hide some of the uglier parts of C++ programming and make it look a bit more readable. this snippet is from one of the examples.
int main (void) { value greetings = $("world", "Hello") -> $("everyone", "Hi") -> $("you", "Hey") ; foreach (g, greetings) { fout.writeln ("%s, %s." %format (g, g.id())) ; } return 0; }
As well as the friendlier syntax it includes code for reading XML, CSV, INI files, JSON and is used in the OpenPanel project for ISPs. It's an interesting idea and worthy of a look. As usual I've added it to the C++ code library.
PROGRAamming Challenge 15 Posted

Yes this one is definitely a bit early; it doesn't start until August but I wanted to make sure there weren't any gotchas or things I've overlooked. If I've missed any crucial detail please let me know!
The Challenge is to write a program that generates a set of dungeons like you would use in role playing games like Dungeons & Dragons. 30 Years ago I used to run D&D games. Then nearly 20 years ago I actually wrote a dungeon generator as part of a large postal game (called Quest). Mine also had treasures, traps, monsters and non player characters but this challenge is much simpler. Just generate a nine level dungeon with interconnecting rooms and corridors.

No comments: