2

I am trying to make a graph using graphics.h in c++. I was following a tutorial on youtube. It seems that either due to the age of the video (perhaps the syntax has changed slightly?) or a problem on my end; I cannot even get a separate window for my graph to open. I am in completely uncharted waters for me as the limit of my coding knowledge is what you would expect to learn from a first-semester coding class. I am using DEV C++ and am compiling using "TDM-GCC 4.9.2 32-bit Release" (because the 64 bit release gives me an error in "Makefile.win" that scares me) and my program exits with a return value of 3221225477. What am i doing wrong?

#include"graphics.h"
#include<math.h>
#include<conio.h>
#include<iostream>
using namespace std;

int main() {
    
    initwindow(800,600);
    int x,y;
    line(0,300,getmaxx(),300);
    line(400,0,400,getmaxy());
    float pi=3.14159;
    
    for(int i=-360;i<=360;i++){
        
        x=(int)400+i;
        y=(int)300-sin(i*pi/100)*25;
        putpixel(x,y,WHITE);
    
    }
    
    getch();
    closegraph();
    
    return 0;
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
Erabior
  • 31
  • 3

1 Answers1

0

According to your issue and exit-code, the return value in hex is 0xC0000005 or STATUS_ACCESS_VIOLATION. But most developers didn't even bother to learn out-dated legacy API and I can not help you to find the exact line (use debugger, it shows you the exact line, still not the reason).

But to answer your question in the title, well, according to what free framework one uses (Qt or XWidget), the method differs, for Qt (which I would recommend) simply override paint-event and use QPainter renderer to show your QPath data.

Don't reinvent the wheel (or render-system in this case), your course and/or book may soon introduce you to one of the mentioned frameworks.

Top-Master
  • 7,611
  • 5
  • 39
  • 71
  • Thank you for your help! I'm not actually in a programming class but I have taken one. I am doing an assignment for Strength of materials (engineering statics part 2) and the professor wanted to see if it would be fun/useful if he gave us a statically indeterminate problem that cant is solved by traditional methods. So the only way to solve it is to code a script to iterate over unknown values. as far as your answer, it completely flew over my head and I have no idea what you are talking about. What is "Qt" and I'm assuming "Paint-event" is a function? I apologize for being such a novice. – Erabior Feb 13 '21 at 19:06
  • Qt-project is a cross-platform framework (for Mac, Linux, Windows and ...), and paint is a virtual method of QWindow (which we override to handle showing something custom on the window) but even if I introduced you to a graphical example and you manage to build it, there is a high chance your actions are regarded as trying to show off (beside rising future expectations). – Top-Master Feb 13 '21 at 19:40
  • Try to solve the problem with basic console-text input/output, `std::cout` and `std::cin` (instead of showing graphs for now); I wanted to give you some graph source-code but it seems finding a simple console graph app source is rather too hard (all I found were complex games). Still for Qt it was simple to find [a simple graph only sample](https://github.com/dbzhang800/QCustomPlot) – Top-Master Feb 13 '21 at 20:28
  • I can do basic input and output stuff. If I could i would simply output the data as a table. However, the professor has specifically asked for a graph. The only other thought I had was outputting the data to a CSV and importing it into excel. I would prefer to avoid doing this as he got a little annoyed when I asked if it would be acceptable and said he would prefer only grading one file (either one excels sheet or one script file) – Erabior Feb 13 '21 at 20:40
  • If he is not a progrmming teacher, don't worry, just provide him the excel file; if he can only grade one file, there is no need to have a program that can generate millions ¯\\_(​ツ)_/¯. But if some script is specifically asked for, well press Alt+F11 (Cmd+F11) to open Excel's built-in scripting window (VBA IDE). Your script should just set some cell's content (which you use in your 3D graph), by some button click (yes excel has buttons and stuff, you just need to enable the ribbon-tab, goto File -> Options -> Customize Ribbon then fill Developer check-box). – Top-Master Feb 13 '21 at 21:15