我正在尝试为类测试的每个对象创建一个栏。有一个私有变量,状态。如果条形的status == 0
值变为红色,则将颜色更改为绿色。在for循环的多个输入之后,程序的图形窗口崩溃。请帮助我在哪里做错了?
/* bar example */
#include <iostream>
#include <graphics.h>
using namespace std;
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
class test
{
private:
int x;
public:
test()
{
}
int getX()
{
if(x==0)
{
cout << "Room Empty";
}
else
if (x==1)
{
cout << "Room Occupied";
}
return x;
}
void setX(int check)
{
x=check;
this->update();
}
void update()
{
//Block to change the colour of the bar on the screen
if(x==0)
{
setfillstyle(INTERLEAVE_FILL,RED);
bar(100,100,20,20);
}
else
if(x==1)
{
setfillstyle(INTERLEAVE_FILL,BLUE);
bar(100,100,20,20);
}
}
};
int main(void)
{
/* request autodetection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy, i;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) { /* an error occurred */
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(0); /* terminate with an error code */
}
int x;
test object;
for(int i=0;i<20;i++)
{
cout << "Enter 0 for red, 1 for Blue: ";
cin >> x;
object.setX(x);
}
//getch();
/* clean up */
closegraph();
return 0;
}