# C/C++ Querry: Random input



## speedyguy (Oct 5, 2008)

m a noob...ok a bit better than a noob....

so m desgining an animation program whr i need a halt an on going animation..

so my querry is - which command do i use to randomly get an input while a process is active...
for eg. an animation is goin on in an infinite loop....i want user 2 break it on pressing "Q" key....so how do code it???

2ndly....for animation shud i use clrscr() for motion or any other command?

thanks...

Enjoy~!


----------



## Garbage (Oct 5, 2008)

Create a seperate thread for animation ?


----------



## anantkhaitan (Oct 6, 2008)

As for randomization :

```
randomize(); //Initialize Engine
n=rand(); //n integer takes from 0 to highest possible number
```

Or select within range 10 - 90

```
n=rand()%81+10;
```


For input and break you can use kbhit() syntax


```
while(!kbhit())
{
body
}
```


----------



## harryneopotter (Oct 6, 2008)

exactly wat type of animation r u looking for ?


----------



## speedyguy (Oct 9, 2008)

just a loop....for ex creating circle of increasing radius wit the loop value....n wanna break in between wen d user wants to......that kind....

@anantkhaitan:  i think this one is wat i need...
while(!kbhit())
{
body
}

but thats jus for a random key hit....suppose i want a specific key to break that loop?

thanks

Enjoy~!

ps: i tried kbhit().....its working.....can i use it wit parameters?

Enjoy~!


----------



## QwertyManiac (Oct 10, 2008)

Nope.

*int kbhit(void);* (Non-ANSI) returns a non-zero integer in case some key is hit and 0 otherwise. Doesn't tell you what key was hit.


----------



## speedyguy (Oct 10, 2008)

ok so any other command for this?

Enjoy~!


----------



## dheeraj_kumar (Oct 10, 2008)

@Qwerty

Isnt the non zero number the keycode of the pressed key?


----------



## harryneopotter (Oct 10, 2008)

hey ... u can try this ..

void main()
{char ch;

start:
while (!kbhit())
{ch=getch();
if("Your condition to check which key has been pressed, if its the specific key that u wanted to be pressed to break the loop then ..")
goto next;

else
goto start;

next:
BODY OF THE LOOP;
}

Hope you got it ......


----------



## speedyguy (Oct 11, 2008)

yup i got it...thanx mate...

Enjoy~!


----------



## QwertyManiac (Oct 11, 2008)

dheeraj_kumar said:


> @Qwerty
> 
> Isnt the non zero number the keycode of the pressed key?


Nope, AFAIK all kbhit does is check if a value exists in the keyboard buffer and several can exist too. So it just returns any non-zero value on success and 0 otherwise. It's just checking for a stroke and thus getch() will get the normal input post the pressed event, when called.


----------



## dheeraj_kumar (Oct 11, 2008)

@Qwerty

Got it, thanks!


----------



## harryneopotter (Oct 11, 2008)

i knew it will work coz it worked in my project too ...


----------



## speedyguy (Oct 19, 2008)

ok 1 more querry here....refer d foll codes....ofcourse ts not a complete project...tsa work in progress....

"#include<iostream.h>
#include<conio.h>
#include<alloc.h>
#include<graphics.h>

#define CLIP 1

int avg[12][5];

void far setviewport(int,int,int,int,int);
void far getimage(int,int,int,int,void far *buf);
void far putimage(int,int,void far *buf);

void border(int thickness)
{
 int i;
 cleardevice();
 setcolor(4);
 for (i=1;i<thickness;i++)
  rectangle(20+i,20+i,600+i,450+i);
}

void mainpage()
{
 void far *buf;
  int style;
 border(8);
 style=DEFAULT_FONT;
 settextstyle(style,HORIZ_DIR,2);
 setcolor(5);
 outtextxy(75,70,"AUTO-MOBILE FUEL CONSUMPTION");
 setcolor(2);
 settextstyle(TRIPLEX_FONT,HORIZ_DIR,3);
 outtextxy(80,320,"Simulation Project by ABHINAV ARYA");
 outtextxy(110,340,"V SEM BCA, Garden City College");
 setcolor(12);
 style=GOTHIC_FONT;
 settextstyle(style,HORIZ_DIR,1);
 outtextxy(290,380,"press any key to continue...");
 setcolor(6);
 rectangle(120,200,450,250);
 for (int i=120;i<450;i++)
  line(i,200,i,250);
 for (i=1;i<30;i++)
 {
  if (i>20)
   setcolor(8);
  circle(200,250,i);
  circle(370,250,i);
 }
 setcolor(1);
 for (i=190;i<330;i++)
 {
  line(i,200,i+20,170);
 }
 for (i=330;i<390;i++)
 {
  line(i,200,i-20,170);
 }
 setcolor(6);
 line(210,170,370,170);
 setcolor(0);
 line(190,200,390,200);
 line(290,170,290,250);
 setcolor(8);
 for (i=280;i<310;i++)
  line(40,i,70,i);
 getimage(40,280,70,310,buf);
 setviewport(40,280,600,310,CLIP);
 while(!kbhit())
 {
  for (i=0;i<500;i++)
  {
   clearviewport();
   putimage(i,0,buf,COPY_PUT);
  }
 }
 farfree(buf);
}

int selection()
{
 border(4);
 setbkcolor(4);
 setcolor(9);
 settextstyle(DEFAULT_FONT,HORIZ_DIR,3);
 outtextxy(80,100,"AUTOMOBILE FUEL CONSUMPTION");
 settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
 outtextxy(50,150,"Cubic Capacity Chart:::::");
 getch();
 return 0;
}

// START OF THE MAIN FUNCTION //
void main()
{
 int gd=DETECT,gm;
 int avg_ch,avg_f,x;
 int avg_dn;
 clrscr();
 initgraph(&gd,&gm,"");
 mainpage();
 cleardevice();
 avg_ch=selection();
 switch(avg_ch)
 {
  case 1:avg_f=60;
	 avg_dn=100;
	 break;
  case 2:avg_f=55;
	 avg_dn=150;
	 break;
  case 3:avg_f=45;
	 avg_dn=180;
	 break;
  case 4:avg_f=35;
	 avg_dn=250;
	 break;
  case 5:avg_f=20;
	 avg_dn=800;
	 break;
  case 6:avg_f=18;
	 avg_dn=1000;
	 break;
  case 7:avg_f=17;
	 avg_dn=1200;
	 break;
  case 8:avg_f=15;
	 avg_dn=1400;
	 break;
  case 9:avg_f=14;
	 avg_dn=1800;
	 break;
  case 10:avg_f=12;
	  avg_dn=2000;
	  break;
  case 11:avg_f=10;
	  avg_dn=2200;
	  break;
  case 12:avg_f=8;
	  avg_dn=2500;
	  break;
 }
 getch();
 closegraph();
}"

acc to this...wat happens on o/p screen is mainpage() func executes normally wit a small anim but on pressing a key (ie. call of func selection()) the compiler crashes n returns back 2 window....plz exec this code n see if its same wit u...n if u can help me out wit dis....thrs no compiler error on it.

Enjoy~!


----------

