So you're telling me that calling getch() in a loop to build up a string, one character at a time doesn't work?

I am sorry,I'm a beginner in C++ so I don't know much.. But could you tell me how to do it???

But then what is the difference between getch() and getchar()? Because even getchar is used to input one character at a time.

I am desperate to get help in graphics because I am making a chess program and I need all these..

Since I'm a beginner I've tried using what I know.
Because people tell me to learn Windows API to enhance your application but that is totally a new chapter.

whereas in graphics.h you just have to learn some new function and how to use them.

I heard there is a C++ Compiler Code::Blocks, Is this good? Does Graphics.h work in it?

Because I have borland c++ 5.5 but that does not support graphics, so I use Turbo C++ 3.0 the DOS version.

> I heard there is a C++ Compiler Code::Blocks, Is this good?
No, it's an editor which doesn't care about which compiler you use.
Though most people seem to use it with some version of gcc.

> Does Graphics.h work in it?
That belongs to your compiler, if your compiler writer chose to supply it.

We're still waiting for you to post some attempt using getch()

But how do you intput a string using getch()? I don't know how to do that.

Is it something like this

for(int i=0;i<n;i++)
{
  c = getch();
}

c = getch();
But can you store the string in c (variable)??

Yes, use an array of chars

char string[100];
for(int i=0;i<100-1;i++)
{
    string[i] = getch();
    string[i+1] = '\0';
}

Why a null terminator after each character input? I guess it was a typo.

char string[100];
for(int i=0;i<100-1;i++)
{
    string[i] = getch();
}
string[i+1] = '\0';

Yeah it works. I can input text in graphics mode. But there is one thing.

I wrote the code like this

#include<iostream.h>
#include<conio.h>

int main()
{
  int driver = DETECT,mode;
  initgraph(&driver,&mode,"\\tc\\bgi");
  
  char string[25];
  for(int i=0;i<24,i++)
  string[i] = getch();

  string[25] = '/0';
  outtext(string);
  getch();
  closegraph();
}

I inputed the string but at string[24] and string[25] it displayed something like # some wierd thing. Why is that?

You should try to copy and paste the exact code provided to you, at least till you get the concepts right.

I pasted:

for(int i=0;i<24-1;i++)
{
    string[i] = getch();
}

You wrote:

for(int i=0;i < 24,i++)
  string[i] = getch();

Don't you think these two are different?

I got the concept. You declared a array of size 100.
In the for loop you wrote

for(i=0;i<100-1,i++)
Meaning it inputes the string untill the 99th element.

I did the same thing. I declared an array of size 25 and told it directly to input the text till 24. what difference does it make?

Why a null terminator after each character input? I guess it was a typo.

char string[100];
for(int i=0;i<100-1;i++)
{
    string[i] = getch();
}
string[i+1] = '\0';

And yours has them too. Wrong location for the c-string terminator

char string[100];
for(int i=0;i<99;i++)
{
    string[i] = getch();
}
string[i] = '\0';

So which is correct?

I am sorry I highlighted the wrong part.

The statement you ought to change is: string[25] = '/0'; . It should be string[25] = '\0'; , which is a null terminator.

Thanks for correcting my mistakes. It works perfectly.

class shape
            {
                public:  void set(int x,int y, int fillcolor)
                             {
                                 rectangle(x,y,x+20,y+20);
                                 floodfill(x+5,y+5,RED);
                                 setfillstyle(SOLID_FILL,WHITE);
                             }
            };

int main()
{
   int driver = DETECT,mode;
   initgraph(&driver,&mode,"\\tc\\bgi");    

   shape s1,s2;
   s1.set(100,120,15);
   s2.set(100,160,15);

   s1.set(140,140,15); 

   getch();
   closegraph();

}

The problem here is that when I set the coordinates of s1 again the shape s1 can still be seen in its orginal location and also at its new location.

How do I make it such that it appears only at its new coordinate???

I'm trying to find out about this.
I can't use a clrscr() function because you see in the chess program I mentioned if I use this function then the whole board will be erased.

But I just want to remove that particular object from that location.

Like I said before, you need to read the function reference which is inbuilt in Turbo C to find it out since I have never worked with such things. Read something related to view ports concepts in Turbo C.

You have two choices:
- Use clrscr() and redraw the entire board with the updates
- Clear a specific portion of the screen using some functions in BGI.

I think I have found a solution to this problem.

By creating a destructor in the class, when an object like say Pawn is moved from one square to another I can destroy it at the original location and create a new one at the new location thereby it will appear as if that pawn moved from one square to another.

By destructing the object I think the shape will also be erased.

I might get some small problems while I write the chess program. Can I create a new topic so that I can clear them?

With this I will close this topic.

No, that won't work. Destruction of a class object has got nothing to do with the display. You would anyways need to actually 'erase' the thing which is on the screen. Thus your logic would consist of two parts: Game logic and graphics.

When the pawn is moved, you need to call the move() function on the object as well as repaint the screen instead of just destroying the object.

I could do that but the problem is when I erase the whole screen I have set the position of the other objects also.

Or in short each time a object is moved I have to initalize everything again.

Every object you create can have a draw method, which will know how to render itself on the screen. So you just need to call the draw method of each object in a loop for all objects in the game. The object will have its own information like color, position etc. so you don't need to worry about the location of drawing.

I tried the clrscr() function.But the screen is half filled with grey color.This is the code I wrote

#include<graphics.h>

class shape
{
  public:
 
   void set(int x,int y,int color)
   {
      rectangle(x-15,y-20,x,y);
      floodfill(x-10,y-10,WHITE);
      setfillstyle(SOLID_FILL,BLUE);
   }
};

void board()
{
  rectangle(5,20,60,75);
  floodfill(30,50,WHITE);
  setfillstyle(SOLID_FILL,BLACK);
}

int main()
{
  shape s1;
  board();
  s1.set(30,50,15);
  getch();
  clrscr();
  board();
  s1.set(30,100,15);
}

I found a solution to this.
Clrscr(); function does not work well in graphics mode.

But cleardevice(); function works perfectly!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.