I am just trying to get the all keyboard keys state. For that I wrote this simple program for trial.
I have made a separate thread for updating keystate.
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <pthread.h>
BYTE keys[256];
void *Thread(void *vargp);
void gotoxy(int x, int y);
COORD coord;
int main(){
pthread_t KEYSThread;
pthread_create(&KEYSThread,NULL,Thread,NULL);
while(1){
for(int i=0x41;i<=0x5A;i++){
printf("Key State %c: %d\n",i,keys[i]);
}
if(keys[0x31]){
if(keys[0x32]){
exit(0);
}
}
gotoxy(0,0);
}
}
void *Thread(void *vargp){
while(1){
GetKeyState(0);
//This is done to update the Keyboard buffer
//which in many cases windows won't do itself .. might be some bugs
GetKeyboardState(keys);
}
}
void gotoxy(int x, int y)
{
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
Now when running initially it is all set to zero.
It shows the state of Keys A-Z keys.
As I press the keys the keys[] becomes 1 which is desirable but after I lift it should have gone to 0 but seems that is not the case.
Only after I press the keys again will the keys[i] go to 0.
It seems like GetKeyboardState() is identifying every keys as toggle buttons.
Can any one help me with this problem.
If you can suggest me some alternatives to GetKeyboardState(), please present a link to where I can learn about it (or syntax of the command).
OTHER THAN GetAsyncKeyState(),etc.
I want the state data to be transferred in a bulk rather than one by one.
So I am a beginner in C and I was trying to use functions in my programs but I cant figure out why my function isn't returning 1 or 0, and if I put a printf("Hello"); in the middle of the loop in the function it isn't appearing in the console. Any help would be appreciated!
int Primo(int x);
int main() {
Ex4_MED_4();
}
void Ex4_MED_4(){
int prime,number;
printf("Pick a number:");
scanf("%d",number);
prime=Primo(number);
printf("%d",prime);
}
int Primo(int x){
for(int i=2;i<=x/2;i++){
if (x%i==0) {
return 0;
}
}
return 1;
}
I don't know how to mark a comment has a solution, but thanks to #Eugene Sh. I was able to see I am brain dead and forgot to put a & in the scanf(). So scanf("%d",number) --> scanf("%d",&number). If someone could tell me how to make him the top answer would appreciate!
Good evening everyone!
I have started messing around with strings and pointers in C.
I want to write a programm that reads a text file, then calculating the frequency of each word and printing it.
My variables are:
FILE *fp;
char *words[N] //N defined 100
int i=0, y=0;
int *freq;
int freq_count=0;;
int word_number=0;
The code part:
for(i=0;i<word_counter;i++){
while(y<word_counter){
if(strcmp(words[i],words[y]==0){
freq1++;
} y++;
}
if(i==0){
freq=(int*)malloc(sizeof(int));
strcpy(freq, freq1); freq1=0;
}
else{
freq=(int*)realloc(freq, (i+1)*sizeof(int));
strcpy(freq, freq1); freq1=0;
}
y=0;
}
I get several errors running this...What is wrong?
Take into consideration that in words[N] i have put each word of the text by itself in each cell.
Thank you all in advance.
Maybe another array is not what you want, but still better than using realloc and condition in loop.
int freq[N];
for(i=0;i<word_counter;i++){
freq1 = 0;
for(y=0;y<word_counter;y++){
if(strcmp(words[i],words[y]==0)
freq1++;
}
freq[i] = freq1;
}
I recently discovered winBGIm libraries, and I find it very useful to learn to code, so I started creating something to get some practice but I got stuck with my program.
The program should show a little ball and two rectangles approaching to the ball itself, and the player can move the ball up and down simply by pressing a button on the keyboard. Initially, I wrote everything in the main to make it fast, but since this is terrible I divided the program into functions.
First of all, even when everything was together, the getch function seemed to not work, because, while it should wipe the input buffer for the kbhit function, it made the ball not move at all, while the mere kbhit function worked, but obvioulsy the ball continued going up even when you stopped pressing a key. I used the same procedure in another text-only program and it worked very well, so I don't know where the problem is.
The second and most important problem is that, after splitting the program into functions, it became static, since the main loop which would make the graphic move stops at the end of the first iteration. It only restarts working when I delete the cleardevice function at the end of the loop and I disable the duble buffering in the initwindow function, but I can't understand the relationship between these things.
Finally, when I set a new background color, if it is not 0 (black), the window remains completely black.
I hope someone can help me.
Best regards, Giacomo.
#include <graphics.h>
#include <time.h>
#include <stdio.h>
int rettangoli(int b);
void bird(int x_default, int y_default);
void bird();
int main() {
int a=640;
int b=480;
int x_default=150;
int y_default=400;
int verifica=0;
srand(time(NULL));
initwindow(a, b, "BGI", 0, 0, true, true);
setbkcolor(0);
while(1) {
bird(x_default, y_default);
verifica=rettangoli(b);
if(verifica==1) {
outtextxy(0, 0, "HAI PERSO");
outtextxy(0,20, "PREMERE UN TASTO PER CONTINUARE");
break;
}
delay(50);
while(kbhit()) {
getch();
}
swapbuffers();
cleardevice();
}
delay(350);
getch();
closegraph();
return 0;
}
void bird(int x_default, int y_default) {
static int x_pos=x_default;
static int y_pos=y_default;
if(kbhit()) {
if(y_pos-16>0)
y_pos=y_pos-5;
circle(x_pos, y_pos, 16);
setfillstyle(SOLID_FILL, YELLOW);
floodfill(x_pos, y_pos, 15);
}
else {
while(y_pos<400)
y_pos=y_pos+15;
circle(x_pos, y_pos, 16);
setfillstyle(SOLID_FILL, YELLOW);
floodfill(x_pos, y_pos, 15);
}
return;
}
int rettangoli(int b) {
static int x_rett=615;
int verifica=0;
int contatore;
int altezza;
if(x_rett==615)
altezza=rand()%(b-60)+1;
rectangle(x_rett, 0, x_rett+25, altezza);
rectangle(x_rett, altezza+60, x_rett+25, 480);
setfillstyle(SOLID_FILL, GREEN);
floodfill(x_rett+1, 1, 15);
floodfill(x_rett+1, 479, 15);
for(contatore=0;contatore<altezza && x_rett>100;contatore++) {
if(getpixel(x_rett-1, contatore)!=getbkcolor())
verifica=1;
}
for(contatore=altezza+60;contatore<b && x_rett>100;contatore++) {
if(getpixel(x_rett-1, contatore)!=getbkcolor())
verifica=1;
}
for(contatore=0;contatore<15;contatore++) {
if(getpixel(contatore, altezza)!=getbkcolor())
verifica=1;
}
for(contatore=0;contatore<15;contatore++) {
if(getpixel(contatore, altezza+60)!=getbkcolor())
verifica=1;
}
x_rett=x_rett-5;
printf("%i", x_rett);
return verifica;
}
Nevermind, I solved the problem on my own. There was an incompatibility issue between Windows 8 x64 and Codeblocks-EP, and I think there is a general incompatibility between winBGIm and x64 systems.
I created a virtual machine using Vmware Player and Windows XP x86 and now everything is fine.
i'm trying to do a simple bouncing ball code ,my code below doesn't bounce the ball,it just makes it move when the enter button is used, what can I do to make the ball bounce by it self when the program is run?
#include<alloc.h>
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
void main()
{
int d=DETECT,m;
initgraph(&d,&m,"H:\\tc\\bgi");
int l=getmaxx()/2,t=0;
int x=1,y=1;
int xstep=1,ystep=1;
while(!kbhit())
{
cleardevice();
circle(l,t,18);
delay(5);
circle(l,t,18);
if(l>=getmaxx()||l<=0)
{
x*=-1;
xstep=x*(random(4)+1);
ystep=y*(random(3)+1);
if (l<=0)
t=0;
else
l=getmaxx();
}
if(t>=getmaxy()||t<=0)
{
y*=-1;
ystep=(y*random(4)+1);
xstep=(x*random(3)+1);
if(t<=0)
t=0;
else
t=getmaxy();
}
l+=x+xstep;
t+=y+ystep ;
getch();
}
closegraph();
}
I will recommend you to make a few changes at the earliest.
Don't use getch() in the while loop.
Try increasing and decreasing the values of the parameters of the
delay() function.
Try ellipse instead of circle.