-1

Context Language : C++ , Editor : Microsoft Visual Studio Code Version 1.15.1

Problem I am using Microsoft Visual Studio Code Version 1.15.1 for Compiling my C++ Programs. I want to use Graphics.h to use Graphics in my C++ Programs. That's why I downloaded the Three files Graphics.h ,winbgim.h and libbgi.a and Put them into the Right Places.I also Downloaded an BGI folder from https://www.cs.colorado.edu/~main/bgi/visual/BGI2010.zip and Put it into the Right Place. But When I am Compiling My Program then it Showing Some error.

My C++ Program

    #include<iostream>
    #include<math.h>
    #include<graphics.h>
    using namespace std;
    int main(){
        int x,y;
        int gd = DETECT ,gm;
        initgraph(&gd,&gm,"C:\\MinGW\bgi");
        cout<<"Enter the Value of X Co-ordinate : ";
        cin>>x;
        cout<<"Enter the Value of Y Co-ordinate : ";
        cin>>y;
        putpixel(x,y,WHITE);
        closegraph();
    }

Error of my Program

computerGraphics1.cpp: In function 'int main()':
computerGraphics1.cpp:8:25: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
     initgraph(&gd,&gm,"C:\\MinGW\bgi");
                         ^
C:\Users\TUSK\AppData\Local\Temp\ccEbDqZq.o: In function `main':
C:\Users\TUSK\Desktop\Practise\C++ Projects/computerGraphics1.cpp:8: undefined reference to `initgraph'
C:\Users\TUSK\Desktop\Practise\C++ Projects/computerGraphics1.cpp:13: undefined reference to `putpixel'
C:\Users\TUSK\Desktop\Practise\C++ Projects/computerGraphics1.cpp:14: undefined reference to `closegraph'
collect2.exe: error: ld returned 1 exit status

So Guys who ever Finds its Solution Please Answer it

genpfault
  • 51,148
  • 11
  • 85
  • 139
Himanshu
  • 17
  • 1
  • 1
  • 2
  • 2
    The `` header and library are antiquated and obsolete. And was made for C which allows pointers to non-constant strings. In C++ all literal string constants are just that: ***Constant***. The *warning* is telling you that, because the `initgraph` takes a pointer to a non-constant string (which as stated before is valid in C, even though literals strings are read-only). – Some programmer dude Aug 29 '17 at 14:21

3 Answers3

1

Detailed setup if you want to use graphics.h in VC Code.

This worked for me...

char driverPath[] = "C:\\MinGW\\lib\\libbgi.a"; //static file
initgraph(&gd, &gm, driverpath);

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
ullas kunder
  • 384
  • 4
  • 11
0

You can "fix" the error with a const_cast, but beware, if ever you do try to modify the string contents you can expect a crash.

Paul Floyd
  • 5,530
  • 5
  • 29
  • 43
0

Cin is not working when we use Graphics in VS Code, so we need to define the values of 'x' and 'y' manually.

    #include<iostream>
    #include<graphics.h>
    using namespace std;
    int main(){
        int x,y;
        int gd = DETECT ,gm;
        initgraph(&gd,&gm,NULL);
        // cout<<"Enter the Value of X Co-ordinate : ";
        // cin>>x;
        // cout<<"Enter the Value of Y Co-ordinate : ";
        // cin>>y;
        x=156;
        y=172;
        putpixel(x,y,WHITE);
        getch();
        closegraph();
    }

Output

Terminal Result:

  Starting build...
  C:\MinGW\bin\g++.exe -fdiagnostics-color=always -g "C:\Programming\Computer 
  Graphics\stackover.cpp" -o "C:\Programming\Computer Graphics\stackover.exe" - 
  lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32

  Build finished successfully.

  Terminal will be reused by tasks, press any key to close it.

Follow these steps to run graphics.h properly in VS Code.

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Nol4635 Jul 03 '22 at 23:04