Labdabeta 182 Posting Pro in Training Featured Poster

I figured it out! I made the mistake of creating my code from pseudo-code. When I initialize best I should set it's score to -infinity, not to 0.

Labdabeta 182 Posting Pro in Training Featured Poster

Ok, I made some modifications and a few minor fixes (I have no clue why I was returning true from main?) Now it works but only with very few empty spaces, once the move is not obvious it goes back to returning null...

new TicTacToeTest.cpp:

#include <iostream>
#include "PathFindingAI.h"
class TicTacToeState: public IGameState
{
    private:
    unsigned char board[9];
    public:
    TicTacToeState(){board={' ',' ',' ',' ',' ',' ',' ',' ',' '};}
    TicTacToeState(char b[10])
    {
        for (int i=0; i<9; i++)
            board[i]=b[i];
    }
    virtual TicTacToeState &execute(GameMove m)
    {
        if (m.size()!=2||(m[0]!='X'&&m[0]!='O')||m[1]>8)
            return *this;
        board[m[1]]=m[0];
        return *this;
    }
    virtual TicTacToeState &undo(GameMove m)
    {
        if (board[m[1]]!=m[0])
            return *this;
        board[m[1]]=' ';
        return *this;
    }
    void output()
    {
        cout<<board[0]<<"|"<<board[1]<<"|"<<board[2]<<endl;
        cout<<"|||||"<<endl;
        cout<<board[3]<<"|"<<board[4]<<"|"<<board[5]<<endl;
        cout<<"|||||"<<endl;
        cout<<board[6]<<"|"<<board[7]<<"|"<<board[8]<<endl<<endl;
    }
    virtual unsigned char *data(){return board;}
    virtual bool gameOver()
    {
        unsigned char tiars[8][3]={ {board[0],board[1],board[2]},
                                    {board[3],board[4],board[5]},
                                    {board[6],board[7],board[8]},
                                    {board[0],board[4],board[8]},
                                    {board[2],board[4],board[6]},
                                    {board[0],board[3],board[6]},
                                    {board[1],board[4],board[7]},
                                    {board[2],board[5],board[8]}};
        for (int i=0; i<8; i++)
        {
            if (tiars[i][0]==tiars[i][1]&&tiars[i][1]==tiars[i][2]&&tiars[i][2]!=' ')
                return true;
        }
        for (int i=0; i<9; i++)
        {
            if (board[i]==' ')
                return false;
        }
        return true;
    }
};
class TicTacToePlayer: public IGamePlayer
{
    private:
    bool isX;
    public:
    TicTacToePlayer(bool isX):isX(isX){};
    virtual int evaluate(IGameState &s)
    {
        unsigned char tiars[8][3]={ {s.data()[0],s.data()[1],s.data()[2]},
                                    {s.data()[3],s.data()[4],s.data()[5]},
                                    {s.data()[6],s.data()[7],s.data()[8]},
                                    {s.data()[0],s.data()[4],s.data()[8]},
                                    {s.data()[2],s.data()[4],s.data()[6]},
                                    {s.data()[0],s.data()[3],s.data()[6]},
                                    {s.data()[1],s.data()[4],s.data()[7]},
                                    {s.data()[2],s.data()[5],s.data()[8]}};
        int ret=0;
        for (int i=0; i<8; i++)
        {
            //1st check for win
            if (tiars[i][0]==tiars[i][1]&&tiars[i][1]==tiars[i][2])
            {
                if (isX)
                    return (tiars[i][0]=='X'?INT_MAX:-INT_MAX);
                else
                    return (tiars[i][0]=='X'?-INT_MAX:INT_MAX);
            }//then return [# of 3-in-a-rows left for me]-[# of 3-in-a-rows left for you]
            else
            {
                if (tiars[i][0]==' '||tiars[i][1]==' '||tiars[i][2]==' ')
                {
                    if (!(tiars[i][0]=='X'||tiars[i][1]=='X'||tiars[i][2]=='X'))//O can get this row!
                        (isX?ret--:ret++);
                    if (!(tiars[i][0]=='O'||tiars[i][1]=='O'||tiars[i][2]=='O'))//X can get this row!
                        (isX?ret++:ret--);
                } …
Labdabeta 182 Posting Pro in Training Featured Poster

That shouldn't matter since the escape condition is if (ply==0||current.gameOver()) so it will only ever loop until the end of the game. I think the problem is that -tmp.score is never greater than 0 in my outer (first-non-recursive) call of alphabeta. I just cannot figure out a way to debug that without having to go through all of the recursive function calls.

Labdabeta 182 Posting Pro in Training Featured Poster

I have tried that and I have tried saving the return value into a local variable before accessing any members. I have even tried converting the code to Java (which has a simpler inheritance structure) but no matter what AlphaBeta returns 'real' values up until the final 'unwind' at which point it returns null. I cannot figure this out!

Labdabeta 182 Posting Pro in Training Featured Poster

This doesnt make sense. I have debugged this program countless times, as far as I can tell the value of best is always legit until the function returns! What is wrong?!

Labdabeta 182 Posting Pro in Training Featured Poster

Thank you!

Labdabeta 182 Posting Pro in Training Featured Poster

From what I understand of your problem, which in my opinion is poorly worded, a possible solution may be to rotate the points one degree at a time and then use some kind of detection algorithm. I fear that perhaps a learning AI may be necessary.

Labdabeta 182 Posting Pro in Training Featured Poster

Here is my TicTacToeTest.cpp:

#include <iostream>
#include "PathFindingAI.h"
class TicTacToeState: public IGameState
{
    private:
    unsigned char board[9];
    public:
    TicTacToeState(){board={' ',' ',' ',' ',' ',' ',' ',' ',' '};}
    virtual TicTacToeState &execute(GameMove m)
    {
        if (m.size()!=2||(m[0]!='X'&&m[0]!='O')||m[1]>8)
            return *this;
        board[m[1]]=m[0];
        return *this;
    }
    virtual TicTacToeState &undo(GameMove m)
    {
        if (board[m[1]]!=m[0])
            return *this;
        board[m[1]]=' ';
        return *this;
    }
    void output()
    {
        cout<<board[0]<<"|"<<board[1]<<"|"<<board[2]<<endl;
        cout<<"|||||"<<endl;
        cout<<board[3]<<"|"<<board[4]<<"|"<<board[5]<<endl;
        cout<<"|||||"<<endl;
        cout<<board[6]<<"|"<<board[7]<<"|"<<board[8]<<endl<<endl;
    }
    virtual unsigned char *data(){return board;}
    virtual bool gameOver()
    {
        unsigned char tiars[8][3]={ {board[0],board[1],board[2]},
                                    {board[3],board[4],board[5]},
                                    {board[6],board[7],board[8]},
                                    {board[0],board[4],board[8]},
                                    {board[2],board[4],board[6]},
                                    {board[0],board[3],board[6]},
                                    {board[1],board[4],board[7]},
                                    {board[2],board[5],board[8]}};
        for (int i=0; i<8; i++)
        {
            if (tiars[i][0]==tiars[i][1]&&tiars[i][1]==tiars[i][2]&&tiars[i][2]!=' ')
                return true;
        }
        for (int i=0; i<8; i++)
        {
            if (board[i]==' ')
                return false;
        }
        return true;
    }
};
class TicTacToePlayer: public IGamePlayer
{
    private:
    bool isX;
    public:
    TicTacToePlayer(bool isX):isX(isX){};
    virtual int evaluate(IGameState &s)
    {
        unsigned char tiars[8][3]={ {s.data()[0],s.data()[1],s.data()[2]},
                                    {s.data()[3],s.data()[4],s.data()[5]},
                                    {s.data()[6],s.data()[7],s.data()[8]},
                                    {s.data()[0],s.data()[4],s.data()[8]},
                                    {s.data()[2],s.data()[4],s.data()[6]},
                                    {s.data()[0],s.data()[3],s.data()[6]},
                                    {s.data()[1],s.data()[4],s.data()[7]},
                                    {s.data()[2],s.data()[5],s.data()[8]}};
        int ret=0;
        for (int i=0; i<8; i++)
        {
            //1st check for win
            if (tiars[i][0]==tiars[i][1]&&tiars[i][1]==tiars[i][2])
            {
                if (isX)
                    return (tiars[i][0]=='X'?INT_MAX:-INT_MAX);
                else
                    return (tiars[i][0]=='X'?-INT_MAX:INT_MAX);
            }//then return [# of 3-in-a-rows left for me]-[# of 3-in-a-rows left for you]
            else
            {
                if (tiars[i][0]==' '||tiars[i][1]==' '||tiars[i][2]==' ')
                {
                    if (!(tiars[i][0]=='X'||tiars[i][1]=='X'||tiars[i][2]=='X'))//O can get this row!
                        (isX?ret--:ret++);
                    if (!(tiars[i][0]=='O'||tiars[i][1]=='O'||tiars[i][2]=='O'))//X can get this row!
                        (isX?ret++:ret--);
                }
            }
        }
        return ret;
    }
    virtual vector<GameMove> getMoves(IGameState &s)
    {
        vector<GameMove> ret;
        for (int i=0; i<9; i++)
        {
            if (s.data()[i]==' ')
            {
                GameMove m;
                m.push_back(isX?'X':'O');
                m.push_back(i);
                ret.push_back(m);
            }
        }
        return ret;
    }
};
int main()
{
    TicTacToeState board;
    TicTacToePlayer x(true),o(false);
    bool isX=true;
    while (!board.gameOver())
    {
        board.output();
        GameMove gm=AlphaBeta(board,(isX?x:o),(isX?o:x),9).move;
        board.execute(gm);
    }
    return true; …
Labdabeta 182 Posting Pro in Training Featured Poster

hit the 'x' just under the scroll bar for the source code viewing area. You can also find this 'x' by looking at the message you got and moving to the top-right of its box. I do not suggest getting rid of this necessarily as it is relatively important information.

Labdabeta 182 Posting Pro in Training Featured Poster

It probably said that std::vector<string> has no function push_back(int) because on line 6 you say v.push_back(1), but v is a string vector, not an int vector.

Labdabeta 182 Posting Pro in Training Featured Poster

I have a difficult function to write and I have no idea where to begin. Here is the definition of what I need to write:
"A function f(x,y) which returns true if the binary number formed by concatenating the bits of x, 8*y-1 zeroes and a one is prime, false otherwise. x is an array of bytes (unsigned char) and y is an unsigned integer comprised of two bytes (unsigned short)."
EG:
f(10,1234)=isPrime(00001010...0x9871...1)
I am just completely lost as to what to do. I think there should be a better way than actually creating a class that can store massive numbers and than doing the traditional primality check of:

bool isPrime(myLargeIntegerMimic a)
{
    for (myLargeIntegerMimic i=2; i*i<a; i++)
    {
        if (a%i==0)
            return false;
    }
    return true;
}

Since myLargeIntegerMimic would have to store a massive array of 'y' bytes and then would have to successfully do multiplication and modulation on it, this would be extremely slow. I am hoping that I am missing some kind of short-cut.

Labdabeta 182 Posting Pro in Training Featured Poster

I have this code for AlphaBeta implementation and have created versions of the required interfaces that work perfectly (I have debugged them quite thoroughly). Unfortunately my algorithm keeps returning an empty vector and score of 0. Here is the code:

#include <vector>
#include <limits.h>
using namespace std;
typedef vector<unsigned char> GameMove;
struct MoveScore
{
    GameMove move;
    int score;
};
class IGameState
{
    public:
    virtual IGameState &execute(GameMove)=0;//execute the move
    virtual IGameState &undo(GameMove)=0;//undo the move
    virtual unsigned char *data()=0;
    virtual bool gameOver()=0;
};
class IGamePlayer
{
    public:
    virtual int evaluate(IGameState&)=0;//evaluate the move
    virtual vector<GameMove> getMoves(IGameState&)=0;
};
MoveScore AlphaBeta(IGameState &current, IGamePlayer &us, IGamePlayer &them, int ply, int low=-INT_MAX, int high=INT_MAX)
{
    //RND r;
    MoveScore best={GameMove(),0};
    if (ply==0||current.gameOver())
    {
        best.score=us.evaluate(current);
        return best;
    }
    vector<GameMove> moves=us.getMoves(current);
//    for (int i=0; i<moves.size(); i++)
//    {
//        int swap=r.RAND<int>()%moves.size();
//        GameMove tmp=moves[i];
//        moves[i]=moves[swap];
//        moves[swap]=tmp;
//    }
    for (int i=0; i<moves.size(); i++)
    {
        current.execute(moves[i]);
        MoveScore tmp=AlphaBeta(current,them,us,ply-1,-high,-low);
        current.undo(moves[i]);
        if ((-tmp.score)>best.score)
        {
            low=-tmp.score;
            best.move=moves[i];
            best.score=low;
        }
        if (low>=high)
        {
            return best;
        }
    }
    return best;
}

Can anybody see where I went wrong?

Labdabeta 182 Posting Pro in Training Featured Poster

While this thread is somewhat alive again, I have always wondered if it would be possible to use __asm to use assembly language to emulate getch(). INT 21 with AH = 07 seems to pop out at me. I just have major trouble with my assembler and I do not fully understand assembly, but I don't see why it wouldn't be possible.

Labdabeta 182 Posting Pro in Training Featured Poster

I figured it out. Facepalm kind of situation. AlphaBeta is a template function. I never specified the ply!

Labdabeta 182 Posting Pro in Training Featured Poster

Here is the specific code:
PathFindingAI.h:

#include <vector>
#include <limits.h>
using namespace std;
struct GameMove
{
    unsigned char *data;
    int l;
};
struct MoveScore
{
    GameMove move;
    int score;
};
class GameState
{
    public:
    virtual GameState &execute(GameMove)=0;//execute the move
    virtual GameState &undo(GameMove)=0;//undo the move
    virtual unsigned char *data()=0;
};
class GamePlayer
{
    public:
    virtual int evaluate(GameState&)=0;//evaluate the move
    virtual vector<GameMove> getMoves(GameState&)=0;
};
template <unsigned int ply>
MoveScore AlphaBeta(GameState &current, GamePlayer &us, GamePlayer &them, int low=INT_MIN, int high=INT_MAX)
{
    MoveScore best={{NULL,0},0};
    if (ply==0||us.getMoves(current).size()==0)
    {
        best.score=us.evaluate(current);
        return best;
    }
    vector<GameMove> moves=us.getMoves(current);
    for (int i=0; i<moves.size(); i++)
    {
        current.execute(moves[i]);
        MoveScore tmp=AlphaBeta<ply-1>(current,them,us,-high,-low);
        current.undo(moves[i]);
        if (-tmp.score>best.score)
        {
            low=-tmp.score;
            best.move=moves[i];
            best.score=low;
        }
        if (low>=high)
            return best;
    }
    return best;
}

TicTacToeTest.cpp:

#include <iostream>
#include "PathFindingAI.h"
class TicTacToeState:public GameState
{
    private:
    unsigned char board[9];
    public:
    TicTacToeState(){board={' ',' ',' ',' ',' ',' ',' ',' ',' '};}
    virtual GameState &execute(GameMove m)
    {
        if (m.l!=2||(m.data[0]!='X'&&m.data[0]!='O')||m.data[1]>8)
            return *this;
        board[m.data[1]]=m.data[0];
        return *this;
    }
    virtual GameState &undo(GameMove m)
    {
        if (board[m.data[1]]!=m.data[0])
            return *this;
        board[m.data[1]]=' ';
        return *this;
    }
    unsigned char *output()
    {
        unsigned char *ret=new unsigned char[10];
        for (int i=0; i<9; i++)
            ret[i]=board[i];
        ret[9]=0;
        return ret;
    }
    virtual unsigned char *data(){return board;}
};
class TicTacToePlayer:public GamePlayer
{
    private:
    bool isX;
    public:
    TicTacToePlayer(bool isX):isX(isX){};
    virtual int evaluate(GameState &s)
    {
        unsigned char tiars[8][3]={ {s.data()[0],s.data()[1],s.data()[2]},
                                    {s.data()[3],s.data()[4],s.data()[5]},
                                    {s.data()[6],s.data()[7],s.data()[8]},
                                    {s.data()[0],s.data()[4],s.data()[8]},
                                    {s.data()[2],s.data()[4],s.data()[6]},
                                    {s.data()[0],s.data()[3],s.data()[6]},
                                    {s.data()[1],s.data()[4],s.data()[7]},
                                    {s.data()[2],s.data()[5],s.data()[8]}};
        int ret=0;
        for (int i=0; i<8; i++)
        {
            //1st check for win
            if (tiars[i][0]==tiars[i][1]&&tiars[i][1]==tiars[i][2])
            {
                if (isX)
                    return (tiars[i][0]=='X'?INT_MAX:INT_MIN);
                else
                    return (tiars[i][0]=='X'?INT_MIN:INT_MAX);
            }//then return [# of 3-in-a-rows left for me]-[# of …
Labdabeta 182 Posting Pro in Training Featured Poster

I have a function that takes a reference to an interface class as a parameter. I have created a derived class from the interface class, but when I try to call the function with an instance of my derived class as an argument I get a compiler error saying "error: no matching call to 'FunctionName(DerivedClassName &)'" Why won't my compiler let me use inheritance?i

Labdabeta 182 Posting Pro in Training Featured Poster

My suggestion (although I do not have much experience in Windows API programming per se) would be to create a pop-up window and force it to remain on top of the z-order at all times and visible at all times. That way any attempt of the user to interact with other windows would be futile because your window would keep popping up in front of it. Not sure if I helped, but maybe that's what you are looking for?

Labdabeta 182 Posting Pro in Training Featured Poster

Try filling out this code with whatever corresponding functions exist in your library:

const char up=0;//put the value for up here
const char down=0;//put the value for down here
const char right=0;//put the value for right here
const char left=0;//put the value for left here
const char exit=0;//put the value for exit here
const char disp=0;//put the value for display character here
class movingChar
{
    private:
    int x,y;
    void moveUp()
    {
        printAt(x,y,' ');//delete the old char (use the library for this)
        y++;//change position (possible use y-- depending on your coordinate system
        printAt(x,y,disp);
    }
    //etc...
    public:
    movingChar()
    {
        //you need to do this on your own
    }
    bool step()
    {
        //this is the step function, return false to quit
        char in=getInput();//your library will have this i suppose?
        switch (in)
        {
             case up:
             moveUp();
             return true;
             case down:
             moveDown();
             return true;
             //etc...
             case exit:
             return false;
             default:
             return true;
        }
    }
};
int main()
{
    movingChar mc;
    while (mc.step()){}
    return 0;
}
Labdabeta 182 Posting Pro in Training Featured Poster

How can I get the coordinates of the output of a truetype font. I need to write a function like so:

struct CartesianCoord{float x,float y};
CartesionCoord *printfont(const char *filename,char ch, int &len)
{
    /*Read the font in filename.ttf and convert it to a set of vertices describing the resulting shape. Do this for the letter 'ch' and return an array of coordinates describing it. len stores the length of this returned array.*/
}

I think I need to know more about .ttf file formats but I do not know where to begin! (Yes I have tried googling it)

Labdabeta 182 Posting Pro in Training Featured Poster

I have some code, but I am missing the meat and potatoes of the code (I think). Here is what I have in a header file called AlphaBeta.h:

#include <vector.h>
using namespace std;
class GameMove
{
    //what do i put here?
};
struct MoveScore
{
    GameMove move;
    int score;
};
class GameState
{
    public:
    virtual GameState &execute(GameMove)=0;//execute the move
    virtual GameState &undo(GameMove)=0;//undo the move
    virtual vector<GameMove> getMoves()=0;//get all possible moves
};
class GamePlayer
{
    public:
    virtual int evaluate(GameMove)=0;//evaluate the move
};
#define INT_MINVAL
#define INT_MAXVAL
template <unsigned int ply>
MoveScore AlphaBeta(GameState current, GamePlayer us, GamePlayer them, GameMove nullMove, int low=INT_MINVAL, int high=INT_MAXVAL)
{
    MoveScore best={nullMove,0};
    if (ply==0||current.getMoves().size()==0)
    {
        best.score=us.evaluate(current);
        return best;
    }
    vector<GameMove> moves=current.getMoves();
    for (int i=0; i<moves.size(); i++)
    {
        current.execute(moves[i]);
        MoveScore tmp=AlphaBeta<ply-1>(current,them,us,nullMove,-high,-low);
        current.undo(moves[i]);
        if (-tmp.score>best.score)
        {
            low=-tmp.score;
            best.move=moves[i];
            best.score=low;
        }
        if (low>=high)
            return best;
    }
    return best;
}

I also have a file called tictactoe.cpp and I know that I need to define the different interfaces but I do not know exactly how. I am also not sure that my alphabeta algorithm is correct.

Labdabeta 182 Posting Pro in Training Featured Poster

I have been teaching myself algorithms and I am stuck with the AlphaBeta AI algorithm. I want to test my knowledge of it with a TicTacToe simulation. The thing is I want it to be easy to modify for any two player game and I cannot figure out how to get it to return the best next move. Can anybody give me any pointers, or maybe an idea of how to make the abstract classes that will be used by my function? I am really stuck.

Labdabeta 182 Posting Pro in Training Featured Poster

I need to know where to find a tutorial on javascript assuming javascript is what I need. I want to make a dynamic website with buttons that can toggle visibility of text. Really I just need something like a link that makes a bunch of text appear then disappear but having a little more knowledge would help. I know very little about website programming although I have taken a course on website design we used dreamweaver to make all the html code for us and we just used flash to do anything 'flashy'. Any ideas on where I should begin?

Labdabeta 182 Posting Pro in Training Featured Poster

Taking your suggestions in hand, would this be 'perfect'?

myDLL.cpp:

#include "myDLL.h"
void DLL_EXPORT ConstructMyInt(myIntStruct *i){i->val=0;}
void DLL_EXPORT DoSomething(myIntStruct *i){i->val*=i->mul++;}

myDLL.h:

#ifndef MY_BAD_FORGETTING_HEADERS_H
#define MY_BAD_FORGETTING_HEADERS_H 1.0//I like a version here
#include <windows.h>
#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport) __stdcall
#else
    #define DLL_EXPORT __declspec(dllimport) __stdcall
#endif
#ifdef __cplusplus
extern "C"
{
#endif
struct myIntStruct
{
int val;
int mul;
};
void DLL_EXPORT ConstructMyInt(myIntStruct *);
void DLL_EXPORT DoSomething(myIntStruct *);
#ifdef __cplusplus
}
#include "myInt.h"
#endif
#endif

myInt.h:

#ifndef MY_BAD_FORGETTING_HEADERS_H
#ifndef MY_INT_H
#define MY_INT_H 1.0//version 1.0
#if MY_INT_H==MY_BAD_FORGETTING_HEADERS_H
class myInt
{
private:
myIntStruct variables;
public:
myInt(){ConstructMyInt(&variables);}
int do(){DoSomething(&variables);return variables.val;}
};
#endif
#endif
#endif

myTestProgram.cpp:

#include "myDLL.h"
#include <iostream>
int main()
{
    myInt i;
    std::cout<<i.do()<<i.do()<<endl;
    return 0;
}
Labdabeta 182 Posting Pro in Training Featured Poster

A friend asked me how to write and use a DLL in C++. I told him that I had to check and make sure my understanding was accurate. So here I am. Would this work as a DLL:

myDLL.cpp:

#include "myDLL.h"
void DLL_EXPORT ConstructMyInt(myIntStruct &i){i.val=0;}
void DLL_EXPORT DoSomething(myIntStruct &i){i.val*=i.mul++;}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
            // attach to process
            // return FALSE to fail DLL load
            break;

        case DLL_PROCESS_DETACH:
            // detach from process
            break;

        case DLL_THREAD_ATTACH:
            // attach to thread
            break;

        case DLL_THREAD_DETACH:
            // detach from thread
            break;
    }
    return TRUE; // succesful
}

myDLL.h:

#include <windows.h>
#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
#else
    #define DLL_EXPORT __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C"
{
#endif
struct myIntStruct
{
int val;
int mul;
};
void DLL_EXPORT ConstructMyInt(myIntStruct &);
void DLL_EXPORT DoSomething(myIntStruct &);
#ifdef __cplusplus
}
#include "myInt.h"
#endif

myInt.h:

class myInt
{
private:
myIntStruct variables;
public:
myInt(){ConstructMyInt(variables);}
int do(){DoSomething(variables);return variables.val;}
};

myTestProgram.cpp:

#include "myDLL.h"
#include <iostream>
int main()
{
    myInt i;
    std::cout<<i.do()<<i.do()<<endl;
    return 0;
}
Labdabeta 182 Posting Pro in Training Featured Poster

Ok, I changed those lines to:

char *temp=new char[len(text)+len("<table width=\"100%%\" border=\"0\" cellspacing=\"0\">\n<tr>\n<td>1.</td>\n<td bgcolor=\"#FFFFFF\">")+len("</span></td>\n</tr>\n</table>")];
    sprintf(temp,"<table width=\"100%%\" border=\"0\" cellspacing=\"0\">\n<tr>\n<td>1.</td>\n<td bgcolor=\"#FFFFFF\">%s</span></td>\n</tr>\n</table>",text);

just for a temporary fix. I am now working on converting it to std::string. I guess I just thought that using c-strings would be faster since I don't need all the functionality of an std::string. I guess I was wrong. Thanks!

Labdabeta 182 Posting Pro in Training Featured Poster

I attached a word document that better explains using pictures. This has me stumped, I cannot think of any reason why the debugger can run the code successfully but outside it the code fails.

Labdabeta 182 Posting Pro in Training Featured Poster

The code for read() works fine, its write() that is only working in the debugger and not outside of it. When I debug the program executes with no problem and with the expected output, but when I just run it outside the debugger it crashes and a windows error report screen pops up. Why does my write() function fail only outside the debugger?

Labdabeta 182 Posting Pro in Training Featured Poster

Sorry, my ideas were not gathered. Let me rephrase, I have a crash in this function but only when I run it outside the debugger. I get to the commented line before failure:

void write(char *text, char *fname)
{
    if (text==NULL)
        return;
    FILE *file=fopen(fname,"w");
    printf("Opening output file...\n");
    if (file==NULL)
        return;
    printf("Printing to file...\n");
    for (int i=0; text[i]; i++)
    {
        fputc(text[i],file);//failure here... but why?!
        printf("%c",text[i]);
    }
    printf("Done printing to file...\n");
    fclose(file);
}
Labdabeta 182 Posting Pro in Training Featured Poster

Let me explain a little further. In my debugger this all works fine, but when I actually run the program outside the debugger it fails... but why?

Labdabeta 182 Posting Pro in Training Featured Poster

I am getting extra garbage at the end of the string returned by this function. Why?

char *read(char *fname)
{
    FILE *file=fopen(fname,"r");
    printf("Opening input file...\n");
    if (file==NULL)
        return NULL;
    int length=0;
    char *ret=new char[length];
    while (!feof(file))
    {
        char ch=fgetc(file);
        char *temp=new char[length+1];
        for (int i=0; i<length; i++)
            temp[i]=ret[i];
        delete[]ret;
        if (ch==EOF)
            ch=0;
        temp[length++]=ch;
        ret=temp;
    }
    fclose(file);
    return ret;
}
Labdabeta 182 Posting Pro in Training Featured Poster

I got it working, I changed my debugger settings and had to close Code::Blocks and re-open the project. Then I rebuilt it, and the debugger worked properly. Thanks for the help!

Labdabeta 182 Posting Pro in Training Featured Poster

I think that I have attached a picture of my failing Code::Blocks to this reply.

Labdabeta 182 Posting Pro in Training Featured Poster

I tried adding that, it still ignores my breakpoints though, and the output doesn't change. I am really confused now... Code::Blocks y u no debug?!

Labdabeta 182 Posting Pro in Training Featured Poster

A) How do I check if I am compiling natively.
B) I thought Eclipse was just for Java
C) Are you sure that I can't fix Code::Blocks?

Labdabeta 182 Posting Pro in Training Featured Poster

I tried what you said. I even made a new project to no avail. I have a Windows XP Professional (Build 2600.xpsp_sp3_gdr.101209-1647 if you need to know). I really need a debugger, what can I do?

Labdabeta 182 Posting Pro in Training Featured Poster

Please help, I cannot get anything done without a debugger...

Labdabeta 182 Posting Pro in Training Featured Poster

I am really confused as to the purpose of the unary + operator. What in the world does it do? I have tested it and it doesn't return the absolute value. Does it just return the operand? And if so what purpose is it?! (I mainly care about integer types)

Labdabeta 182 Posting Pro in Training Featured Poster

If it will help here is the output in the debug window:

Building to ensure sources are up-to-date
Build succeeded
Selecting target: 
Debug
Adding source dir: D:\Programming\C++\SyntaxHighlighter\
Adding source dir: D:\Programming\C++\SyntaxHighlighter\
Adding file: bin\Debug\SyntaxHighlighter.exe
Starting debugger: 
done
Registered new type: wxString
Registered new type: STL String
Registered new type: STL Vector
Setting breakpoints
(no debugging symbols found)
Debugger name and version: GNU gdb 6.8
Child process PID: 9312
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
Program received signal SIGTRAP, Trace/breakpoint trap.
In ntdll!DbgUiConnectToDbg () (C:\WINDOWS\system32\ntdll.dll)
Debugger finished with status 0
Labdabeta 182 Posting Pro in Training Featured Poster

I did install MinGW with Code::Blocks and I even tried reinstalling. I have tested the debugger with a bunch of different programs now and it just doesnt work for any of them. The program I currently need the debugger for is this one that is designed to parse code for HTML output:

#include <stdio.h>
#define IFNAME "input.txt"
#define OFNAME "output.txt"
int len(char *str)
{
    int i;
    for (i=0; str[i]; i++){}
    return i;
}
char *read(char *fname)
{
    FILE *file=fopen(fname,"r");
    printf("Opening input file...\n");
    if (file==NULL)
        return NULL;
    int length=0;
    char *ret=new char[length];
    while (!feof(file))
    {
        char ch=fgetc(file);
        char *temp=new char[length+1];
        for (int i=0; i<length; i++)
            temp[i]=ret[i];
        delete[]ret;
        temp[length]=ch;
        length++;
        ret=temp;
    }
    fclose(file);
    return ret;
}
void write(char *text, char *fname)
{
    if (text==NULL)
        return;
    FILE *file=fopen(fname,"w");
    printf("Opening output file...\n");
    if (file==NULL)
        return;
    for (int i=0; text[i]; i++)
        fputc(text[i],file);
    fclose(file);
}
void replaceAll(char *text, const char *find, const char *replace)
{
// TODO (lburke#1#): Create this function!
}
void replaceAll(char *text, char  find, const char *replace)
{
    for (int i=0; text[i]; i++)
    {
        if (text[i]==find)
        {
            //return text[0->i],replace,replaceAll(text[i->end],find,replace)
            char *end=new char[len(text)-i];
            for (int ii=i; text[ii]; ii++)
                end[ii-i]=text[ii];
            replaceAll(end,find,replace);
            char *ret=new char[i+len(end)];
            char *start=new char[i];
            for (int ii=0; ii<i; ii++)
                start[ii]=text[ii];
            sprintf(ret,"%s%s",start,end);
            delete[]start;
            delete[]end;
            delete[]text;
            text=ret;
        }
    }
}
void insertBefore(char *text, int index, char insert)
{
    char *ret=new char[len(text)+1];
    for (int i=0; i<index; i++)
        ret[i]=text[i];
    ret[index]=insert;
    for (int i=index; text[i-1]; i++)
        ret[i]=text[i-1];
    delete[]text;
    text=ret;
}

void highlight(char *text)
{
    if (text==NULL)
        return;
    int length=len(text);
    replaceAll(text,"&nbsp"," ");
    replaceAll(text,"&gt",">");
    replaceAll(text,"&lt","<");
    replaceAll(text,"&quot","\"");
    replaceAll(text,"&amp","&");
    char …
Labdabeta 182 Posting Pro in Training Featured Poster

I would absolutely suggest using classes. Something like this will do nicely:

class enemyCar
{
    private:
    char car[4][3]=//put your cars' code here
    int x;
    int y;
    public:
    enemyCar(/*any constructor parameters you may want*/);//this is the constructor
    void step();//executes one turn for this car
    void draw();//draw the car in the correct spot
};

Once you have this class (with the functions filled in of course) you can create and use enemy cars in something similar to this:

enemyCar *enemies=new enemyCar[/*number of enemies you want*/];
while (/*player is alive*/)
{
    for (int i=0; i</*number of enemies you want*/; i++)
    {
        enemies[i].step();
        enemies[i].draw();
    }
}

Hope this helps!

Labdabeta 182 Posting Pro in Training Featured Poster

I have a C++ console project in Code::Blocks and I have set breakpoints to test an error. I have set the build for the project to produce debugging symbols for the debug build. I built it, then when I hit debug it just ignored my breakpoints and skipped to the end of the program. The debugging window said "(no debugging symbols found)" what is wrong with my Code::Blocks?

Labdabeta 182 Posting Pro in Training Featured Poster

I have a website where I show a lot of C++ source code in tables by creating styles named, preprocessor, operator, keyword, etcetera and applying these styles where appropriate to highlight the syntax in my code. The problem is that I have so far been doing it manually. For example I search the entire code and as I see keywords I style them as such. Is there any way of doing this that is not so tedious?

Labdabeta 182 Posting Pro in Training Featured Poster

In case it helps here is the configuration:

#---------------------------------------------------------------------------
# Project related configuration options
#---------------------------------------------------------------------------
DOXYFILE_ENCODING      = UTF-8
PROJECT_NAME           = "OpenGL λαβ DLL Documentation"
PROJECT_NUMBER         = 1.0
PROJECT_BRIEF          = "Documentation for OpenGL λαβ DLL"
PROJECT_LOGO           = D:/ProgrammingLogo.bmp
OUTPUT_DIRECTORY       = D:/Programming/C++/OpenGLLAB
CREATE_SUBDIRS         = YES
OUTPUT_LANGUAGE        = English
BRIEF_MEMBER_DESC      = YES
REPEAT_BRIEF           = YES
ABBREVIATE_BRIEF       = "The $name class" \
                         "The $name widget" \
                         "The $name file" \
                         is \
                         provides \
                         specifies \
                         contains \
                         represents \
                         a \
                         an \
                         the
ALWAYS_DETAILED_SEC    = NO
INLINE_INHERITED_MEMB  = NO
FULL_PATH_NAMES        = YES
STRIP_FROM_PATH        = 
STRIP_FROM_INC_PATH    = 
SHORT_NAMES            = NO
JAVADOC_AUTOBRIEF      = NO
QT_AUTOBRIEF           = NO
MULTILINE_CPP_IS_BRIEF = NO
INHERIT_DOCS           = YES
SEPARATE_MEMBER_PAGES  = NO
TAB_SIZE               = 4
ALIASES                = 
OPTIMIZE_OUTPUT_FOR_C  = NO
OPTIMIZE_OUTPUT_JAVA   = NO
OPTIMIZE_FOR_FORTRAN   = NO
OPTIMIZE_OUTPUT_VHDL   = NO
EXTENSION_MAPPING      = h=C++
BUILTIN_STL_SUPPORT    = NO
CPP_CLI_SUPPORT        = NO
SIP_SUPPORT            = NO
IDL_PROPERTY_SUPPORT   = YES
DISTRIBUTE_GROUP_DOC   = NO
SUBGROUPING            = YES
INLINE_GROUPED_CLASSES = NO
INLINE_SIMPLE_STRUCTS  = NO
TYPEDEF_HIDES_STRUCT   = NO
SYMBOL_CACHE_SIZE      = 0

#---------------------------------------------------------------------------
# Build related configuration options
#---------------------------------------------------------------------------
EXTRACT_ALL            = YES
EXTRACT_PRIVATE        = YES
EXTRACT_STATIC         = YES
EXTRACT_LOCAL_CLASSES  = YES
EXTRACT_LOCAL_METHODS  = YES
EXTRACT_ANON_NSPACES   = YES
HIDE_UNDOC_MEMBERS     = YES
HIDE_UNDOC_CLASSES     = YES
HIDE_FRIEND_COMPOUNDS  = NO
HIDE_IN_BODY_DOCS      = NO
INTERNAL_DOCS          = NO
CASE_SENSE_NAMES       = NO
HIDE_SCOPE_NAMES       = NO
SHOW_INCLUDE_FILES     = YES
FORCE_LOCAL_INCLUDES   = YES
INLINE_INFO            = YES
SORT_MEMBER_DOCS       = YES
SORT_BRIEF_DOCS        = NO
SORT_MEMBERS_CTORS_1ST = NO
SORT_GROUP_NAMES       = NO
SORT_BY_SCOPE_NAME     = NO
STRICT_PROTO_MATCHING  = NO
GENERATE_TODOLIST      = YES
GENERATE_TESTLIST      = YES
GENERATE_BUGLIST       = YES
GENERATE_DEPRECATEDLIST= YES
ENABLED_SECTIONS       = 
MAX_INITIALIZER_LINES …
Labdabeta 182 Posting Pro in Training Featured Poster

I have a project that I have formatted for Doxygen. I use extra header files to hold extra classes. The thing is that Doxygen only generates documentation for the first header file. How do I get it to document all of the files I specify?

Labdabeta 182 Posting Pro in Training Featured Poster

I need to clear the output console buffer from within java on a windows console. I was thinking of a batch call to "cls" how can this be achieved?

public static void cls(){
//clear the screen pretty please!
}
Labdabeta 182 Posting Pro in Training Featured Poster

I accidentally hit Source->Externalize Strings... on eclipse and now hundreds of string literals are stored in a properties file. I want them back scattered about my code. How can I do this without re-entering them?

Labdabeta 182 Posting Pro in Training Featured Poster

Ok, I guess I will stick to my tables of manually coloured syntax :( Thank you anyways.

Labdabeta 182 Posting Pro in Training Featured Poster

Because I was challenged by a friend to make a C++ tutorial website and I am determined to win my challenge ;). Also how can I have hidden content (so you press a button and all of a sudden extra text appears.

Labdabeta 182 Posting Pro in Training Featured Poster

I cannot see the problem, but then again it is hard to read your code because you seem to have ignored modulation. In case you do not know, modulation is the practice of chunking your code together into separate functions so that they are easier to read. For example you could replace your code for showing the main menu with a showMenu() function. And the code that implements each menu option should also be a function. Your main should look more like this:

int main()
{
    initialize();//this is the section of your code up until the menu loop
    int menuChoice=showMenu();
    while (menuChoice>EXIT_CONDITION)//EXIT_CONDITION will be a macro, defined as 7 in your code
    {
        switch (menuChoice)
        {
            case DISPLAY_CONDITION://again DISPLAY_CONDITION is a macro
            displayRecords();
            break;
            //...
            default:
        }
    }
    return cleanUp();//maybe you need to do something to wrap up the program?
}

Writing the initialize() function and the showMenu() function etcetera is your job. This is just a general idea of proper programming style.

WaltP commented: That's modulARation. Modulation is changing the frequency of something. +17
Labdabeta 182 Posting Pro in Training Featured Poster

The problem is that you are sending ARRAY_SIZE to you sum and average functions. The issue is that the array may not be filled up all the way to ARRAY_SIZE. You need to pass numElements to those functions instead.