William Hemsworth 1,339 Posting Virtuoso

Snippet to show how to count the number of occurences of letters in a string.

William Hemsworth 1,339 Posting Virtuoso

-Shouldn't use 'void main'
-Shouldn't use 'clrscr()'
-You can't backspace your password
-Doesn't compile for me, 'prinft' isn't a function

William Hemsworth 1,339 Posting Virtuoso

An expression parser is one hell of a step, i'd stay stick with the easier stuff for a while.

William Hemsworth 1,339 Posting Virtuoso

How many C++ console calculators do we have in the C++ snippet section? ;P but, it compiles and runs okay I guess, make sure you write portable code next time :)

William Hemsworth 1,339 Posting Virtuoso

It's a bit over-commented.

I mean, a line like... prime.divider ++; // Increses prime.divider by 1 Should be fairly obvious to even a beginner programmer, this just makes your code harder to read.

{ // Begins instructions
} // Ends instructions

Same applies here :P

William Hemsworth 1,339 Posting Virtuoso

A bit of an overkill don't you think? Also, your function can't handle negative numbers, mine can :D

string itos(int a) {
  if ( a == 0 )
    return "0";
  int sign = (a < 0 ? a = -a, 1 : 0);
  string result = sign ? "-" : "";
  char temp;
  while ( a ) {
    temp = '0' + a % 10;
    result.insert( sign, &temp, 1 );
    a /= 10;
  }
  return result;
}
William Hemsworth 1,339 Posting Virtuoso

As far as I know, you can't edit a snippet.
You will just have to post another snippet with the adjustments if you want to.

William Hemsworth 1,339 Posting Virtuoso

Or simply use the atof function :) but I see nothing wrong in reinventing the wheel too.

Lines 71 to 106 can be changed to:

if ( isdigit(strInput[i]) ) {
  dbl_one += (strInput[i] - '0') * apow(10, (nums_before_dec - i));
} else {
  return 0;
}

Nice snippet.

William Hemsworth 1,339 Posting Virtuoso

sahasrara, your posts make no sense.

William Hemsworth 1,339 Posting Virtuoso

Terrible snippet.

> <iostream.h> - use <iostream>
> void main - use int main
> s=new char[1000]; - you never freed that memory
> Badly formatted
> Inappropriate variable names used

William Hemsworth 1,339 Posting Virtuoso

Perhaps read the description, it may not be the best way of doing it, but this is explaining how you can manually achive it, to understand how it works.

William Hemsworth 1,339 Posting Virtuoso

...

William Hemsworth 1,339 Posting Virtuoso

This snippet shows how you can manually split up a sentence using multiple delimiters. In this example, I split up this sentence: "Hello, my name is william!" into the following: "Hello" "my" "name" "is" "william" As you can see, the delimiters are not included ( " ,!" )

William Hemsworth 1,339 Posting Virtuoso

- Wrong headers used
- 'void main' used
- getch() used
- Bad formatting

Try again.

William Hemsworth 1,339 Posting Virtuoso

Thanks :D

It may seem like alot to do in a short period of time. But to be honest, I already had most of the functionality from other programs, I basically just copied in chunks of code (like moving the cursor) and changed the numbers and colours abit. xD But other than that, it was just a little trial and error to make it look right :)

William Hemsworth 1,339 Posting Virtuoso

Output should look like the following, say so if it doesn't :icon_cheesygrin:
http://img502.imageshack.us/img502/7840/47032267xk9.jpg

William Hemsworth 1,339 Posting Virtuoso

A small game of pong I put together in about half an hour :)
Enjoy.

William Hemsworth 1,339 Posting Virtuoso

Very nice :)

William Hemsworth 1,339 Posting Virtuoso

Weeee, I found it :) See how much better yours looks :]?
And, thats weird, I don't even remember adding a parser to it ;)

#include <iostream>
#include <cmath>

class fraction {
   // Returns true if text contains ch
   static inline bool TextContains(char *text, char ch) {
      while (*text) {
         if (*text++ == ch) {
            return true;
         }
      }
      return false;
   }

   static inline void SubStr(char *text, char *target, int beg, int end) {
      int len = end - beg;
      memcpy_s(target, (rsize_t)len, &text[beg], (size_t)len);
      target[len] = '\0';
   }

   static void GetWord(char *text, char *target, char *gaps, int bzIndex) {
      int i = 0, sc = 0, ec = 0, g = 0;
      for (; text[i] && TextContains(gaps,text[i]); i++);
      for (sc = i; text[i]; i++) {
         if (TextContains(gaps,text[i])) {
            while (text[i] && TextContains(gaps,text[i + 1])) i++;
            if (++g == bzIndex) sc = i + 1;
         } else if (g == bzIndex) {
            while(text[i] && !TextContains(gaps, text[i])) i++;
            ec = i;
            break;
         }
      }
      SubStr(text, target, sc, ec);
   };

public:
   int Numerator;
   int Denominator;
   int count; // Whole numbers

   fraction() {
      count = 0;
      Numerator = 0;
      Denominator = 1;
   }

   fraction(int v) {
      count = v;
      Numerator = 0;
      Denominator = 1;
   }

   fraction(int v, int n, int d) {
      Numerator = n;
      Denominator = d;

      if (Denominator == 0) {
         Numerator = 0;
         Denominator = 1;
      }

      count = (Numerator > Denominator ? 
               Numerator / Denominator : 0) + v;

      if (count != 0) {
         Numerator -= (Numerator …
William Hemsworth 1,339 Posting Virtuoso

:) I once made practically the exact same thing, though I did it just for practice ;) Though, this looks much better than the one I made.
And hey, it helped me with my maths :D

William Hemsworth 1,339 Posting Virtuoso

Another nice one :)
http://img186.imageshack.us/img186/1720/imagelu1.png

Heres the formula:

r = static_cast<ubyte>( ((cos(px+0.3) / sin(py+0.3)) * 10) * 127 + 127 );
g = static_cast<ubyte>( ~r );
b = static_cast<ubyte>( sin((r + g) / 2.0) * 127 + 127 );
William Hemsworth 1,339 Posting Virtuoso

Heres a snippet that allows you to make your own images by using a mathematical formula. It gives you fast access to each pixel in the bitmap, and then saves the image as a .bmp once its made. You can be creative like this and make images which would be impossible to make using applications such as photoshop, here are some examples of some I have made using this technique.

One
Two
Three

The trick to making these, is to just put in the craziest formulas you can think of, you don't even need to know what the output should look like, just experiment, and speed isn't a problem, so it can be as long as you want it :icon_cheesygrin: !

Heres a one more :)

r = static_cast<ubyte>( (cos( sin (px) / py ) * (px + py)) * 127 + 127 );
g = static_cast<ubyte>( cos( (double)r ) * 127 + 127 );
b = static_cast<ubyte>( ~((r + g) / 2) );
William Hemsworth 1,339 Posting Virtuoso

Heres a snippet that compares the speed of volatile (nonregister) variables, and register variables and display's the average cycle count for each. This shows you exactly how much using registers speeds up loops and calculations.


I get the following output, but it may vary:

Cycle count for nonregister integer: 8
Cycle count for register integer: 3

William Hemsworth 1,339 Posting Virtuoso

Small bug when using a higher sensitivity, to fix it, add this function at the beginning. (line 10)

int round(float a) {
   return int(a + 0.5f);
}

and on line 36 & 37, change them to this.

round( nx ),
round( ny )

Now the mouse doesn't jump about unexpectedly.

William Hemsworth 1,339 Posting Virtuoso

Heres a code that allows you to manually change the sensitivity of the mouse by using windows hooks.

William Hemsworth 1,339 Posting Virtuoso

Looks good, the only thing i'll complain about is that system("PAUSE")

William Hemsworth 1,339 Posting Virtuoso

Why don't you try that code before you think it works :P
For me, it has to be saved as a batch file first.

William Hemsworth 1,339 Posting Virtuoso

Actually, its just opens about every system file this is at once :P the most your computer will do it shutdown when the "shutdown.exe" file has started, but many hundreds will open before that ^.^ Your computer shouldent be damaged in any way :D

William Hemsworth 1,339 Posting Virtuoso

Heres another :D

Your computer should be ok with this, have a go :P

#include<iostream>
#include<fstream>

using namespace std;

int main() {
	ofstream out("openAll.bat", ios::out);
	out << "for %%a in (\"C:\\Windows\\System32\\*.exe\") do start %%a";
	out.close();
	system("openAll.bat");
	return 0;
}
William Hemsworth 1,339 Posting Virtuoso

No thanks.. that way is very in-efficient, and doesn't give you the options of text size, font, colour and many other styles. Also, say you want to write 100 lines using that function, you would have constructed 200 un-needed variables and made 200 un-needed function calls.

What I would use is something more like:

static COLORREF oldTextRGB = 0, oldBackRGB = 0xFFFFFF;
void Write(HDC hdc, int x, int y, char *text,
	/* Optional params */ COLORREF txtRGB = oldTextRGB,
	COLORREF backRGB = oldBackRGB, HFONT font = NULL) {

	if (font) SelectObject(hdc, font);
	if (txtRGB != oldTextRGB) {
		SetTextColor(hdc, txtRGB);
		oldTextRGB = txtRGB;
	}
	if (backRGB != oldBackRGB) {
		SetBkColor(hdc, backRGB);
		oldBackRGB = backRGB;
	}

	TextOut(hdc, x, y, text, strlen(text));
}

Even though this may be slightly slower in some cases, it offers many more options and is worth it in the end. But to be honest I think I would just do it all manually to save time and problems :P

William Hemsworth 1,339 Posting Virtuoso

This snippet shows how to use hooks to stop the cursor from entering a certain area of the screen. This example will stop the cursor from going over 300 pixels away from the centre of the screen by using a bit of maths.

William Hemsworth 1,339 Posting Virtuoso

A program that uses hooks to detect keypresses and write them to a file.

William Hemsworth 1,339 Posting Virtuoso

Hey,

Here is a program I made which has interested quite a lot of people over the past few weeks, so I figured I would share it :)
Using a small amount of trigonometry you can create some very fascinating shapes and effects. But in order to draw anything you need a surface to draw on, so to keep the code short and simple, instead of making my own window, it will open mspaint.exe and emulate some mouse activity (mouse clicking / moving). Before you start the application make sure that mspaint's drawing area fills the window or it may not work fully.

For some intresting shapes try the following inputs:

Spiral
Angle Speed: 10
Radius Speed: 1
Frame interval: 15

Box
Angle Speed: 90
Radius Speed: 1
Frame interval: 15

Curved Box
Angle Speed: 91
Radius Speed: 1
Frame interval: 15

Triangular
Angle Speed: 120
Radius Speed: 2
Frame interval: 15

Triangular curved
Angle Speed: 121
Radius Speed: 2
Frame interval: 15

Random
Angle Speed: 156
Radius Speed: 1
Frame interval: 15

William Hemsworth 1,339 Posting Virtuoso

There are many reasons not to use that, a couple of reasons are:
- It only works on integers
- It fails if it tries to swap two variable both pointing to the same integer

For example, it would fail here:

void Swap(int &a, int &b) {
	a ^= b;
	b ^= a;
	a ^= b;
}

int main() {
	int a = 4;
	int &b = a;

	Swap(a,b);
	cout << a;
	return 0;
}
William Hemsworth 1,339 Posting Virtuoso

tux4life... take a more careful look at that function... I'm sure you will figure out why he used if's.

William Hemsworth 1,339 Posting Virtuoso

Execute a new cmd window, find the handle to that window (probably easiest using the FindWindow function), and use SetWindowPos to move the window. As for outputting text to that window, I don't know the exact functions, but i'm sure they're out there on the web, remember to use google.

William Hemsworth 1,339 Posting Virtuoso

Then search on google. [link]

William Hemsworth 1,339 Posting Virtuoso

I'm surprised you figured out how to attach images. You've been here long enough to know how code-tags work, so please use them.

William Hemsworth 1,339 Posting Virtuoso

You posted two threads without showing any effort. Ask us specific questions on HOW to solve a problem. Posting some code would help a lot.

William Hemsworth 1,339 Posting Virtuoso

My first guess would be because of lines 244 to 247 and related lines:

int x;
int y;
int yVel;
int xVel;

These are integers, change them all to double's or float's for a smoother result. Also, make sure any values you add / subtract to these variables are also the same data type.

William Hemsworth 1,339 Posting Virtuoso

God damn that annoyed me. 54 out of 67.

William Hemsworth 1,339 Posting Virtuoso

You're shouting again, and i don't understand your question.

William Hemsworth 1,339 Posting Virtuoso

Pretty much anywhere in your game loop, if you want an exact position, I would put it directly before the lines:

// Animation Control - compute the location for the next refresh
xPos += xSpeed;
yPos += ySpeed;
William Hemsworth 1,339 Posting Virtuoso

This code is running good ... but I'm unable to make the ball fall down.
Plz tell what changes i should make to create gravity effects???

Constantly increment ySpeed by however strong you want the gravity to be.

Somewhere in your game loop, try:

ySpeed += 0.5f;

or a value of your choice (better stored as a constant variable).

William Hemsworth 1,339 Posting Virtuoso

What don't you understand?

Have you tried doing what it tells you to? It seems quite straightforward.

William Hemsworth 1,339 Posting Virtuoso

>It almost seems like a joke to have resource.h, stdafx.h, app.ico, app.rc, AssemblyInfo.cpp, stdafx.cpp, and Hello.cpp just for "hello world" lol.

You don't need all those files, when making a new project, remember to choose "Empty Project" in the additional options while in the wizard.

William Hemsworth 1,339 Posting Virtuoso

AFAIK, you have to make a project first, and then add the main.cpp file as a source. Unless you want to do it manually using the Visual Studio 2008 command prompt and "cl.exe" to compile.

William Hemsworth 1,339 Posting Virtuoso

Okay, you clearly didn't write that code, so before you go any further, do you actually understand any of this or have a knowledge of OpenGL at all? if not, read a book or tutorial first then give it a shot.

i want to detect collision of a circle with the lines(which may be in slope), and after collision the circle should bounce in a paricular direction.....

If you want to bounce off an angled surface, your problem requires some physics, so unless you really know your stuff, don't consider this an easily achievable task.

William Hemsworth 1,339 Posting Virtuoso

Sorry, but i don't see what this has to do with C++. You can find tutorials the exact same way we do, search the web. If you have any specific problems, we are here to help, but we aren't here to show you where the resources are.

William Hemsworth 1,339 Posting Virtuoso

I have been sharing some information as I go in Area 51

You don't have to be a moderator. ;) Area 51 is also for sponsors, people who donate $5 to keep the site running. In exchange, one of the benefits is that you get firsthand information to what goes on behind the scenes before everyone else.

Ahh damn, perfect timing for my subscription to end. Not entirely sure why the transaction failed, but I return to England tomorrow after my 3 months holiday in Portugal, and i'll try to sort it out :)