-
Borland...
I just bought Borland C++ 5.02 and it took me foever to figure out how to compile and run a simple program. I cant figure ou how to use graphics.h and kbhit()...
Ive used examples ive found in the forums but they will not work and call a warning about converting char to int.
-
Do you have a question? I sure don't see one
-
ok....
Why wont kbhit() work...
Code:
#include <iostream.h>
#include <conio.h>
char grid[3][3] = {
{'*','*','*'},
{'*','*','*'},
{'*','*','*'}};
char key;
int x;
int y;
int count = 0;
char changex;
char changey;
void makegrid();
void plyrmve();
void change_grid(const char changex, const char changey);
int main()
{
grid[0][0] = 'X';
for(count = 0; count < 9; count++){
makegrid();
plyrmve();
clrscr();
}
getch();
return 0;
}
void makegrid()
{
cout<<grid[0][0]<<grid[0][1]<<grid[0][2]<<endl
<<grid[1][0]<<grid[1][1]<<grid[1][2]<<endl
<<grid[2][0]<<grid[2][1]<<grid[2][2]<<endl;
}
void change_grid(const char changex, const char changey)
{
grid[x][y] = ' ';
if ( changex == '+' ) x++;
else if ( changex == '-') x--;
else if ( changey == '+') y++;
else if ( changey == '-') y--;
grid[x][y] = 'X';
}
void plyrmve()
{
if ( kbhit() ){
key = getch();
switch( key )
{
case 72: change_grid ( '-', 0 ); break;
case 80: change_grid ( '+', 0 ); break;
case 75: change_grid ( 0, '-' ); break;
case 77: change_grid ( 0, '+' ); break;
default: cout<<"Error";
}
}
}
it says may lose significant digits...
-
Bah... Stupid code blocks wont align it right so... sorry for the sloppiness.
-
I believe it is because your change_grid function is prototyped to take char and in your switch statement you are passing an integer as an argument. The compiler is letting you know that when it does the type cast you may lose some accuracy, this isn't a problem in your case however and the warning shouldn't stop you from compiling and linking the program.
-
when I compile it and run it it just goes to the getch() func and doesnt even make the grid.
-
Are you sure, because according to your code it will cycle throw the for loop, clearing the screen each time, it never seems to wait for the user to press a key. Yes it does check for it, but it doesn;t wait for it. Then the last thing the for loop does is clear the screen before going to the getch() function........
-
oh i see...
What header do i need for pause_for_input?
would that stall it?
-
Just put a while statement in your plyrmve() function like this:
Code:
while(!kbhit());
//then rest of code minus if statement
-
sigh...
Well if i press anything now... it just closes
Code:
#include <iostream.h>
#include <conio.h>
#include <time.h>
char grid[3][3] = {
{'*','*','*'},
{'*','*','*'},
{'*','*','*'}};
char key;
int x;
int y;
int count = 0;
char changex;
char changey;
void makegrid();
void plyrmve();
void change_grid(const char changex, const char changey);
int main()
{
grid[0][0] = 'X';
for(count = 0; count < 9; count++){
makegrid();
plyrmve();
clrscr();
}
getch();
return 0;
}
void makegrid()
{
cout<<grid[0][0]<<grid[0][1]<<grid[0][2]<<endl
<<grid[1][0]<<grid[1][1]<<grid[1][2]<<endl
<<grid[2][0]<<grid[2][1]<<grid[2][2]<<endl;
}
void change_grid(const char changex, const char changey)
{
grid[x][y] = ' ';
if ( changex == '+' ) x++;
else if ( changex == '-') x--;
else if ( changey == '+') y++;
else if ( changey == '-') y--;
grid[x][y] = 'X';
}
void plyrmve()
{
while( !kbhit() ){
key = getch();
switch( key )
{
case 72: change_grid ( '-', 0 ); break;
case 80: change_grid ( '+', 0 ); break;
case 75: change_grid ( 0, '-' ); break;
case 77: change_grid ( 0, '+' ); break;
default: cout<<"Error";
}
}
}
-
I'm sorry you must have misunderstood me. the code should be like this:
Code:
while(!kbhit()); //wait for key press
key = getch();//when key is pressed continue with code
switch( key )
{
case 72: change_grid ( '-', 0 ); break;
case 80: change_grid ( '+', 0 ); break;
case 75: change_grid ( 0, '-' ); break;
case 77: change_grid ( 0, '+' ); break;
default: cout<<"Error";
}
-
THANK YOU :D
Now on the 5th move it closes....
Why is that?
-
Sorry about the delay, my daughter woke up. Anyhow here you go:
Code:
for(count = 0; count < 9; count++){
makegrid();
getch();
plyrmve();
clrscr();
}
Key codes actually return two values,kbit() was seeing two values every time the loop went around therefore for every move you make the loop actually executed twice.
-
YAY!!!
Thanks Traveller,
Now i need to figure out what to make with this "engine".
I would really like to figure out how to use graphics.h to input graphics into my program.
I tried to make a circle with this header but it said BGI is not supported under windows, what does that mean?