mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster

To match your very little effort at asking the question..
my answer is: std::sort (<algorithm> header).

mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster

I know how to fix your code, but to let you do it on your own, I will say this: the recursive formula (the second one) is used to compute each _term_ of the first equation one after the other. In your implementation, you use "result" in the recursive formula, not the "term", you need to use the recursive formula to compute the terms and separately add them to get the result.

empror9 commented: thanks +1
mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster

Consider something like this:

#include "Cores.h" //I assume this would contain all GeneralCore, GraphicsCore.. but I would suggest you have one file per core, in fact one header per class in general.

namespace my_engine {

class Engine
{
   GeneralCore mMyGeneralCore;
   GraphicsCore mMyGraphicsCore;
   GameCore mMyGameCore;
   LogicCore mMyLogicCore;
};

};

// in GeneralCore.hpp
namespace my_engine {

class GeneralCore {
  ... whatever ...
};

};

// so on for the others cores.

With proper implementation of all the cores and the Engine class, all you would have to do to start the game would be:

#include "Engine.hpp"

using namespace my_engine;

int main() {
  Engine MyGame(.. some parameters like which game logic to start ..);

  MyGame.run(); // or something like that.
};

I have always done something like that for my past game engines.

In response to:
>>Is there a way to create one class out of many different classes?
Well it's a bit vague. Consider this discussion. That gives a few guidelines in general with respect to multiple inheritance (I would not recommend that, but the discussion there also discusses all the alternatives which are probably better suited for your application). You should rely a lot on polymorphism, abstraction and encapsulation. Make sure to understand those topics before making too many structural decisions about your engine.

mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster

In the constructor, you shouldn't have "int boar..", repeating the type (int) will declare a new variable called "board" that is local to the constructor. so you are initializing a local array board and never initializing the data member "board". So that is why the private data member "board" remains with garbage values.

This is allowed in the c++0x standard (add flag "-std=c++0x" to gcc to compile):

ScoreBoard::ScoreBoard() : board({-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0}) { };

Otherwise you have to initialize them one by one, i.e. with a loop from 0 to 14 to set to -1 and finally setting the last element to 0.

Ancient Dragon commented: well said. +31
mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster

B=N*I*R*R/2[R*R+x+x]^3/2;

Is not a valid C/C++ syntax. the ^ operator is not for power (it's binary XOR which I doubt is what you intended). C/C++ is not matlab or mathematica or a hand calculator.

If the function is, as I understand it: B = (N * I * R * R) / (2 * (R^2 + x^2)^(3/2))
Then in C++ it is:

B = N * I * R * R / (2.0 * pow(R*R + x*x, 3.0 / 2.0));

BTW: the value R is the radius of the coil, not the resistance, and x should be declared as a float. Oh and PI = 3.14159.. not 3.14.

mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster

Please wrap code in tags next time..

Why do you need to get the numbers a string first? Just use an array of int for "names" and get them from cin directly as integers, just like you do with "input".

so change the type of "names" to "int names[5];" and it will work.

Also, the size of your array is 5 not 500 (the value to send to search).

mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster

Look at this.

When you are about to write the value of No_atoms (which I guess is a dummy value at first), call tellp() before writing the dummy value and save the location value it returns. Afterwards, when you have a real value of No_atoms, use seekp() to get back to that position in the file and write No_atoms again. One thing to check for is that the length is the same, because it will overwrite characters of the original dummy value, so format the ofstream such that both the dummy value and the actual final value take up the same amount of characters (writing it in binary form would be also easier for that, but then you have to write the whole file in binary, which might not be a bad idea if the file is as long as you say it is).

mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster

Read this through and you will understand it all.

emps commented: Very good link! +1
mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster

I remember, as I was looking into making an ABI for my projects, that there is very loose standards about how a compiler is allowed to lay out the variables of different scopes. From what I recall, public data members are required to be ordered in memory as they are declared in the class definition, but private members are not, the compiler can choose to arrange them differently. That is in fact one of the major reasons why the Pimpl or Cheeshire-Cat Idiom is often called a "compiler firewall", because it makes the private section have only one data member, i.e. the pimpl pointer, and thus, all compilers will produce the same binary foot-print for an object of that class (given that other conditions are satisfied too!).

So it appears that in the new up-coming standard they may have added some guidelines to make it possible to have all-private data members in a POD. But still not to break code that has mixed public-private data whose binary foot-print is compiler-specific (and even compiling options specific), it has to remain non-POD.

I'm not sure this all makes sense, or is really true, but this is my impression of it.

mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster

Ok, relax. First of all, you may think that the "_db* db" pointer that is initialized in main.cpp is the same as the "static _db* db;" declared in main.h. IT IS NOT! How scoping works is that the compiler tries to find the first variable named "db" in the inner-most scope first, so it will find a pointer "db" declared in the main() function and initialize that one, not the global one in "main.h". So when you get to readConfig() function, you are addressing a garbage point "db" that has not been initialized.

Simple solution: replace "_db *db = new _db;" with "db = new _db;" and it should work fine.

Another issue, make sure both cpp are compiled in the same translation unit. Otherwise you will have two different global "db" variables... and things will get messy. Remember one of my earlier posts where I suggest a static variable wrapped in a global function implemented in a unique cpp file. I said that was very robust and that I use it for many reasons.. you are starting to experience those reasons, by not following my suggested solution.

mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster

Simple Google search gives this.

mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster

I think in code it makes more sense, here are all the cases I can think of:
Notice cases B, C, E and F, they are all valid classes that cannot be instantiated.

#include <iostream>

class simplePOD {
  public:
    int valueInt;
    double valueDouble;
};

class notPOD {
  public:
    simplePOD& someRef;
};

class privCtorClass {
  private:
    int someValue;

    privCtorClass() : someValue(42) { };

};

class privCtorClassWithStatic {
  private:
    int someValue;

    privCtorClassWithStatic() : someValue(42) { };
  public:
    static privCtorClassWithStatic* Create() { 
      return new privCtorClassWithStatic(); 
    };
};

class abstractBase {
  public:
    virtual void somePureVirtualMethod() = 0;
};

class abstractDerived : public abstractBase {
  public:
    virtual void anotherMethod() { 
      std::cout << "Hello!" << std::endl; 
    };
};

/* This will not compile with error class 'useForwardDeclared' is incomplete because of member 'someObject' or incomplete type 'forwardDeclared'
class forwardDeclared;

class useForwardDeclared {
  public:
    forwardDeclared someObject;
};

//No real declaration of forwardDeclared.
*/

int main() {
  simplePOD* A = new simplePOD(); //This works fine.

  notPOD* B = new notPOD(); //This does NOT work.
  
  privCtorClass* C = new privCtorClass(); //This does NOT work.

  privCtorClassWithStatic* D = privCtorClassWithStatic::Create(); //This works fine.

  abstractBase* E = new abstractBase(); //This does NOT work.

  abstractDerived* F = new abstractDerived(); //This does NOT work.
};
mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster

I'm not sure there is syntaxically correct way provided by C++ (or C legacy stuff), but here is one slightly dirty way to get it done:

memmove(variable + 6, "YouMary", 7 * sizeof(char));
mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster

Yeah, it's relevant! Dev-C++ is outdated and produces erroneous code on windows version Vista and newer. Use code::blocks instead.

I copied your code and compiled it on linux and I never got errors even with all the original issues that I though _might_ cause the error. So.. yeah it's relevant that you used Dev-C++!

mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster

I'm not sure I understand what you want to accomplish exactly, but consider first using a shell file (.sh in Unix/Linux or .bat in win console). I'm not sure how you should write it because I don't understand the goal, but I'm pretty sure that will be enough. If you want to write a command from a program, call "system(command_string);" that's all (at least for Linux/Unix).

mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster

Use a mutex to protect the access to vector. Mutexes are your best friends in multi-threading. A mutex (short for "mutual exclusion") is a thing that is supported by all multi-threading-capable operating systems (the API functions to use may change depending on your OS). What it does is that it allows one thread to lock the mutex while it's accessing memory in vector (read or write or both, doesn't matter). While the mutex is locked to one thread, it cannot be locked by another until it is released (or unlocked) by the thread that owns the lock. So if a thread wants to access the vector while another thread has a lock on it, it will be blocked (suspended execution) until the mutex is released and then it will lock it for itself and do its own operations on the vector. In code, using boost::thread library (which I recommend for that purpose, but others are similar):

vector<int> v; //say you have a global vector v of integers.
boost::mutex v_mutex; //create a global mutex for it.

int getValue_ThreadSafe(int Index) {
  //lock the mutex at first, if already locked, this will suspend execution until it's available.
  boost::unique_lock< boost::mutex > lock_access_here(v_mutex);
  //return the value and "lock_access_here" variable will go out of scope and release the lock on the mutex.
  return v[Index];
};

void push_back_ThreadSafe(int NewValue) {
  //Again same procedure..
  boost::unique_lock< boost::mutex > lock_access_here(v_mutex);
  //push_back, return and thus, release mutex lock.
  v.push_back(NewValue);
};

Other mutex implementation beside boost::thread exist of course, but this …

mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster

Well, I have more than 12 years of experience in OOP programming and I started as a teen with 3D game programming (now doing more multi-body dynamics simulation, robotics control software and artificial intelligence). I jumped right into OpenGL from the start and painfully rammed through it all. I recommend being a little more cautious but still 3D gaming is exciting and it's hard to go slowly once you get a few things running. One link I highly recommend to learn openGL is http://nehe.gamedev.net (it is where I learned and all my friends in the gaming industry too, awesome tutorial, simple, clear and detailed).

How much C++ do you need? Well, you can start programming games from knowing nothing or you can start after having a Computer Science degree (which really won't make that big a difference to be honest). That I know of, there is only one way to learn, that is practice, practice, practice... It seems you understand that you can't just read books and expect to be a good programmer (as you say you practice a few hours every day). Personally, I have had many little projects one after the other, making a game engine, then a physics engine, then a better game engine, then a 3D model editor, then a particle system, then a better physics engine, etc. etc. I think now I have iterated to my 5th or 6th completely new physics engine (or now multi-body dynamics simulator) and about the same …

mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster

1. Initially the constructor has its own allocated memory (which sizeof(string*) + 2*sizeof(int), for arr (pointer to string) and numAllocated and numUsed (two integers)), then the constructor itself allocates memory for two string objects (size = 2*sizeof(string)). I cannot tell you how big each of there are because that is OS and PC specific, so for your environment, you can simply check it but writing a simple program which calculates the above two sizes, if you really want to know. But knowing the exact size is useless since you really cannot use it because the standard does not guarantee it in any way.

2. For the null-character stuff, this is internal to the class "string" and thus I have no idea and in 12 years of programming I have frankly never cared about it. This is what is called "abstraction" in C++.

3. The strings are stored in memory one after the other, but each "string" object holds some pointer to the actual character sequence (probably null-terminated, or not, again I don't know and I don't care, and so should you, because it is meant to be abstracted away from whoever uses class "string"). So to answer the question, to access a character of a string you do arr[1][2], for the third character of the second string in the array.

The above implementation is called a dynamic array because it allocated memory by a factor of 2 every time more space is needed, this highly reduces the …

mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster

I think this thread addresses the issue very well:

http://bytes.com/topic/c/answers/774079-end-stream-std-cin

Simply put, the std::cin will not reach end-of-stream until the user presses ctrl-D or kills the app. From the last example on the previous post, it is pretty clear why it doesn't make sense for eos to be reached unless the app is about to die, because the user can always enter characters until that point or until he detaches the terminal from the app.

Agni commented: thanks !! +3
mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster

This site was not mentioned yet, but I like it a lot for all the small details you might overlook while coding but that often are hugely important:

http://www.parashift.com/c++-faq-lite/

It nicely written and cover a plethora of issues from very simple to very complex.

Lusiphur commented: Thanks :) Added to bookmarks +1
mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster

@Agin: I think using generic algorithms goes against the purpose of the exercise..

First, a simple trick is to use a magic number like 0 or MAX_INTEGER or something to mark the already matched values as invalid, then check that a number is valid before looping for matches.
Second, the second loop (match search) only needs to start from i+1 because all previous values have already been looked at as a.

//set some magic number.
#define INVALID_VALUE -43425234 

{..}

for(int i=0;i<max;i++) {
  if(a[i] == INVALID_VALUE)
    continue;
  count=1;
  for(int j=i+1;j<max;j++) {
    if(a[i]== a[j]) {
      count++;// count houw many inegers are the same
      a[j] = INVALID_VALUE;
    }
  }
  cout<<a[i]<< " = "<<count<<endl;
  count= 0;
}

{..}