Graphics in functions

I've started experimenting with graphics.h and noticed that when I draw lines/shapes/etc. in a function outside of the main, that it draws to the screen the object, but also leaves a big gray block about 2/3 or the screen. Was wondering if there is any way to fix this. P.S. I'm using Turbo C++ because it's "real C++" according to my teacher.
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<conio.h>
#include<iomanip.h>
#include<stdlib.h>
#include<graphics.h>
#include<stdio.h>
class Piece{
	public:
		Piece();
		int move(int,int);
		void setx(int);
		void sety(int);
	private:
		int xvalue;
		int yvalue;
};
Piece::Piece(){
	xvalue=yvalue=0;
}
void Piece::setx(int xval){
	xvalue=xval;
}
void Piece::sety(int yval){
	yvalue=yval;
}
void board(){
	clrscr();
	int x=50;
	int y=30;
	for (int i=0;i<10;i++){
		x=50;
		for (int p=0;p<6;p++){
			rectangle(x,y,x+40,y+40);
			x=x+40;
		}
		y=y+40;
	}
	outtextxy(65,10,"1    2    3    4    5    6");
	outtextxy(35,50,"1");
	outtextxy(35,90,"2");
	outtextxy(35,130,"3");
	outtextxy(35,170,"4");
	outtextxy(35,210,"5");
	outtextxy(35,250,"6");
	outtextxy(35,290,"7");
	outtextxy(35,330,"8");
	outtextxy(35,370,"9");
	outtextxy(25,410,"10");
}
void main(){
	Piece white[12];
	Piece red[12];
	int gd=DETECT,gm,error;
	initgraph(&gd,&gm,"C:\TC\BGI");
	error=graphresult();
	if (error != grOk){
		cout<<"There was an error of "<<grapherrormsg(error);
		getch();
		exit(1);
	}
	board();
	int key=0;
	int posx=70;
	int posy=50;
	do{
		key=getch();
                board();
		setfillstyle(SOLID_FILL,1);
		if ((key==77)&&(posx<270)){
			posx=posx+40;
			circle(posx,posy,5);
		}else if ((key==75)&&(posx>70)){
			posx=posx-40;
			circle(posx,posy,5);
		}else if ((key==72)&&(posy>50)){
			posy=posy-40;
			circle(posx,posy,5);
		}else if ((key==80)&&(posy<410)){
			posy=posy+40;
			circle(posx,posy,5);
		}
	}while (key!=13);
	clrscr();
	board();
	setfillstyle(SOLID_FILL,RED);
	circle(posx,posy,15);
	getch();
	closegraph();
}
Last edited on
[code] "Please use code tags" [/code]
Sorry, I don't know about the graphics library, so make sure that you are using correctly.
Look for something like 'refresh', 'clear' or 'swap buffers'
initgraph(&gd,&gm,"C:\TC\BGI"); That path looks wrong.

I'm using Turbo C++ because it's "real C++" according to my teacher.
Excuses
Its not a problem with the commands. I'm using clrscr() but whenever I print it to the screen I get this http://tinypic.com/r/1z4eczc/5 Also, the directory is correct. BGI holds all of the necessary files to use Graphics mode, otherwise you receive errors.
AFAIK you need to escape backslashes.

If you say that the code is correct, then the problem is in the environment. Try to create a minimal example.
Maybe your teacher could provide you with a 'real' OS. By the way ¿did you already make him eat his words talk to him?
I have talked with him. He likes Turbo because it uses char * and has looked at others but doesn't like how they look. Anyway I might be able to solve the issue in this compiler?
When you have backslashes in strings, the compiler thinks you want to escape something (like \n to escape the letter n and make a new line). If you have something that it doesn't recognize, it throws a fit. Either use \\ to escape the backslash, or use forward slashes / in the path (your computer won't care)
After I changed the file path, it still had the gray block.
Topic archived. No new replies allowed.