m4ster_r0shi 142 Posting Whiz in Training

Piece of cake! Use SetForegroundWindow -> http://msdn.microsoft.com/en-us/library/ms633539(v=vs.85).aspx

#define _WIN32_WINNT 0x0500

#include <windows.h>

int main()
{
    HWND hwnd = GetConsoleWindow();

    int timer_1 = 0;
    int timer_2 = 0;
    int delay = 50;

    while (true)
    {
        if (timer_1 > 100)
        {
            if (GetAsyncKeyState(VK_ESCAPE) >> 15) break;

            timer_1 = 0;
        }

        if (timer_2 > 3000)
        {
            SetForegroundWindow(hwnd); Sleep(15);
            SetForegroundWindow(hwnd);

            timer_2 = 0;
        }

        timer_1 += delay;
        timer_2 += delay;

        Sleep(delay);
    }
}
m4ster_r0shi 142 Posting Whiz in Training

Looks good to me too. Just don't forget to add a trail->next = 0; statement at the end, to show that trail is the last node of your list.

Also, there's a bug in the pseudocode for 'S' I posted above. The first
line should be -> loop while S_quantity != 0 [B]&& head != 0[/B] (because you don't want to try to operate on an empty list, of course!)

m4ster_r0shi 142 Posting Whiz in Training

It's an interesting topic, so I decided to investigate a bit.

The program you wrote does work with video games.
However, don't forget that in a video game the whole
window is redrawn many times a second. This means
your rectangle is visible merely for some milliseconds.

If you try something like this, you can actually see it:

#include <windows.h>

int main()
{
    HWND wnd;
    HDC dc;

    while (true)
    {
        if (GetAsyncKeyState(VK_ESCAPE) >> 15) break;

        wnd = GetForegroundWindow();
        dc = GetDC(wnd);

        Rectangle(dc, 0, 0, 200, 200);

        ReleaseDC(wnd, dc);

        Sleep(15);
    }
}

But there's a flickering problem, even if you remove the Sleep call.

I googled a bit and discovered the so called layered windows:
http://msdn.microsoft.com/en-us/library/ms997507.aspx

Using info from msdn, I whipped up a small test program:

#define _WIN32_WINNT 0x0500

#include <windows.h>

int main()
{
    HWND hwnd;
    HWND lwnd;
    HDC dc;

    SIZE size;
    POINT ptSrc = {0, 0};
    BLENDFUNCTION blend;

    lwnd = CreateWindowEx(WS_EX_LAYERED | WS_EX_TRANSPARENT |
                          WS_EX_TOOLWINDOW | WS_EX_TOPMOST,
                          0, 0, WS_POPUP | WS_VISIBLE,
                          0, 0, 200, 200, 0, 0, 0, 0);
    size.cx = 200;
    size.cy = 200;

    blend.BlendOp = AC_SRC_OVER;
    blend.BlendFlags = 0;
    blend.AlphaFormat = 0;
    blend.SourceConstantAlpha = 100;

    while (true)
    {
        if (GetAsyncKeyState(VK_ESCAPE) >> 15) break;

        dc = GetDC(lwnd);

        Ellipse(dc, 50, 50, 100, 100);

        ReleaseDC(lwnd,dc);

        hwnd = GetForegroundWindow();

        dc = GetDC(hwnd);

        UpdateLayeredWindow(lwnd, dc, 0, 0, 0, 0, 0, &blend, ULW_OPAQUE);

        ReleaseDC(hwnd,dc);

        Sleep(15);
    }
}

However, the flickering problem is still there...

The fact that the flickering remains …

m4ster_r0shi 142 Posting Whiz in Training

it due by tomorrow

Well, you'd better hurry and check out the links I gave you!

I'll give you one last hint:

cmplxNumber add_cmplx(const cmplxNumber & c1, const cmplxNumber & c2)
{
    // create a complex number variable (let's call it temp) 
    
    // set temp's real part to 
    // c1's real part plus c2's real part
    
    // set temp's imaginary part to 
    // c1's imaginary part plus c2's imaginary part
    
    // return temp;
}
m4ster_r0shi 142 Posting Whiz in Training

Hmmm... I'm really beginning to believe that the code you posted above is not yours...
Why don't you refresh your knowledge of functions and structures and then try again?

http://cplusplus.com/doc/tutorial/functions/
http://cplusplus.com/doc/tutorial/functions2/
http://cplusplus.com/doc/tutorial/structures/

m4ster_r0shi 142 Posting Whiz in Training

This means that you just declared your functions, i.e. you just added these lines to your code:

cmplxNumber add_cmplx(const cmplxNumber & c1, const cmplxNumber & c2);
cmplxNumber sub_cmplx(const cmplxNumber & c1, const cmplxNumber & c2);

You have to define them too (i.e. tell the compiler exactly how they work). Just like you do with PrintComplex.

This is its declaration:

ostream & PrintComplex( ostream & outDevice, cmplxNumber Root );

And this is its definition:

ostream & PrintComplex( ostream & outDevice, cmplxNumber Root )
{
	outDevice << Root.dReal;
	if( Root.dImaginary < 0.0 ) {
		outDevice << " - " << abs(Root.dImaginary) << " j" << endl;
	}
	else if( Root.dImaginary > 0.0 ) {
		outDevice << " + " << Root.dImaginary << " j" << endl; 
	}
	else 
		cout << endl;
	return outDevice;
}
m4ster_r0shi 142 Posting Whiz in Training

The problem is that your compiler doesn't know how to add/subtract complex numbers using operators +/-

A simple solution is to define two functions for these operations...

//...

cmplxNumber add_cmplx(const cmplxNumber & c1, const cmplxNumber & c2)
{
    //...
}

cmplxNumber sub_cmplx(const cmplxNumber & c1, const cmplxNumber & c2)
{
    //...
}

//...

...and then use them like this:

//...

C = add_cmplx(A, B);

//...

C = sub_cmplx(A, B);

//...
m4ster_r0shi 142 Posting Whiz in Training

This is not a real problem. You can...

(1) either traverse the array backwards after you sort it,
(2) or pass an extra argument to std::sort:

//...

bool compare(double d1, double d2) { return d1 > d2; }

//...

std::sort(fitness, fitness + 250, compare);

//...

Now, std::sort sorts in descending order.

I suspect that the first solution is faster.

m4ster_r0shi 142 Posting Whiz in Training

Oh, I see. Is this an implementation of a genetic algorithm or something?

I'm not familiar with VS's performance wizard but
I can tell you a couple of things you can do...

First, since you already know the size of your container,
you can simply use an array instead of a list. Second,
you should declare it outside the loop. Something like...

#include <algorithm> // for std::sort

//...

double fitness[250];

loop_2000_times
{
    //...

    for(i = 0; i < 250; ++i)
        fitness[i] = random_number;

    std::sort(fitness, fitness + 250);

    //...
}

Doing it like this saves you creating and destroying your container 2000 times.
Also, using an array saves you the dynamic memory allocation overhead.

Now, what else is inside that loop that runs 2000 times?

m4ster_r0shi 142 Posting Whiz in Training

Hmmm... On second thought, a list of 250 doubles isn't really a big list.
Plus, list::sort's complexity is the same as std::sort's (N log N comparisons).

Are you really sure that sorting the list is what slows you down?

m4ster_r0shi 142 Posting Whiz in Training
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

void shuffle(const string deck[52], string card[52])
{
    // build your temp array here

    for(int i = 0; i < 52; ++i)
        card[i] = deck[temp[i] - 1];
}

int main()
{
    string deck[52] = { "H*A","H*2","H*3","H*4","H*5","H*6","H*7","H*8","H*9","H*10","H*J","H*Q","H*K",
                        "D*A","D*2","D*3","D*4","D*5","D*6","D*7","D*8","D*9","D*10","D*J","D*Q","D*K",
                        "C A","C 2","C 3","C 4","C 5","C 6","C 7","C 8","C 9","C*10","C J","C Q","C K",
                        "S A","S 2","S 3","S 4","S 5","S 6","S 7","S 8","S 9","S*10","S J","S Q","S K"};
    string card[52];

    shuffle(deck, card);
}

EDIT: You could also put deck inside the function...

//...

void shuffle(string card[52])
{
    static const string deck[52] = { /*...*/ };

    // build your temp array here

    for(int i = 0; i < 52; ++i)
        card[i] = deck[temp[i] - 1];
}

int main()
{
    string card[52];

    shuffle(card);
}
m4ster_r0shi 142 Posting Whiz in Training
m4ster_r0shi 142 Posting Whiz in Training

The fact that the function doesn't return anything is irrelevant here.
You can pass your array as an argument, modify it, and the changes
you make will be reflected in main. This is the default behaviour
for arrays. I assure you it will work if you fill in the missing code.
I have tried it and it works perfectly fine for me.

m4ster_r0shi 142 Posting Whiz in Training

i tried your code...

Which one?

This -> http://www.daniweb.com/software-development/cpp/threads/370601/1593577#post1593577 ?
Or this -> http://www.daniweb.com/software-development/cpp/threads/370601/1593600#post1593600 ?

The first obviously won't work unless you fill in the missing code (which is not too hard).
The second obviously won't work if you don't have the necessary boost headers.

m4ster_r0shi 142 Posting Whiz in Training

Ok, I'll play along.

#include <boost/shared_array.hpp>
#include <iostream>
#include <string>

boost::shared_array<std::string> test()
{
    boost::shared_array<std::string> array(new std::string[5]);

    array[0] = "Hello";
    array[1] = "World";
    array[2] = "How";
    array[3] = "Are";
    array[4] = "You";

    return array;
}

int main()
{
    boost::shared_array<std::string> b;

    b = test();

    for(int i = 0; i < 5; ++i)
        std::cout << b[i] << std::endl;

    return 0;
}

EDIT:

i want a function to do some modification to an string array and pass it back...

Did you see my first reply?

m4ster_r0shi 142 Posting Whiz in Training

Why don't you pass the array as an argument to the function instead?

//...

void test(string a[5])
{
    a[0] = "Hello";

    //...
}

int main()
{
    string b[5];

    test(b);

    //...
}
m4ster_r0shi 142 Posting Whiz in Training

I would use two node pointers for this. One always pointing to the first element of my list
and one always pointing to the last one. Much like the head and trail pointers you have.

When I encountered an 'R', I would create a new node
and push it to the end of my list, using the trail pointer.

When I encountered an 'S', I would do something like this:

loop while S_quantity != 0
{
    if head_quantity > S_quantity
    {
        // sell stuff and update head and S
    }
    else if head_quantity <= S_quantity
    {
        // sell stuff and update head and S
    }

    if head_quantity == 0, pop head
}
m4ster_r0shi 142 Posting Whiz in Training

(somewhat off-topic) BTW, if you want something for C++ you should really check this out -> http://code.google.com/p/bwapi/

I downloaded it recently and I'm playing around with it these days. I couldn't get version 3.5 to work; I'm using 3.4 beta with VS 2008 express.
I haven't tried the latest version yet. Here's a video with my latest creation -> http://www.4shared.com/file/pX1C5h00/capture_the_probe_AI.html
I use A* guided by an influence map to find the shortest path to a probe for my zergling, avoiding the photon cannons along the way (^_^)

m4ster_r0shi 142 Posting Whiz in Training

How would I go about calling java in c++ or invoking it..

I don't think you need to do that. Usually, a bot doesn't have access to the game's code.
It only interacts with the game the same way a human would interact with it. That is, by
getting visual input from the screen and sending mouse clicks to appropriate positions.

I believe the most straightforward way to do this in C++ would be to...

(1) Use OS API calls to get color information from the game window.
(2) Use that information to locate the player, the obstacles, the target, etc...
(3) Use some logic to decide what the next move should be.
(4) Use OS API calls to send keystrokes or mouse clicks to the game.
(5) goto step (1)

If you're on windows, these could be useful:

http://msdn.microsoft.com/en-us/library/dd144909(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/ms646310(v=vs.85).aspx

m4ster_r0shi 142 Posting Whiz in Training

Please, don't double post -> http://cplusplus.com/forum/general/45559/

Salem commented: Damn straight - well said! +17
m4ster_r0shi 142 Posting Whiz in Training

Excuse me, this is not homework, right? Why not do it the STL way?

#include <iostream>
#include <string>
#include <bitset>

using namespace std;

string atobcd(const string & ascii)
{
    string ret;

    string::const_iterator cur_it = ascii.begin();
    string::const_iterator end_it = ascii.end();

    for ( ; cur_it != end_it; ++cur_it)
        ret += bitset<8>(*cur_it - '0').to_string(); // uncompressed representation
     // ret += bitset<4>(*cur_it - '0').to_string(); //       packed representation

    return ret;
}

int main()
{
    string ascii;

    while (true)
    {
        cout << "enter a number: ";

        getline(cin, ascii);

        if (ascii == "") break;

        cout << atobcd(ascii) << endl;
    }
}
m4ster_r0shi 142 Posting Whiz in Training

You need to add const qualifiers in your function's declaration and definition.

//...

class csv_File
{
    //...

    void dump2ivector(int column, vector<int> &data) const;

    //...
};

//...

void csv_File::dump2ivector(int column, vector<int> &data) const { /*...*/ }

//...

EDIT: Too slow...

m4ster_r0shi 142 Posting Whiz in Training

Does grab return a reference?

m4ster_r0shi 142 Posting Whiz in Training

...then clearly the attempt was unsuccessful.

Either that or that's what I want other people to believe.

m4ster_r0shi 142 Posting Whiz in Training

It's true. You can build games using C. But you can build better using C++.
Object oriented design helps a lot when you model the game world, entities
and entity interactions. Plus, there are very good libraries available to help.

As for your code request, I guess there are a couple of things I can give you...

The first was my first attempt to write a console based snake game. I attach the cpp file.

The second was an attempt to seduce a girl with an advanced version of the above.
You can get it here -> http://www.4shared.com/file/NXIQiQFA/love_snake.html

I'd appreciate it if you didn't ask whether the attempt was successful or not.

m4ster_r0shi 142 Posting Whiz in Training

Ok, I'll write it in pseudocode:

for (i = 0 to 24)
{
    get type from file

    if (type is R)
    {
        get quantity and price from file

        do stuff...
    }

    else if (type is S)
    {
        get quantity from file

        do stuff...
    }

    else if (type is P)
    {
        get quantity from file

        do stuff...
    }
}

Can you translate the above to C++?

m4ster_r0shi 142 Posting Whiz in Training

This is called padding and it is done by your compiler to improve access speed of the struct members.

Useful link -> http://en.wikipedia.org/wiki/Data_structure_alignment#Data_structure_padding

m4ster_r0shi 142 Posting Whiz in Training

You shouldn't delete your buttons in your constructor. Do it in your destructor, using a for loop.

The for loop in your constructor should look like this:

//...

for(int bt_index = 0; bt_index < NrButtons; bt_index++)
{
    File >> mask; //rading the mask
    File >> imag; //normal image
    File >> imag1;//animated image
    File >> bt_position.x;
    File >> bt_position.y;

    tButton = new Button(imag1.c_str(), imag.c_str(), mask.c_str(), bt_position);
    this->v_button.push_back(tButton);
}

//...

In the constructor I push in the vector a pointer to where memory was allocated, but because I delete the memory allocated in the previous loop(line 72->76)
and set to NULL(and because the pointers in the vector have the same address, they will also point to NULL)such as only the last vector wasn't equal to NULL.

When you push_back a pointer in your vector, a new pointer (careful here, a new pointer, not a new button) is created
and set to point to the address tButton does. Setting tButton to NULL doesn't affect the pointer stored in your vector.

However, deleting tButton sends a message to your OS that that particular memory area is free
to be used by your OS or by other programs. This is bad because the pointers in your vector
point to that area too. The result of using the data in that memory area is undefined.

Since your program doesn't crash and you don't see your buttons, my guess is that either your buttons are drawn but
because …

m4ster_r0shi 142 Posting Whiz in Training

Obviously, we need to see the definition of DocProduction too.

Is CDocum's destructor virtual? If yes, I suspect that the
problem is caused by accidentally overwriting the vtable.

m4ster_r0shi 142 Posting Whiz in Training

Can you show us the definition of CDocum::~CDocum and CDocum::QuoteDocProd?

m4ster_r0shi 142 Posting Whiz in Training

Nothing gets displayed because none of the conditions is met. If you want to check what d_type is, doesn't it make sense to get it
before the check? Try removing the three widgets >> d_type statements you have and put only one of them above your first if.

m4ster_r0shi 142 Posting Whiz in Training

Two things you need to do:

(1) Read the data from the file (put widgets >> d_... statements first thing inside your for loop).
(2) Print the data only after you get all of it (close your for loop immediately after closing if(!head) ).

m4ster_r0shi 142 Posting Whiz in Training

Yes, I believe the above will work.
As for the other question...

Is there any way to write it like while( style!={"contain","defensive","counter"} ) {/*...*/}

Yes, there is a way to do it like this:

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

const string allowed_styles[7] = { "contain", "defensive",
    "counter", "standard", "control", "attacking", "overload" };

bool is_valid_style(const string & style)
{
    return find(allowed_styles,
                allowed_styles + 7,
                style) != allowed_styles + 7;
}

int main()
{
    string style;

    cout << "enter style: ";
    cin  >> style;

    while ( !is_valid_style(style) )
    {
        cout << "invalid style...\n";
        cout << "enter style: ";
        cin  >> style;
    }

    return 0;
}

Useful link -> http://cplusplus.com/reference/algorithm/find/

m4ster_r0shi 142 Posting Whiz in Training

Here's a correct and portable program etc etc...

Ah, right. Thanks for the corrections. In general, I'm happy to just get my programs to compile and run on my pc. I don't like to dig into
portability or standard compliance issues because it really makes my head hurt >:( I prefer to leave that kind of stuff to smarter people...

m4ster_r0shi 142 Posting Whiz in Training

Why do you use managed C++ for this? Try creating an new, empty project, then add a main.cpp file and paste the code there.

m4ster_r0shi 142 Posting Whiz in Training

Are you sure the file exists and is in the correct folder? What do you mean by saying they do not work?
Are you getting compilation errors, or is it something else? It would really help if you posted your code.

m4ster_r0shi 142 Posting Whiz in Training

will that work if my program is running in the background of another program?

Absolutely. GetAsyncKeyState is perfect for implementing global hotkeys. And keyloggers :D

Here's a minimal example:

#include <windows.h>
#include <iostream>

bool IsPressed(int virtual_key)
{
    return GetAsyncKeyState(virtual_key) >> 15;
}

int main()
{
    bool key_state[256] = { 0 };

    while (true)
    {
        // hit esc to quit
        if (IsPressed(VK_ESCAPE)) break;

        for (int i = 0; i < 256; ++i)
        {
            if (IsPressed((i)))
            {
                if (key_state[i] == false)
                {
                    key_state[i] = true;
                    std::cout << i << " was pressed!" << std::endl;
                }
            }
            else
            {
                if (key_state[i] == true)
                {
                    key_state[i] = false;
                    std::cout << i << " was released!" << std::endl;
                }
            }
        }
    }

    return 0;
}
PixelatedKarma commented: Excellent answer to the issue I was having +2
m4ster_r0shi 142 Posting Whiz in Training

I may be wrong, but I think the OP wants to demonstrate that the value of the pointer is
increased by 4 (or whatever sizeof(int) is in his machine) when he uses operator ++ on it.

This should do it:

#include<stdio.h>

int main()
{
    int * p = (int *) 0x2000;

    printf("%x", ++p);

    return 0;
}
m4ster_r0shi 142 Posting Whiz in Training

Yes. It works with any file. Though, it doesn't work with folders. Note that I suggested this because you asked for a simple way.
A more appropriate way to do this would be to use OS API functions -> http://msdn.microsoft.com/en-us/library/aa364418(v=vs.85).aspx

m4ster_r0shi 142 Posting Whiz in Training

You can try opening it for reading and check if it's open. If it's not open, it doesn't exist.

//...

#include <fstream>
#include <string>

//...

std::string filename;

//...

std::ifstream fin(filename.c_str());

if (fin.is_open()) { /* file exists */ }
else { /* file does not exist */ }

//...

Useful link -> http://cplusplus.com/doc/tutorial/files/

m4ster_r0shi 142 Posting Whiz in Training

how can i access a particular memory area using pointers?

Why do you want to do this? What kind of memory area is this? Maybe there are OS API functions that do exactly what you want.
For example, if you want to write a cheat engine on windows, you can use these two to mess with memory of other processes:

http://msdn.microsoft.com/en-us/library/ms680553(v=vs.85).aspx (ReadProcessMemory)
http://msdn.microsoft.com/en-us/library/ms681674(v=vs.85).aspx (WriteProcessMemory)

m4ster_r0shi 142 Posting Whiz in Training

To add to the above, if you don't want to create a window, you can also use this -> http://msdn.microsoft.com/en-us/library/ms646293(v=vs.85).aspx

m4ster_r0shi 142 Posting Whiz in Training

I recently stumbled upon an interesting pdf explaining how you can do OOP in C -> http://www.planetpdf.com/codecuts/pdfs/ooc.pdf

It uses structures like this one to hold the type information in a compact way:

struct Class {

    size_t size;

    void * (* ctor) (void * self, va_list * app);
    void * (* dtor) (void * self);

    void * (* clone) (const void * self);

    int (* differ) (const void * self, const void * b);
};

Maybe skimming it could give you an idea or two.

m4ster_r0shi 142 Posting Whiz in Training

I have a suspicion, I could be wrong though. Do you actually allocate memory for that event?

If you do this instead, does it work?

void proc_events()
{
    SDL_Event my_event;

    while( SDL_PollEvent( &my_event ) )
    {
        if ( my_event.type == SDL_QUIT )
            SDL_Quit();
    }
}
m4ster_r0shi 142 Posting Whiz in Training

You are very close. You should set current to zero every time i changes, not just when it's big enough to become the current mode count.

EDIT: You were closer than I initially thought...

m4ster_r0shi 142 Posting Whiz in Training

It really isn't easy to say without seeing the contents of "user.hpp"...

If I were you, I'd put cout statements in key places of the code in order to isolate the problem.

m4ster_r0shi 142 Posting Whiz in Training
m4ster_r0shi 142 Posting Whiz in Training

The list will be piped in from the command line and terminated with end-of-stream (^Z).

This means that you can simply do it like this:

#include <iostream>
#include <vector>

int main()
{
    std::vector<int> my_vector;

    int temp;

    while (std::cin >> temp)
        my_vector.push_back(temp);

    //...
}

When you reach the end-of-stream (CTRL-Z), std::cin >> temp will evaluate to false and the loop will break.

EDIT: Oh, I thought giving out such a small piece of code wouldn't hurt...

m4ster_r0shi 142 Posting Whiz in Training

Is using a vector a requirement? You could use a (multi)set and its upper_bound member function.
If the data on the file is already sorted, you can use the global upper_bound function with a vector.

Useful links:

http://www.cplusplus.com/reference/stl/set/
http://www.cplusplus.com/reference/stl/set/upper_bound/

http://www.cplusplus.com/reference/stl/multiset/
http://www.cplusplus.com/reference/stl/multiset/upper_bound/

http://cplusplus.com/reference/algorithm/upper_bound/

m4ster_r0shi 142 Posting Whiz in Training

If you don't modify your initial vector after populating the second, I guess it's ok.
Otherwise, both approaches (pointers and iterators) are problematic.

BTW, there are ready solutions to this -> http://www.boost.org/doc/libs/1_46_1/libs/multi_index/doc/index.html

EDIT: Also, if you use decide to use pointers, you probably need pointers to myClass, not to int :P