Hello ppl

I am new to c++ and especially a noob in graphics. All the functions I have learnt are from Help File in Turbo c++ 3.0. Since this part of c++ is not in my course, I am having difficulties learning it.

Now, in the following code the outtext function is not displaying the string passed as argument to it. Instead it seems that it is printing it below the rectangle.

Code:

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<dos.h>

void boxout(int xstart,int xend,int ystart,int yend,char item[])
 {
  setcolor(BLACK);
  moveto(xstart,ystart);
  outtext(item);

  setcolor(LIGHTGRAY);
  setfillstyle(SOLID_FILL,LIGHTGRAY);
  rectangle(xstart,ystart,xend,yend);
  floodfill(xstart+1,ystart+1,LIGHTGRAY);

  for(int i=xstart;i<=xend;i++)
   {
    putpixel(i,ystart,WHITE);
    putpixel(i,yend,DARKGRAY);
   }

  for(i=ystart;i<=yend;i++)
   {
    putpixel(xstart,i,WHITE);
    putpixel(xend,i,DARKGRAY);
   }

 }


void main()
{
 int gdriver=DETECT,gmode,error;

 initgraph(&gdriver,&gmode,"");

 error=graphresult();

 if(error!=grOk)
  {
   cout<<"Graphics error exiting!";
   exit(-1);

  }

 boxout(1,60,1,20,"Hello");
 boxout(61,120,1,20,"World");
 setcolor(BLACK);
 settextstyle(SANS_SERIF_FONT,HORIZ_DIR,1);
 getch();
 closegraph();

}

Plz need help

Try relocating the outtext(item) call ...

void boxout(int xstart,int xend,int ystart,int yend,char item[])
 {
  setcolor(BLACK);
  moveto(xstart,ystart);

  setcolor(LIGHTGRAY);
  setfillstyle(SOLID_FILL,LIGHTGRAY);
  rectangle(xstart,ystart,xend,yend);
  floodfill(xstart+1,ystart+1,LIGHTGRAY);

  for(int i=xstart;i<=xend;i++)
   {
    putpixel(i,ystart,WHITE);
    putpixel(i,yend,DARKGRAY);
   }

  for(i=ystart;i<=yend;i++)
   {
    putpixel(xstart,i,WHITE);
    putpixel(xend,i,DARKGRAY);
   }

  outtext(item);
  }

Nope, it doesnt work, instead some of the text's pixels are coming in the borders of the rectangle, i.e it looks corrupted on its sides only. Do you reckon that it has to do something with floodfill?

Nope, it doesnt work, instead some of the text's pixels are coming in the borders of the rectangle, i.e it looks corrupted on its sides only.

Hmm, sorry but I don't fully understand that one. Perhaps you could try the following ...

void boxout(int xstart,int xend,int ystart,int yend,char item[])
{
  moveto(xstart,ystart);
  
  setcolor(LIGHTGRAY);  
  setfillstyle(SOLID_FILL,LIGHTGRAY);
  rectangle(xstart,ystart,xend,yend);
  floodfill(xstart+1,ystart+1,LIGHTGRAY);

  // at this point you should have a filled LIGHTGRAY rect

  for(int i=xstart;i<=xend;i++)
   {
    putpixel(i,ystart,WHITE);
    putpixel(i,yend,DARKGRAY);
   }

  for(int i=ystart;i<=yend;i++)
   {
    putpixel(xstart,i,WHITE);
    putpixel(xend,i,DARKGRAY);
   }

  // lastly, output the text
  setbkcolor(LIGHTGRAY);
  setcolor(BLACK);
  outtext(item); 
}

Then lastly, I think you really should consider getting yourself a newer development system, like MS VC 200x Express / Borland C++ Builder, which are available for free.

Well, the above didnt work. To look at that output see attachment.

And anyway latest Borland C++ compilers dont support BGI graphics. The program could be written only in Turbo C++ 3.0.

Well, the above didnt work. To look at that output see attachment.

Sorry, but I'll have to give up on that one then.

And anyway latest Borland C++ compilers dont support BGI graphics.

About upgrading your compiler, the main point is that you should give up on an antique compiler in case you are into modern computing.

The program could be written only in Turbo C++ 3.0.

I actually tried that one on Dev-C++ with Winbgi library and it worked for me :).

Ok let me make it clear. Actually I am a student in school and I am quite new to C++, say 1 year. I am learning this stuff for my project and facing only this minute(but critical) problem.
Another problem is that my teacher will never allow me to learn in Dev-C++ because this project needs to be shown in either Borland C++ 5.5/4.5(which dont support BGI) or the antique Turbo 3.0

However I am starting to infer that there does seems to be some kind of a problem with floodfill. Will using some other function for similar use like putpixel(..) ,work?

If you have the BGI documentation, then go through it, and learn how the functions you are using (all of them) indicate an error condition of any kind. If you don't have it, then you might want to take look in
http://www.cs.colorado.edu/~main/cs1300/doc/bgi/ to have an idea of how the BGI works.

Then add code that checks for any error(s) that might have occurred. This way you may find out the root cause.

Well, good news, the program finally worked, not that it was showing any error but with a different combination. Here is new code:

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<dos.h>

void boxout(int xstart,int xend,int ystart,int yend,char item[])
 {
 
  moveto(xstart+4,ystart+6);

  setcolor(LIGHTGRAY);
  setfillstyle(SOLID_FILL,LIGHTGRAY);
  rectangle(xstart,ystart,xend,yend);
  floodfill(xstart+1,ystart+1,LIGHTGRAY);
  floodfill(100,100,YELLOW); //For background color
  for(int i=xstart;i<=xend;i++)
   {
    putpixel(i,ystart,WHITE);
    putpixel(i,yend,DARKGRAY);
   }

  for(i=ystart;i<=yend;i++)
   {
    putpixel(xstart,i,WHITE);
    putpixel(xend,i,DARKGRAY);
   }
  setcolor(BLACK);
  outtext(item);

 }


int main()
{

 int gdriver=DETECT,gmode,error;

 initgraph(&gdriver,&gmode,"");

 error=graphresult();

 if(error!=grOk)
  {
   cout<<"Oops!";
   exit(-1);

  }


 boxout(1,60,1,20,"Hello");
 boxout(61,120,1,20,"World");
 setcolor(BLACK);
 settextstyle(SMALL_FONT,HORIZ_DIR,1);
 moveto(1,1);
 getch();
 delay(1000);
 closegraph();
 return 0;

}

However it was weird to see the code not working at all when setbkcolor(LIGHTGRAY) was used. Weird!

And i got reason for that erraneous code at the top(by u), incase no one noticed( even me until now) that setcolor(GRAY) was active throughout, instead setcolor(BLACK) should have been before outtext(item).
So outtext after rectangle worked, cant see why didnt work when setbkcolor was used.

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.