Thread: Help with figuring out error

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    7

    Help with figuring out error

    Hello Everyone,

    I am hoping someone here will be able to help me figure out why i can't seem to load the program for the code properly. I admit that i am an absolute novice and very poor when it comes to reading code properly. The thing that i believe to be the problem appears to be something to do with the int errorcode line but i am not to sure on that. I know that the problem was meant to have error for us to figure out but after a 4 days of looking and trying on my own, i am still lost.. Everything is all set except for the aformentioned reason and along with the code is a small text file with the following information

    5
    c c s t s

    we are meant to have the program read the text file and convert text into shapes but again the cmd prompt either closes immediately or loads indefinitely is i remove the int error code lines.

    If there is any assistance that can be giving it would be greatly appreciated.

    (p.s this is actually my first time on the forums so please forgive if i didn't use the code tag correctly)

    Code:
    
    
    The program loads a number of shapes from a file.
    It then reads characters indicating what to draw of the given size.
    'c' = circle, 's' = square, 't' = triangle.
    The shapes are drawn across the screen in a row.
    */
    
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    #include <math.h>
    
    
    #ifdef __APPLE__
    /* this preprocessor statement avoids warnings about using OpenGL 2.0 standard */
    #  pragma clang diagnostic ignored "-Wdeprecated-declarations"
    #  include <GLUT/glut.h>
    #else
    #  include <GL/glut.h>
    #endif
    
    
    #define DEG2RAD   (3.1415926536 / 180)
    
    
    #define WINWIDTH    800   /* window width */
    #define WINHEIGHT 400   /* window height */
    
    
    int nshapes;        /* the number of shapes to draw */
    char *Shapes;     /* 1D array of shapes */
    float shapewidth; /* width of one shape */
    
    
                      /*
                      Load the shapes from the disk file. Allocate memory for the shape array
                      and establish the initial window width and height.
                      Returns 0 if maze read successfully, returns non-zero error code if not
                      */
    int loadShapes(char filename[]) {
        FILE *shapefile;
        int nok;
        int col;
        char ch;
    
    
        shapefile = fopen(filename, "r");
        if (shapefile == NULL)
            return 1;
    
    
        nok = fscanf(shapefile, "%d\n", &nshapes);
        if (nok != 1)
            return 2;
    
    
        Shapes = (char *)malloc(nshapes * sizeof(char));
    
    
        for (col = 0; col < nshapes; col++) {
            ch = getc(shapefile);
            while (isspace(ch))
                ch = getc(shapefile);
            if (ch == EOF)
                return 3;
            Shapes[col] = ch;
        }
    
    
        shapewidth = (float)WINWIDTH / (float)nshapes;
    
    
        return 0;
    }
    
    
    /*
    Reshape Routine: update the screen viewport to keep the maze
    in correct proportions
    */
    void reshapeWindow(int width, int height) {
        glViewport(0, 0, width, height);
    
    
        /* lower left of window is (0, 0), upper right is (W, H) */
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluOrtho2D(0, WINWIDTH, 0, WINHEIGHT);
    }
    
    
    void drawCircle(float cx, float cy) {
        float x, y;
        int theta;
        float r = shapewidth / 2;
    
    
        glBegin(GL_POLYGON);
        for (theta = 0; theta < 360; theta += 5) {
            x = cx + r * cos(theta * DEG2RAD);
            y = cy + r * sin(theta * DEG2RAD);
            glVertex2f(x, y);
        }
        glEnd();
    }
    
    
    void drawSquare(float cx, float cy) {
        float delta = shapewidth / 2;
    
    
        glBegin(GL_POLYGON);
        glVertex2f(cx - delta, cy - delta);
        glVertex2f(cx + delta, cy - delta);
        glVertex2f(cx + delta, cy + delta);
        glVertex2f(cx - delta, cy + delta);
        glEnd();
    }
    
    
    void drawTriangle(float cx, float cy) {
        float delta = shapewidth / 2;
    
    
        glBegin(GL_POLYGON);
        glVertex2f(cx - delta, cy - delta);
        glVertex2f(cx + delta, cy - delta);
        glVertex2f(cx, cy + delta);
        glEnd();
    }
    
    
    /*
    Display Routine: clear the screen and draw the shapes
    */
    void drawShapes() {
        int col;
        float x, y;
    
    
        /* clear the window to the background color */
        glClear(GL_COLOR_BUFFER_BIT);
    
    
        /* draw the circles in red, squares in green, triangles in blue */
        y = WINHEIGHT / 2.0;
        for (col = 0; col < nshapes; col++) {
            x = (col + 0.5) * shapewidth;
            switch (Shapes[col]) {
            case 'c':
                glColor3f(1.00, 0.0, 0.0);
                drawCircle(x, y);
                break;
            case 's':
                glColor3f(0.0, 1.0, 0.0);
                drawSquare(x, y);
                break;
            case 't':
                glColor3f(0.0, 0.0, 1.0);
                drawTriangle(x, y);
                break;
            }
        }
    
    
        glutSwapBuffers();
    }
    
    
    /*
    This routine is called every time a key is pressed on the keyboard
    It causes the program to quit if the q or ESC key is pressed
    */
    void handleKey(unsigned char key, int x, int y) {
    
    
        switch (key) {
        case 'q':        /* q - quit */
        case 'Q':
        case 27:        /* esc - quit */
            exit(0);
    
    
        default:        /* not a valid key -- just ignore it */
            return;
        }
    }
    
    
    /*
    Initialize the maze, and the graphics interface.
    Entering the main loop initiates display
    */
    int main(int argc, char *argv[]) {
        int errorcode;
    
    
        /* shape file name is the only argument */
        if (argc != 2) {
            printf("usage: shapes <name>.txt\n");
            exit(1);
        }
    
    
        /* load the shapes from the disk file and initialize */
        /* exit with an error flag if shapes not loaded correctly */
        errorcode = loadShapes(argv[1]);
        if (errorcode != 0) {
            fprintf(stderr, "load error %d\n", errorcode);
            return 1;
        }
    
    
        /* start up the glut utilities */
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
    
    
        /* create the graphics window, giving width, height, and title text */
        glutInitWindowSize(WINWIDTH, WINHEIGHT);
        glutCreateWindow("Load Some Shapes");
    
    
        /* post routine called to handle keyboard key presses */
        glutKeyboardFunc(handleKey);
    
    
        /* post routine called to draw graphics when window exposed */
        glutDisplayFunc(drawShapes);
    
    
        /* post routine called to update board size when window reshaped */
        glutReshapeFunc(reshapeWindow);
    
    
        /* specify window clear (background) color to be black */
        glClearColor(0, 0, 0, 0);
    
    
        glutMainLoop();
        return 0;
    }
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Sep 2015
    Location
    Australia
    Posts
    63
    Hi

    After 4 days looking, you should have an idea where the problem area is......what sort of Debug approach did you use...

    As the loadShapes() function has 3 return numbers, which was getting returned , if any ?

    Its the return's in main that are shutting down the cmd prompt before you have a chance to see what codes returning, use something like getchar() ( this will just stop and wait for you to enter a char, gives you time to read error reports) before the returns, to see how far the program is getting.......you can also use printf statements to see if your getting the data you expected....Have you tried these easy methods to try track down the problem area..?
    Last edited by JohnGM; 02-27-2016 at 10:34 PM.

  3. #3
    Registered User
    Join Date
    Feb 2016
    Posts
    7
    With the way i went about debugging (Microsoft Visual Studio), what did help was that by removing the int errorcode stuff in int main and changing the return to getchar(), i managed to get the program to load but it simply appears as a blank screen constantly in a loading state. there are no error messages that would pop up under normal circumstances so....

    As i said I am still a novice so forgive that i can't specify further or work on the numbers things to fix it on my own. the thing i want to at least do is make it so the program reads the text file and draws from it. at this point, i'm stuck.

  4. #4
    Registered User
    Join Date
    Sep 2015
    Location
    Australia
    Posts
    63
    Hi ..

    I had it working as you asked, in the basic version to make it run with the error code in there...but no display of shapes..

    You have to make sure the information is being passed to the function.... I placed a " printf(" filename : %s\n".filename); "
    statement as first line in the loadsShapes() function to make sure the filename was being passed. You also have to make sure you store the file shapes.txt in the same folder as the program , and also make sure as I found out.....when you save the txt file it could get saved as shapes.txt.txt.....it did not like that...lol. so check. once that was done it passed all the way through error coding....

  5. #5
    Registered User
    Join Date
    Sep 2015
    Location
    Australia
    Posts
    63
    Hi ..

    And yes the rest of the loadShapes(), functions works, it extracts the number 5 and then extracts the characters.

    One thing I may suggest is to add a statement after the "for- loop "to put a ' \0' on the end of the Shapes string so you don't get garbage out..

  6. #6
    Registered User
    Join Date
    Feb 2016
    Posts
    7
    Hi,

    After following your advise and changing some things, i managed to get it running smoothly! If it would not be too much to ask... Something that i would like to try now with the code is make it so that it reads the txt file but does it in a way where i can stack shapes on top of each other in rows and columns, something like:

    4 5
    c t t t c
    s s s s s
    s s s s s
    c c c c c

    I know enough now to know that the part that needs changing is the int LoadShapes function but i can't piece how to change. my attempt thus far with this endeavor has ended either an executable appearing but not showing or shapes that look out of scale in the window.
    Last edited by MimiTaso; 03-03-2016 at 07:52 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. figuring out code
    By Thegame16 in forum C Programming
    Replies: 16
    Last Post: 09-23-2011, 06:47 PM
  2. Help figuring out error
    By dudeomanodude in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2008, 03:20 PM
  3. Help figuring out equation.
    By gator6688 in forum C++ Programming
    Replies: 4
    Last Post: 10-05-2007, 03:17 PM
  4. help figuring out a problem
    By kwm32 in forum Windows Programming
    Replies: 2
    Last Post: 03-20-2004, 10:47 PM
  5. Need help figuring out compile error.
    By ced in forum C++ Programming
    Replies: 3
    Last Post: 11-25-2002, 08:17 AM