mouse not taking input - c

I am trying to implement mouse interfecing but somethings wrong with the input function.It should tell me that wheater i have made a left click or a right click but its not printing anything.Have a look:
#include<graphics.h>
#include<dos.h>
union REGS in,out;
void Graphics(void);
void DetectMouse(void);
void ShowMouse(void);
void HideMouse(void);
void InputMouse(void);
int main()
{
Graphics();
DetectMouse();
ShowMouse();
// HideMouse();
InputMouse();
getch();
closegraph();
return 0;
}
void Graphics(void)
{
int driver=DETECT,mode;
initgraph(&driver,&mode,"C:\\TC\\bgi");
outtextxy(1,1,"Graphics mode activated!");
}
void DetectMouse(void)
{
int result;
in.x.ax=0;
result=int86(0x33,&in,&out);
if(result)
{
outtextxy(10,10,"Driver succesfully detected");
}
else
{
outtextxy(10,10,"Driver not detected");
}
}
void ShowMouse(void)
{
in.x.ax=1;
int86(0x33,&in,&out);
}
void HideMouse(void)
{
in.x.ax=2;
int86(0x33,&in,&out);
}
void InputMouse(void)
{
for(;kbhit();)
{
int x,y;
in.x.ax=3;
int86(0x33,&in,&out);
if(out.x.bx==1)
outtextxy(30,30,"Left click");
if(out.x.bx==2)
outtextxy(40,40,"Right click");
if(out.x.bx==3)
outtextxy(50,50,"Middle click");
delay(100);
}
}

Try if((out.x.bx & 1) != 0) ... - i.e. check just bit 0 - I note that many of the flags are 'unused' for INT33 AX=3 - whether this guarantees them zero or not is anyone's guess.

You probably want to have a main loop. INT 33 AX=0003 doesn't block waiting for mouse input.

Related

a snake game in c and having problem with randomization and some logic

first problem is that the coordinate of the fruit dose not change after the snake consumes it
and my second problem is that i am having problem with the part of code where snake hits self and dies
and can we do something about the flickering of the graphics due to loop
i have used the compiler codeblocks
#include<stdio.h>
#include<conio.h>
#include <windows.h>
#include <stdlib.h>
#include<time.h>
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
void gotoxy(int x, int y)
{
static HANDLE h = NULL;
if(!h)
h = GetStdHandle(STD_OUTPUT_HANDLE);
COORD c = { x, y };
SetConsoleCursorPosition(h,c);
}
void hidecursor()
{
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO info;
info.dwSize = 100;
info.bVisible = FALSE;
SetConsoleCursorInfo(consoleHandle, &info);
}
static int pos1=30,pos2=15;
int fruitx=0,fruity=0,gameover=0;
char ch;
int tailx[100],taily[100],counttail=0;
void left();
void up();
void right();
void down();
void draw();
void fruit(); // this is to generate fruit
void input_detection( );
int main(){
gotoxy(pos1,pos2);
while (1){
srand(time(NULL)); // this only works at the restart
printf("%d",gameover); // this is to check the value of game over over which i am having errors
draw();
hidecursor();
input_detection();
Sleep(100);
}
return 0;
}
void input_detection(){
//if (gameover=1){ this was meant to be the code to end the game which dosenot work
//exit(0);
//}
if (_kbhit()){
ch=_getch();
}
if (ch==UP)
up();
if (ch==DOWN)
down();
if (ch==LEFT)
left();
if (ch==RIGHT)
right();
if(pos1==fruitx && pos2==fruity){
fruit();
counttail++;
}
int prevx=tailx[0],prevy=taily[0];
int prev2x,prev2y;
tailx[0]=pos1;
taily[0]=pos2;
for(int i=1;i<=counttail;i++){
prev2x=tailx[i];
prev2y=taily[i];
tailx[i]=prevx;
taily[i]=prevy;
prevx=prev2x;
prevy=prev2y;
}
for(int i=0;i<counttail;i++){
if(pos1==tailx[i] && pos2==taily[i]) gameover++;// this is the part where it tells if it hits its own body the code is not working so added ++ to count the error
}
}
void up(){
pos2-=1;
gotoxy(pos1,pos2);
printf("0");
if(pos2==0) pos2=30;
}
void left(){
pos1-=1;
gotoxy(pos1,pos2);
printf("0");
if(pos1==0) pos1=60;
}
void right(){
pos1+=1;
gotoxy(pos1,pos2);
printf("0");
if(pos1==60) pos1=1;
}
void down(){
pos2+=1;
gotoxy(pos1,pos2);
printf("0");
if(pos2==30) pos2=0;
}
void draw(){
int i,j;
system("cls");
srand(time(NULL));
if (fruitx==0)
fruitx=rand()%60;
if (fruity==0)
fruity=rand()%30;
gotoxy(fruitx,fruity);
printf("%c",147);
for(int t=0;t<counttail;t++){
gotoxy(tailx[t],taily[t]);
printf("o");
}
for(i=0;i<=60;i++){
for(j=0;j<=30;j++){
if(i==0 || j==0 || i==60 ||j==30){
gotoxy(i,j);
printf("%c",176);
}
}
}
}
void fruit(){
srand(time(NULL));
if (fruitx==0)
fruitx=rand()%60;
if (fruity==0)
fruity=rand()%30;
}

Not able to figure out the logical error in my code

There is some logical error in my code but I'm not able to figure out what it is. When I run my code it doesn't give me desired results.
OUTPUT:
Enter an infix expression
2+3
2
I get 2 as my postfix expression whereas I should be getting 23+.
Please see if anyone could help me out with this one.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
#define max 20
int top=-1;
char infix[max],postfix[max],stack[max];
char pop();
int empty();
void push(char );
int precedence(char );
void infix_to_postfix(char * ,char * );
void main()
{
clrscr();
printf("Enter an infix expression\n");
gets(infix);
infix_to_postfix(infix,postfix);
printf("%s",postfix);
getch();
}
void infix_to_postfix(char infix[max],char postfix[max])
{
int i=0,j=0;
char value,x;
for(i=0;infix[i]!='\0';i++)
{
value=infix[i];
if(isalnum(value))
{
postfix[j]=value;
j++;
}
else
{
if(value=='(')
push('(');
else
{
if(value==')')
{
while((x=pop())!='(')
{
postfix[j]=x;
j++;
}
}
else
{
while(precedence(value)<=precedence(stack[top])&&!empty())
{
x=pop();
postfix[j]=x;
j++;
}
push(value);
}
}
}
}
while(!empty())
{
x=pop();
postfix[j]=x;
j++;
}
postfix[j]='\0';
}
void push(char x)
{
if(top==max-1)
printf("stack overflow\n");
else
{
top++;
stack[top]=x;
}
}
char pop()
{
char x;
x=stack[top];
top--;
return x;
}
int empty()
{
if(top==-1)
return(0);
return(1);
}
int precedence(char value)
{
if(value=='(')
return(0);
if(value=='+'||value=='-')
return(1);
if(value=='*'||value=='/'||value=='%')
return(2);
return(3);
}
Generally, the logic of your code is OK except a return value mistake in the empty() function.
In the empty(), it should return 1 while stack is empty.
int empty(){
if(top==-1)
return(0); <-- !!! here should return 1
}
Otherwise,
it will go into the while loop at while(precedence(value)<=precedence(stack[top])&&!empty()) even when stack is empty.
And then postfix[j] = x may write a redundant 0(now top= -2) to postfix array.
Finally,under the input 2+3, the postfix[] will be {'2','\0','3','+',...}, which result in the abnormal output 2.
So, it will work after modifying the empty() function, such as
int empty()
{
if(top==-1)
return(1); // not return(0)
return(0);
}
Output:
Enter an infix expression
2+3
23+
all you need to do is change your empty() function
you are checking for !empty() in if condition, but you are returning 0, when it is empty, this will make the condition true, i.e. Stack is not empty. Change the return value to 1 in case of empty stack.

Swap using only two variables not working

I was writing a C code for quick sort but something went wrong. After some debugging i finally found where my code was going wrong.
When i replaced
{
a[lp]+=a[ub];
a[ub]=a[lp]-a[ub];
a[lp]=a[lp]-a[ub];
}
with
{
tmp=a[lp];
a[lp]=a[ub];
a[ub]=tmp;
}
my code started working.
I am Curious to know why my initial implementation of swapping didn't work?
Can anyone help me?
#include<stdio.h>
#define swap(a,b) (a)=(a)+(b);b=(a)-(b);(a)=(a)-(b);
int a[]={7,1,5,2,3};
int partition(int lb,int ub)
{
int k,hp,lp;
k=a[ub];
lp=lb-1;
for(hp=lb;hp<ub;hp++)
{
if(a[hp]<k)
{
lp++;
int tmp=a[lp];
a[lp]=a[hp];
a[hp]=tmp;
}
}
lp++;
a[lp]+=a[ub];
a[ub]=a[lp]-a[ub];
a[lp]=a[lp]-a[ub];
return lp;
}
void quicksort(int lb,int ub)
{
if(lb<ub)
{
int pos=partition(lb,ub);
quicksort(lb,pos-1);
quicksort(pos+1,ub);
}
}
int main()
{
quicksort(0,4);
int i;
for(i=0;i<5;i++)printf("%d ",a[i]);
printf("\n");
return 0;
}
You need to consider what happens when lp == ub (ie. you are being asked to swap an element with itself).
Change it to this:
if (lp != ub) {
a[lp]+=a[ub];
a[ub]=a[lp]-a[ub];
a[lp]=a[lp]-a[ub];
}
Example: http://ideone.com/AS1Dgf

please help me to find Bug in my Code (segmentation fault)

i am tring to solve this http://www.spoj.com/problems/LEXISORT/ question
it working fine in visual studio compiler and IDEone also but when i running in SPOJ compiler it is getting SEGSIGV error
Here my code goes
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char *getString();
void lexisort(char **str,int num);
void countsort(char **str,int i,int num);
int main()
{
int num_test;
int num_strings;
char **str;
int i,j;
scanf("%d",&num_test);
for(i=0;i<num_test;i++)
{
scanf("%d",&num_strings);
str=(char **)malloc(sizeof(char *)*num_strings);
for(j=0;j<num_strings;j++)
{
str[j]=(char *)malloc(sizeof(char)*11);
scanf("%s",str[j]);
}
lexisort(str,num_strings);
for(j=0;j<num_strings;j++)
{
printf("%s\n",str[j]);
free(str[j]);
}
free(str);
}
return 0;
}
void lexisort(char **str,int num)
{
int i;
for(i=9;i>=0;i--)
{
countsort(str,i,num);
}
}
void countsort(char **str,int i,int num)
{
int buff[52]={0,0},k,x;
char **temp=(char **)malloc(sizeof(char *)*num);
for(k=0;k<52;k++)
{
buff[k]=0;
}
for(k=0;k<num;k++)
{
if(str[k][i]>='A' && str[k][i]<='Z')
{
buff[(str[k][i]-'A')]++;
}
else
{
buff[26+(str[k][i]-'a')]++;
}
}
for(k=1;k<52;k++)
{
buff[k]=buff[k]+buff[k-1];
}
for(k=num-1;k>=0;k--)
{
if(str[k][i]>='A' && str[k][i]<='Z')
{
x=buff[(str[k][i]-'A')];
temp[x-1]=str[k];
buff[(str[k][i]-'A')]--;
}
else
{
x=buff[26+(str[k][i]-'a')];
temp[x-1]=str[k];
buff[26+(str[k][i]-'a')]--;
}
}
for(k=0;k<num;k++)
{
str[k]=temp[k];
}
free(temp);
}
Generally speaking, these online judge programs give an example input (in this case, that input appears to work perfectly), but also use a set of harder hidden inputs.
In this case, what happens if the input string has a space in it? For example, an input of:
1
2
hello orld
whateverss
In this case, your scanf("%s",str[j]); will not properly read that input line. I'd suggest switching over to a getline style interface, rather than a scanf style interface.

Linker can't find initgraph() and closegraph()?

The linker gives error that overflow at initgraph and close graph
#include<dos.h>
#define DETECT 0
union REGS in,out;
void detectmouse()//no declaration(prototype)?
{
in.x.ax=0;
int86(0x33,&in,&out);
if(out.x.ax==0)
{
printf("Fail to initialize the mouse.");
}
else
{
printf("Mouse succesfully initialized.");
}
getch();
}
void showmousegraphics()//show mouse in graphics mode
{
int driver=DETECT,mode;
initgraph(&driver,&mode,"c:\\tc\\bgi");
in.x.ax=1;
int86(0X33,&in,&out);
getch();
closegraph();
}
void main()
{
detectmouse();
showmousegraphics();
}
Ahh, you're probably using some old compiler for DOS. In that case, you have to choose large memory model when compiling, so that you can have more than 64kB of code.

Resources