C/C++ Querry: Random input

Status
Not open for further replies.

speedyguy

Cyborg Agent
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~!
 

anantkhaitan

Burning Bright
As for randomization :
Code:
randomize(); //Initialize Engine
n=rand(); //n integer takes from 0 to highest possible number

Or select within range 10 - 90
Code:
n=rand()%81+10;


For input and break you can use kbhit() syntax

Code:
while(!kbhit())
{
body
}
 
OP
speedyguy

speedyguy

Cyborg Agent
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~!
 
Last edited:

QwertyManiac

Commander in Chief
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.
 
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 ......
 

QwertyManiac

Commander in Chief
@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.
 
OP
speedyguy

speedyguy

Cyborg Agent
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~!
 
Status
Not open for further replies.
Top Bottom