Hi!, I'm newbie here on daniweb.. and i dont know if i'm in a right place to post this problem. lol. *sorry for my english*
anyway, will you please help me about Turbo C? My teacher gve us a sample problem and he starts it with this code :

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
int gd,gm;
void main()
{
gd=DETECT;
initgraph(gd,gm,"");
if(graphresult()!=grOk){exit(1);}

rectangle(0,0,getmaxx(),getmaxy());
rectangle(1,1,getmaxx()-1,getmaxy()-1);
settextstyle(1,0,1);
outtextxy(50,50,"HELLO");
circle(400,600,10);
getch();
closegraph();
}

and when I tried to run it, it suddenly closed. and I don't know what is wrong with this. :'( help me! did I missed some codes to put on or any misspelled codes? pls check it and tell me what is the right code for this. (^_^)

and another one, my teacher wants us to make/draw us a "computer set (monitor,cpu,keyboard)" using turbo C. and i dont have any idea how to start with it. Do you have any codes for this problem? pls help me. :'(

thank you so much!

Dani AI

Generated

Short diagnosis and where to look first — the visuals are almost always failing at graphics initialization, not at the shape-drawing calls. Several people here already pointed in that direction: and flagged the init call, noticed the program exits silently, and correctly suggested printing errors instead of just quitting. The Borland Graphics Interface (BGI) loads device drivers and fonts from disk and will fail if the driver path, driver files, or initialization parameters are wrong. Borland Graphics Interface (BGI) — overview. (en.wikipedia.org)

Practical quick checks (do these in order):

  • Run the program inside DOSBox if you are on a modern 64‑bit Windows — Turbo C and BGI are DOS/16‑bit era and often won’t run natively on x64 systems. [Run Turbo C in DOSBox / compatibility notes]. (stackoverflow.com)
  • Don’t let the program call exit(1) silently. After your init call, query the graphics error code and print the message so you see exactly why init failed. A minimal diagnostic uses the library’s error-to-string function to show a human message (the BGI ports expose this). See the BGI documentation for the error string function. [WinBGIm / BGI function reference]. (winbgim.codecutter.org)

If you don’t want to deal with DOS/.BGI files, use a modern BGI port that runs with MinGW/Code::Blocks or SDL:

  • WinBGIm provides a Windows-compatible BGI layer you can link into MinGW/Code::Blocks. [WinBGIm project]. (winbgim.codecutter.org)
  • SDL_bgi is a cross‑platform BGI reimplementation based on SDL2. [SDL_bgi project]. (sdl-bgi.sourceforge.io)

For the “computer set” assignment: think in relative coordinates (use window width/height to scale), draw large outer rectangles for monitor and case, an inner filled rectangle for the screen area, a stacked rectangle for the CPU, and a grid of small rectangles for keys. Draw background shapes first, then smaller foreground details and text last. Use plain fills and borders — BGI is primitive but predictable.

Checklist to finish quickly: run under DOSBox if native fails, print the BGI error string instead of exiting, or move to WinBGIm/SDL_bgi to avoid BGI driver hassles. This follows the practical advice already posted by , and while avoiding the brittle DOS environment.

Recommended Answers

All 12 Replies

>>when I tried to run it, it suddenly closed

That's because there is nothing at the end of main() to prevent that behavior. If you want it to stay open so that you can see it, then add getche(); at the end of main(). The program will then stop until you hit a key.

>will you please help me about Turbo C?

Turbo C/C++ is a real real old, and an ancient compiler!

Yes we know, but I think his school requires it. :@

>>when I tried to run it, it suddenly closed

That's because there is nothing at the end of main() to prevent that behavior. If you want it to stay open so that you can see it, then add getche(); at the end of main(). The program will then stop until you hit a key.

Dragonmeister!?!?! getche() ?!?!? getchar() please. It's at least standard...

Dragonmeister!?!?! getche() ?!?!? getchar() please. It's at least standard...

There is very little about Turbo C that is standard, so it doesn't make any difference whether he uses getche() or not. Could also do this without a problem

while( !kbhit() )
    delay(100);

That's not standard either, but works in that old crappy compiler.

commented: Couldn't agree more. Wish, could +rep > 1 :) +1

I have not programmed much on Turbo C, specially graphics.. But I have a couple of observations. I am 100% sure if these are indeed correct ... Please correct me if I am wrong

>>when I tried to run it, it suddenly closed

That's because there is nothing at the end of main() to prevent that behavior. If you want it to stay open so that you can see it, then add getche(); at the end of main(). The program will then stop until you hit a key.

He has a getch() on line 17. All the display functions are before that. So I think if all the functions are called correctly he should be able to see the output

Secondly

and when I tried to run it, it suddenly closed. and I don't know what is wrong with this. :

From this information I infer that he had a run time fault. Similar to the seg fault in the linux environment

>>when I tried to run it, it suddenly closed

That's because there is nothing at the end of main() to prevent that behavior. If you want it to stay open so that you can see it, then add getche(); at the end of main(). The program will then stop until you hit a key.

i tried it but still. :'(

I'm guessing that initgraph() fails there and the program just silently exits (i.e. calls exit(1)).

[EDIT]
After looking up initgraph() it's clear that the teacher gave you a bad example (perhaps intentionally?). Anyway, you have to change the call to initgraph() so that it matches the function's signature.

ya ..i am agree with mitrmkar .so you have to right the code for initgraph.
and it's right code will be

initgraph(&gd,&gm,"");

Hi all,

i have a douby that on the line number 9.That is we have to mention the path of graphics folder located in harddisk.means in line 9 ,between the quotemarks we should have to mention the graphics location.Like

if(graphresult()!=grOk){[B]exit(1)[/B];}

This is the cause. graphresult() fails.
P.S. @OP: How come nobody before noticed it? Because the formatting was bad. Format your code nicely & bugs will come out faster. :)

A great deal of what you want is included in Turbo C's help files.

After starting up the IDE (the editor inside Turbo C), click on "Help" on the far right hand side, and then on "Index".

One graphic function that you will certainly want to look at is "rectangle" (just start typing it and the help window will take you to it), and maybe "line", and possibly "setcolor", etc.

Along with a full explanation of these functions, it also has a complete sample program you can study and copy and paste, as well.

The above description is for Turbo C/C++ 1.01. Later versions may vary.

Regarding your previous problem - never have your program quit due to an error, without first printing out what the error was. Really elementary stuff like that, will save you a TON of time, in debugging.

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.