William Hemsworth 1,339 Posting Virtuoso

How about using std::valarray.

#include <iostream>
#include <valarray>

int main(int argc, char **argv) {
   std::valarray<std::string> arguments(argc);
   for (int i = 0; i < argc; ++i)
      arguments[i] = argv[i];

   return 0;
}
iamthwee commented: i won't use valarrays but your code helped +17
William Hemsworth 1,339 Posting Virtuoso

Nope, the preprocessor ONLY works before compile time, remember that, so you cant have a mix of the two :)

edit: but maby you should look at the typeid operator, it might be what you need for that example.

William Hemsworth 1,339 Posting Virtuoso

>Please post in where you are having problems with your code

I do not understand anything! I do not even know how to start!!!

Im assuming she just wants it to be done for her with no effort what so ever.

William Hemsworth 1,339 Posting Virtuoso

Theres also another common mistake that people make, which can easily cause memory leaks, consider this example:

int *varPtr = new int[1000]; // Allocate 1000 int's
int intArray[] = {1, 2, 3, 4, 5}; // Make an array
varPtr = intArray; // Assign varPtr with the adress of intArray

In this example, you allocated 1000 int's, but then you made varPtr (pointing to the 1000 int's) point to intArray instead, so what just happened to those 1000 int's you just allocated? To remove the problem, make sure you call the delete[] operator before reassigning the pointer.

Ancient Dragon commented: Good one -- I've seen that here too :) +36
William Hemsworth 1,339 Posting Virtuoso

Im still using firefox as ive noticed 2-3 bugs, and you cant scroll by clicking the mouse wheel like FF can. But if they add this functionality to Chrome, I may consider switching. But what I do like is the tabbing (you can draw a tab out to make a seperate window) and incognito mode.

William Hemsworth 1,339 Posting Virtuoso

right..

William Hemsworth 1,339 Posting Virtuoso

I'm using it now, its good. Only a few things I have noticed:
- Doesn't allow you to scroll by clicking the mouse wheel
- Full screen flash applications run with a slightly slower FPS

William Hemsworth 1,339 Posting Virtuoso

Something I heard, "Life is too good and too short to waste on this filthy habbit, give it up, live a nice long life".

William Hemsworth 1,339 Posting Virtuoso

>That will not help as you will loose members by forcing it that way
Loose members ?? I doubt it, and quite frankly any member who leaves the site after 20 seconds who cant be bothered to read the instructions doesn't really deserve to be here. :icon_wink:

William Hemsworth 1,339 Posting Virtuoso

>They manage to ignore all the other hints we throw at them, what makes you think they'd read this one?

Force them to read it :)
Make it appear for 20 seconds on the screen with no way to escape.

William Hemsworth 1,339 Posting Virtuoso

You can save the entire structure as binary, heres an exmaple of how to:

#include <iostream>
#include <fstream>
using namespace std;

template<typename type>
void SaveStruct(ofstream &out, type &obj) {
   out.write(reinterpret_cast<char*>( &obj ), sizeof type);
}

template<typename type>
void LoadStruct(ifstream &in, type &targetObj) {
   in.read(reinterpret_cast<char*>( &targetObj ), sizeof type);
}

struct A {
   char str[20];
   size_t str_len;
};

int main() {

   // Make Structure
   A a;
   strcpy_s(a.str, 20, "Hello World");
   a.str_len = strlen(a.str);

   // Save structure to file using SaveStruct(...)
   ofstream out("savefile.txt", ios::out | ios::binary);
   SaveStruct(out, a);
   out.close();

   // Load structure from file into the variable b
   A b;
   ifstream in("savefile.txt", ios::in | ios::binary);
   LoadStruct(in, b);

   // Display struct b
   cout << "str: " << b.str << "\nstr_len: " << b.str_len;
   
   cin.ignore();
   return 0;
}
William Hemsworth 1,339 Posting Virtuoso

Maby a good idea would be to make a dialog appear just before submitting thats says (in a very big font) "USE CODE TAGS OR YOU WILL BE IGNORED OR YELLED AT!!!" for newbies who have less than 3-4 posts ? Perhaps that will get to em. ;)

William Hemsworth 1,339 Posting Virtuoso

Thanks for using code tags on your first post :) its not often that happends. Some of your indentation is a bit strange so I will fix that up, I also removed one warning to do with signed / unsigned comparisons.

#include <iostream>
#include <string>
#include <iomanip>
#include <string.h>
#include <fstream>
    
using namespace std;

void displayBinary(unsigned);

int main()
{
	string str;
	fstream file_op("c:\\test_file.txt", ios::in);

	while (file_op >> str) {
		for (size_t i = 0; i < str.length(); i++) {
			displayBinary((unsigned)str.at(i));
		}
	}

	cout << endl;
	cout << endl;

	file_op.close();
	return 0;
}

void displayBinary(unsigned u)
{
	register int b;

	for (b = 128; b > 0; b = b/2) {
		(u & b) ? (cout << '1') : (cout << '0');
	}
	cout << " ";
}

Now that its easier to read, try reading from the file byte by byte.
Heres how you would do that and pass it to your function displayBinary.

ifstream in("test_file.txt", ios::in);
char ch;

// Assign each byte to ch
// and then pass it to the function displayBinary
while (in.get(ch)) {
	displayBinary(ch);
}

in.close();

To save it back to a file you do basically the same thing, except this time use the ofstream and use ofstream::put to write it to the file.

Try modifying the displayBinary function like this.

ofstream out("output.txt", ios::out);
void displayBinary(unsigned u)
{
	register int b;
	char bit;

	for (b = 128; b > 0; b = b/2) {
		(u & b) ? (bit = '1') …
William Hemsworth 1,339 Posting Virtuoso

Yeh, thanks to Ancient Dragon I think theres been some moments when I have had higher reputation than post count :)

William Hemsworth 1,339 Posting Virtuoso

Uhm, I think its quite obvious what my name is supposed to be, would you mind changing it to WilliamHemsworth please :) ?? It was the worst possible mistake I could do when making this account :D

William Hemsworth 1,339 Posting Virtuoso

>I really don't understand, what you both mean by saying 'code tags'
What ?!, are you serious. There are so many places on this site that fully explains how they work.. Try reading the link I posted.

William Hemsworth 1,339 Posting Virtuoso

Oh boy, where to start..
First, learn to use code tags, its not difficult.
Give more details on your problem, and show the errors you are getting.
Dont use void main.
Try posting this again, but this time.. properly
and then I will help you with your problem.

Read this thread which you should have read anyway before posting.
Read This Before Posting

William Hemsworth 1,339 Posting Virtuoso

I suggest you change the structure of your code to look something more like this. Handle all key events and make them only change the velocity, not the actual sprite location. Then at the end, when you have done all the key event handling, then add the velocity to the current location.

I haven't tested this so dont assume that it will work

// vx = Velocity x
// vy = Velocity y

if (event.type == SDL_KEYDOWN) {
   
   static bool jumping = false;
   
   if (keystates [SDLK_RIGHT]) {
      vx += VXSPEED;
   } else
   if (keystates [SDLK_LEFT]) {
      vx -= VXSPEED;
   }

   if (!jumping && keystates [SDLK_UP]) {
      vy = -JUMPSTRENGTH; // Jump upwards
      jumping = true;
   } else

   if (jumping) {
      vy += GRAVITY;
      if (posy > FLOOR_Y) {
         // Hit the floor
         posy = FLOOR_Y;
         vy = 0; // Stop falling
         jumping = false;
      }
   }
   
} else {
   
}

// Mabey add friction to slow sprite down
// About 0.95 would be appropriate
posx *= FRICTION;

// Now apply velocity to current position
posx += vx;
posy += vy;

Remember, this is only how you should structure the code if your going to be using velocity. To be totally honest I dont even think you need velocity for the kind of game that this is, just the jumping. If this code works the way I think it should, then when you press the UP key, the sprite should jump and fall back to the ground, and when …

William Hemsworth 1,339 Posting Virtuoso

where did no change it? Looks the same to me.

include <iostream>

namespace A {
   int var = 10; <<< error here
};

namespace B {
   int var = 20; <<< error here
};

using namespace A; <<< error here
using namespace B; <<< error here
<snip>

Ehh ?? Those aren't errors.

Ancient Dragon commented: you are right :) +34
William Hemsworth 1,339 Posting Virtuoso

I know ^.^ I changed it as fast as I could, way before you posted.

William Hemsworth 1,339 Posting Virtuoso

Its your choise if you want to include namespaces, but they are there for a reason, mainly for structuring your code and avoiding name confliction. Take this example:

#include <iostream>

namespace A {
   int var = 10;
};

namespace B {
   int var = 20;
};

using namespace A;
using namespace B;

int main() {
   std::cout << var; // Which one are we talking about ??
   std::cout << A::var; // Displays  10
   std::cout << B::var; // Displays  20
   return 0;
}

Hope this helps.

William Hemsworth 1,339 Posting Virtuoso

I had this problem many times, its not vista, you have to make sure that the program configuration is set to "Release" instead of "Debug". But I came across so many of these errors when using .NET and got tyred of it, so I stopped using it and have never had any problems since :)

Wiki_Tiki commented: Helped me solve a problem i've been trying to solve for 2 years +1
William Hemsworth 1,339 Posting Virtuoso

Try adding this:

case WM_LBUTTONDOWN:
{ 
   float mx = 0;//mousex
   float my = 0;//mousey
   mx = LOWORD(lParam);
   my = HIWORD(lParam);
   [B]POINT mp;
   mp.x = mx;
   mp.y = my;
   ScreenToClient(hwnd, &mp);
   mx = mp.x;
   my = mp.y;[/B]
   return (0);
}

For more information look here.

edit: I just realised this might not be what your looking for ;)

William Hemsworth 1,339 Posting Virtuoso

Something like this perhaps ?

#pragma comment(lib, "mylib.lib")
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

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

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

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

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

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

It seems a bit pointless to have double the amount of cases than you actually need.

//when user inputs a char..
    char classChoice;
    cin>> classChoice;


    switch(tolower(classChoice))
    {
         case 'w':
                       cPtr = new Character(/*Warrior stats here*/);
                       break;
         case 'm':
                       cPtr = new Character(/*Mage stats here*/);
                       break;
          default:
                       cout << "Improper selection" << endl;
                       break;

    }
William Hemsworth 1,339 Posting Virtuoso

Win32 programming definetly requires a a bit of experience in C++, but if you get good at C++, you dont have to worry about learning the Win32 API, its easy. The hardest part I would say is just remembering all the function and macro names because theres just so many :-/
In the Win32 API the switch statement is very commonly used, and its the basic stuff like that which you really need to know.

William Hemsworth 1,339 Posting Virtuoso

OK then.. Now this is just completely off topic. :icon_wink:

William Hemsworth 1,339 Posting Virtuoso

Hmm, now that we are recommending books, heres the first book I ever read for C++, at about the age of 12 ^.^.
http://www.amazon.com/Beginners-Guide-Second-Guides-McGraw-Hill/dp/0072232153
But there has already been an entire thread for C++ books here.
http://www.daniweb.com/forums/thread70096.html

William Hemsworth 1,339 Posting Virtuoso

Im not the person to ask for that, I have never worked with linux before.

William Hemsworth 1,339 Posting Virtuoso

That tutorial might be to advanced for your level, so stick with console applications for a while before trying the Win32 API or you might get really confused :confused:

William Hemsworth 1,339 Posting Virtuoso

Yes

William Hemsworth 1,339 Posting Virtuoso

You can make more than just console application using C++, you can use the Win32 API to create visual application. Try googling:
C++ Win32 API or
C++ MFC

William Hemsworth 1,339 Posting Virtuoso

You're not very quick, are you?

I see what you mean :D

So your telling me what is being displayed on my windows screen is the output of a C++ program?

Most probably.

William Hemsworth 1,339 Posting Virtuoso

Isn't it just:

binaryImage->Save("binaryImage.bmp");

???

William Hemsworth 1,339 Posting Virtuoso

In addition to what niek_e sead, you could make things easier by using some windows macros.

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

int main(){
    /*max 65535 (= sizeof(unsigned int) /2) */
    unsigned int i = 2222; 
    unsigned int j = 1111;
    unsigned int combined = MAKELONG(i, j);
    cout << LOWORD(combined) << '\n';
    cout << HIWORD(combined);
    cin.ignore();
    return 0;
}
William Hemsworth 1,339 Posting Virtuoso

On line 11 and 19 you cannot compare a char string using the == operator. Instead use strcmp http://www.cplusplus.com/reference/clibrary/cstring/strcmp.html.

Line 11:

if((argv[i] == "-h" | argv[i] == "--help" ) && help != 1)

Should be:

if ((strcmp(argv[i], "-h") == 0) | (strcmp(argv[i], "--help") == 0)&&help != 1)

Do the same thing with line 19 and see if it works then.

You could also use std::string and then use the == operator to compare it with another string.

FreeFull commented: Helpful +2
William Hemsworth 1,339 Posting Virtuoso

It would have been much more practical if you had attached the many pages of code...
But just a few mistakes / tips:

  1. Dont use system("pause") : http://www.gidnetwork.com/b-61.html
  2. In your switch statement switch (move) you have including double the amount of cases than you actually need, just define it like this switch (tolower(move)) and have each case with just a lower case char.
  3. Format your code better!
William Hemsworth 1,339 Posting Virtuoso

Well, dynamically allocating space is much slower than stack and the life of a dynamic variable is dependant on when you decide to delete it, however stack variables are deleted at the end of the code block.

William Hemsworth 1,339 Posting Virtuoso

Well, the way I did it was to first check how the numbers as integers compared and if they are equal, then you move on to the numbers after the decimal point, and compare each one of them until one of the numbers are different.

William Hemsworth 1,339 Posting Virtuoso

Ok then, this makes stuff slightly harder. I didn't really understand that way you tried to do it so I went ahead and made one myself. Theres most probably an easier way to do this, but heres my attempt at it.

#include <iostream>
#include <string>

using namespace std;

int compareFloats(string str1, string str2) {
	int dp1, dp2;
	int iNum1, iNum2;

	for (int i = 0; i < (int) str1.length(); i++) {
		if (str1[i] == '.') {
			dp1 = i;
		}
	}

	for (int i = 0; i < (int) str2.length(); i++) {
		if (str2[i] == '.') {
			dp2 = i;
		}
	}

	iNum1 = atoi(str1.substr(0, dp1).c_str());
	iNum2 = atoi(str2.substr(0, dp2).c_str());

	if (iNum1 < iNum2) return -1;
	if (iNum1 > iNum2) return  1;

	// Check after point
	for (int i = dp1 + 1,
		     j = dp2 + 1;
			 i < (int) str1.length() && j < (int) str2.length(); i++) {
				 if (str1[i] < str2[j]) return -1;
				 if (str1[i] > str2[j]) return  1;
	}

	return 0; // Equal
}

int main(void)
{
	int n;
	string numz[200];

	cin >> n;

	for (int i = 0; i < n; i++) {
		cin >> numz[i];
	}

	for (int i = 0; i < n - 1; i++) {
		for (int j = 0; j < n - 1; j++) {
			int c = compareFloats(numz[j], numz[j+1]);
			if (c == 0) continue;
			if (c == 1) numz[j].swap(numz[j+1]);
		}
	}

	for (int i = 0; i < n; i++) {
		 cout << numz[i] << ' ';
	} …