William Hemsworth 1,339 Posting Virtuoso

ehh, what the hell ? for some reason my post is before yours, even though i was answering yours :S

William Hemsworth 1,339 Posting Virtuoso

Yep :P

William Hemsworth 1,339 Posting Virtuoso

You posted this twice, and you didn't use code tags...

William Hemsworth 1,339 Posting Virtuoso

Ive gone through mine, formatted and commented it to make it easier to understand, also fixed a couple big problems in it.

Hope it helps :P

William Hemsworth 1,339 Posting Virtuoso

Try this:

FORMNAME ^myform = gcnew FORMNAME();
myform->ShowDialog();
William Hemsworth 1,339 Posting Virtuoso

lie is a word I cant make a sentence out of

William Hemsworth 1,339 Posting Virtuoso

funnily enough, I have just finished making the exact same thing :), take a look at it if you like, its pretey cool and allows you set set playback speeds & more. Hopefully I will also help you with your problem.

My one saves all the data in pure binary, so it is a bit more compressed than the way your trying to do it. It saves each event in the following structure:

[B]eventID[/B]   [B]keyCode[/B]   [B]x[/B]   [B]y[/B]   [B]delay[/B]

Each variable uses exactly 2 bytes.

I have attached the project, I forgot to mention that its not a console application, but a Win32 API project.
Sorry about my un-neat / un-commented code :P

Prabakar commented: You always give me something I am looking for. Great! +1
William Hemsworth 1,339 Posting Virtuoso

Have you tried adding

using namespace std;

before defining the Dot class ?

William Hemsworth 1,339 Posting Virtuoso

This isn't going to end well :P ..

William Hemsworth 1,339 Posting Virtuoso

I put togeather an example to show you how to read words and compare them with each other, mabey this can get you started.

#include<iostream>
#include<sstream>

using namespace std;

int main() {

	stringstream str("word1 word2 word1 word word1 word2");
	string match = "word2";
	string word;
	int count = 0;
	
	while (str >> word) {
		if (match == word) {
			++count;
		}
	}

	cout << word << ": " << count;

	cin.ignore();
	return 0;
}
William Hemsworth 1,339 Posting Virtuoso

Yep, there was a discussion on that not so long ago on the last few posts. http://www.daniweb.com/forums/thread130222.html

William Hemsworth 1,339 Posting Virtuoso

I've always wondered, is this:

int atr = amountToReturn;

a good/safe way of truncating the decimal portion of a float/double? Should it be type-cast? Or should you use the floor() function?

All the didgets after the decimal point are ignored when converting to an integer, there is no need for type casting.

William Hemsworth 1,339 Posting Virtuoso

Well, lets see what you have so far.

William Hemsworth 1,339 Posting Virtuoso

I personally think you would be able to start Win32 GUI programming. Its really not that hard to learn, it just takes time. A good place to start would be most common Win32 tutorial on the web.. :P
http://www.winprog.org/tutorial/start.html

But just incase, the main things you need to know about are:

Hope this helps.

William Hemsworth 1,339 Posting Virtuoso

Try: form2->TopMost::set(true); Before opening the window.

William Hemsworth 1,339 Posting Virtuoso

Give form2 a topmost value so it always appears above other windows, or open it as a dialog (you wont be able to use form1 until form2 is closed).

William Hemsworth 1,339 Posting Virtuoso

I say do your own dirty work, don't try and get other people involved in cheating.. :angry:

Salem commented: Hell yeah! +17
William Hemsworth 1,339 Posting Virtuoso

Use code tags :), don't just colour it in.

William Hemsworth 1,339 Posting Virtuoso

The variable fin is destructed before you use it. For this to work you would have to arrange it slightly differently.

switch( choose )
	{
	case 1:
		// Constructor that opens the file
		ifstream fin("madlip1.txt");

		// If statement to check if the file can be opened or not.
		if( !fin )
		{
			cerr << "\nError, Can't open the File.!\n";
			exit(1);

		} // end if statement
	
	break; // to exit switch

	case 2:
		// Constructor that opens the file
		ifstream fin("madlip2.txt");

		// If statement to check if the file can be opened or not.
		if( !fin )
		{
			cerr << "\nError, Can't open the File.!\n";
			exit(1);

		} // end if statement

		break; // to exit switch loop

	} // end switch

	int i =0; // Initiate this variable for the use of the array.

	// While Loop to read the File, and save the content in the array


	// Too late, its already been destructed
	while( fin>>x )
	{
		A[i] = x;
		i++;
	}

for this to work, construct the variable fin before the switch statement and open the file like this: fin.open("....");

William Hemsworth 1,339 Posting Virtuoso

Communication with other applications isn't very difficult, the hardest part is just finding the window and the rest is easy. Here is an example which opens notepad.exe, gets its handle and sends the message WM_KEYDOWN a few times.

#include<windows.h>

HWND notepad = NULL;

BOOL CALLBACK WindowEnumProc(HWND hwnd, LPARAM lParam) {
	static bool first = 1;
	if (first) {
		notepad = hwnd;
		first = 0;
	}
	return 1;
}

void SendKey(HWND hwnd, char ch) {
	PostMessage(hwnd, WM_KEYDOWN, VkKeyScan(ch), NULL);
}

void SendKeys(HWND hwnd, char *str) {
	while (*str) SendKey(hwnd, *str++);
}

int main() {
	system("start notepad.exe");
	while (notepad == NULL) {
		// Keep trying until notepad is found
		notepad = FindWindow("Notepad", "Untitled - Notepad");
		Sleep(100);
	}
	// Find client textbox
	EnumChildWindows(notepad, WindowEnumProc, NULL);
	// Send keys
	SendKeys(notepad, "hello world");
	return 0;
}

Here is another snippet I made which loads up mspaint.exe and draws shapes in it. :)
http://www.daniweb.com/code/snippet886.html

William Hemsworth 1,339 Posting Virtuoso

Look how old the thread is.. Read the rules!!

Salem commented: You tell 'em!!! +17
William Hemsworth 1,339 Posting Virtuoso

First, you have to load the image using a function like LoadBitmap (may only work for .bmp) or LoadImage, and using some GDI functions like GetPixel you can retrieve a pixel from its HDC. You will probably want to look at a tutorial on GDI before trying this.

William Hemsworth 1,339 Posting Virtuoso

today is the 30th june 2008

William Hemsworth 1,339 Posting Virtuoso

So you revived a 16 month old thread just to say "I will try it!"..

William Hemsworth 1,339 Posting Virtuoso

I think you need Visual Studio 2005 at least to do .NET applications. Download visual Studio 2008 express version here. http://www.microsoft.com/express/download/#webInstall

William Hemsworth 1,339 Posting Virtuoso

The method is wrong.
Save it in jpeg, gif or png.
You can ask on specialized win32 api newsgroup :
news://194.177.96.26/comp.os.ms-windows.programmer.win32
where it's a FAQ...

Tell me, in what way is it wrong ??..

William Hemsworth 1,339 Posting Virtuoso

No it doesn't, you have to create a Win32 project, make a window and then make it change the icon at runtime.. The point is I dont need to try it because im not the one with the problem, hes the one who should be putting in the effort to test it, im just giving suggestions.

William Hemsworth 1,339 Posting Virtuoso

Here is the code to take a full screenshot. The only problem is the bitmap is completely uncompressed and can be 4mb+ just off one screenshot. The next part would be to figure out how to compress the bitmap.

int main() {
	int x1 = 0;
	int y1 = 0;
	int x2 = GetSystemMetrics(SM_CXSCREEN);
	int y2 = GetSystemMetrics(SM_CYSCREEN);
	ScreenCapture(x1, y1, x2 - x1, y2 - y1, "picture.bmp");
	cin.ignore();
	return 0;
}
William Hemsworth 1,339 Posting Virtuoso

I found this code on the web some time ago and saved it. It seems to do exactly what you want :) Heres a quick example of how to save the top left corner of the screen.

#include<iostream>
#include<windows.h>
using namespace std;

inline int GetFilePointer(HANDLE FileHandle){
	return SetFilePointer(FileHandle, 0, 0, FILE_CURRENT);
}

bool SaveBMPFile(char *filename, HBITMAP bitmap, HDC bitmapDC, int width, int height){
	bool Success=0;
	HDC SurfDC=NULL;
	HBITMAP OffscrBmp=NULL;
	HDC OffscrDC=NULL;
	LPBITMAPINFO lpbi=NULL;
	LPVOID lpvBits=NULL;
	HANDLE BmpFile=INVALID_HANDLE_VALUE;
	BITMAPFILEHEADER bmfh;
	if ((OffscrBmp = CreateCompatibleBitmap(bitmapDC, width, height)) == NULL)
		return 0;
	if ((OffscrDC = CreateCompatibleDC(bitmapDC)) == NULL)
		return 0;
	HBITMAP OldBmp = (HBITMAP)SelectObject(OffscrDC, OffscrBmp);
	BitBlt(OffscrDC, 0, 0, width, height, bitmapDC, 0, 0, SRCCOPY);
	if ((lpbi = (LPBITMAPINFO)(new char[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)])) == NULL) 
		return 0;
	ZeroMemory(&lpbi->bmiHeader, sizeof(BITMAPINFOHEADER));
	lpbi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	SelectObject(OffscrDC, OldBmp);
	if (!GetDIBits(OffscrDC, OffscrBmp, 0, height, NULL, lpbi, DIB_RGB_COLORS))
		return 0;
	if ((lpvBits = new char[lpbi->bmiHeader.biSizeImage]) == NULL)
		return 0;
	if (!GetDIBits(OffscrDC, OffscrBmp, 0, height, lpvBits, lpbi, DIB_RGB_COLORS))
		return 0;
	if ((BmpFile = CreateFile(filename,
						GENERIC_WRITE,
						0, NULL,
						CREATE_ALWAYS,
						FILE_ATTRIBUTE_NORMAL,
						NULL)) == INVALID_HANDLE_VALUE)
		return 0;
	DWORD Written;
	bmfh.bfType = 19778;
	bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
	if (!WriteFile(BmpFile, &bmfh, sizeof(bmfh), &Written, NULL))
		return 0;
	if (Written < sizeof(bmfh)) 
		return 0; 
	if (!WriteFile(BmpFile, &lpbi->bmiHeader, sizeof(BITMAPINFOHEADER), &Written, NULL)) 
		return 0;
	if (Written < sizeof(BITMAPINFOHEADER)) 
		return 0;
	int PalEntries;
	if (lpbi->bmiHeader.biCompression == BI_BITFIELDS) 
		PalEntries = 3;
	else PalEntries = (lpbi->bmiHeader.biBitCount <= 8) ?
					  (int)(1 << lpbi->bmiHeader.biBitCount) : 0;
	if(lpbi->bmiHeader.biClrUsed) 
	PalEntries = lpbi->bmiHeader.biClrUsed;
	if(PalEntries){
	if (!WriteFile(BmpFile, &lpbi->bmiColors, PalEntries * sizeof(RGBQUAD), &Written, NULL)) 
		return 0;
		if …
William Hemsworth 1,339 Posting Virtuoso

-994

William Hemsworth 1,339 Posting Virtuoso

169 = 10101001

William Hemsworth 1,339 Posting Virtuoso

you and me

William Hemsworth 1,339 Posting Virtuoso

Its possible to do, ive never tried this but I think it should work.

SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(NULL,
MAKEINTRESOURCE(ICON_ID)));
William Hemsworth 1,339 Posting Virtuoso

ROFL...I can't believe I'd be laughing this hard. You really got a nice thread going on here buddies. I'm already teary eyed.

Yeh, ive read this thread start to finish :P

William Hemsworth 1,339 Posting Virtuoso

-991

William Hemsworth 1,339 Posting Virtuoso

Alright, ive optimized by making it go up to the square route of the value :icon_smile:

template<typename type>
bool IsPrime(type val) {
   if (val == 2) return 1; else
   if (val < 2 || val % 2 == 0) return 0;
   bool prime = 1;
   register type _max = sqrt((double)val) + 1;
   for (register type i = 3; i < _max; i += 2) {
      if (!(val%i)) {
         prime = 0;
         break;
      }
   }
   return prime;
}

I noticed a huge increase in speed when using this.
How else could it be optimized?

William Hemsworth 1,339 Posting Virtuoso

eh, thats the boring way ^.^

William Hemsworth 1,339 Posting Virtuoso

Nope, perhaps you could give a good example of a well optimised prime number generator ?

William Hemsworth 1,339 Posting Virtuoso

Hi everyone.
Here is a program I made which will find patterns in a list of primes.
It does this by finding the difference between each prime and adding them to a vector like this:

Primes:      2 3 5 7 11 13
              ^ ^ ^ ^  ^
Difference:   1 2 2 4  2

After it has all the differences between the primes it then checks for patterns by using a nested loop to check different parts of the vector with each other.

Protend these are the results in the vector, is would match up these numbers and display the first prime in the pattern along with the match length.

Differences: 1 2 2 4 2 3 1 1 2 2 4 2

1 2 2 4 2 3 1 1 2 2 4 2
|_______| --> |_______|

Match Length: 5
#include<iostream>
#include<vector>
using namespace std;

typedef unsigned int UINT;

#define MIN(a,b)   ((a)<(b)?(a):(b))
#define MAX(a,b)   ((a)<(b)?(b):(a))

template<typename type>
bool IsPrime(type val) {
   if (val == 2) return 1; else
   if (val < 2 || val % 2 == 0) return 0;
   bool prime = 1;
   register type vd2 = val/2;
   for (register type i = 3; i < vd2;i+=2) {
      if (!(val%i)) {
         prime = 0;
         break;
      }
   }
   return prime;
}

UINT MatchLength(const UINT *a1, const UINT *a2) {
   UINT len = 0, _max = MAX(a1, a2) - MIN(a1, a2);
   for (; len < _max && a1[len] == a2[len]; ++len);
   return len;
}

int …
William Hemsworth 1,339 Posting Virtuoso

Its very simple to do. All you need is one function that converts an integer to a 3 bit binary number. If fact, its so simple I will give it to you :)

#include <iostream>
#include <ctime>

using namespace std;

void toBin(int num, char *lpStr, int len) {
	lpStr[len] = 0;
	for (int i = len - 1, remainder; i >= 0; i--)
		lpStr[i] = '0' + (remainder = num % 2, num /= 2, remainder);
}

int main() {
	char bin[4];
	srand(time(NULL));
	for (int i = 0; i < 2000; ++i) {
		toBin(rand()%7, bin, 3);
		cout << bin << '\n';
	}
	cin.ignore();
    return 0;
}

Heres how to get the binary of an integer which is what the function toBin does.
Say you want the binary of 18.

18 % 2 = 0
9  % 2 = 1
4  % 2 = 0
2  % 2 = 0
1  % 2 = 1

bin = 10010
William Hemsworth 1,339 Posting Virtuoso

Seems to be good :)
Only problem I had was that I had to include <ctime> in order to compile it.

William Hemsworth 1,339 Posting Virtuoso

why dont you try it and see..

William Hemsworth 1,339 Posting Virtuoso

that is because the function atoi isn't for converting a char to an int, it converts a string (char array) to an int. If you want to convert a char to an int you need to type cast. for example:

char a = 'z';
int num = (int) a;
William Hemsworth 1,339 Posting Virtuoso

To be honest, I dont think he cares anymore.. His last activity was on Jun 5th, 2007.

Alex Edwards commented: That was one hilarious post... it completely made my day. +1
William Hemsworth 1,339 Posting Virtuoso

Lime is similar to lemon

William Hemsworth 1,339 Posting Virtuoso

Well, you didn't really specify your problem, but this might solve your problem.
:)

// function example
#include <iostream>
#include <string>
using namespace std;

int addition (int a, int b)
{
	int r;
	r=a+b;
	return (r);
}

int main ()
{
	string mystring2;
	string mystr;
	int z;
	z = addition (5,3);
	cout << "Please enter your name:";
	cin >> mystr;
	cout << "The result is " << z << mystr << "isn't that neat!";
	cout << "I'm amazing, agreed?";
	[B]cin.ignore();[/B]
	getline (cin, mystring2);
	cout << mystring2 << "?!";
	[B]cin.ignore();[/B]
	return 0;
}
William Hemsworth 1,339 Posting Virtuoso

hey,

you may try recursive function baseb:

void baseb(int b, string fi, unsigned int nb, string &f){
   int p = nb % b; nb = nb / b;
   if ( nb > 0) baseb(b, fi, nb, f);
   f=f+fi[p];
}
int main(){
   int b = 16, nb = 2008;
   string fi = "0123456789ABCDEF", f = "";
   baseb(b, fi, nb, f);
   cout << endl;
   cout << "Decimal number "<< nb <<" in Base-"<< b << " is "<< f << endl;
   // Result: Decimal number 2008 in Base-16 is 7D8
   cin.get();
   return 0;
}

Now it's your turn to program inverse of function baseb.

krs,
tesu

This code is difficult to understand as your variable names are at most 2 letters long.
Like in this function

void baseb(int b, string fi, unsigned int nb, string &f){
   int p = nb % b; nb = nb / b;
   if ( nb > 0) baseb(b, fi, nb, f);
   f=f+fi[p];
}

what does variable b, fi, nb, f, and p actually do ?
If your going to name them this way you should at least add some comments.

William Hemsworth 1,339 Posting Virtuoso

yep.. try learning loops salman :P

William Hemsworth 1,339 Posting Virtuoso

You can only edit your post within 30 minutes of posting it, you will just have to repost it with the correct changes.

William Hemsworth 1,339 Posting Virtuoso

I agree with AD on that, but there has been times when its been helpfull to use OutputDebugString to print directly to the output window, for example when I was working on a scheduler, every few minutes it would have display its status, which isn't possible to do using breakpoints. I suppose I could have also made a log file to do that but I found this way easier.