Search code examples
memorypolygonfltk

Deallocate and reallocate memory. Resetting Polygon in FLTK


#include "Simple_window.h"   
#include "Graph.h"          
#include <math.h>
#include <iostream>
#include <limits>
using namespace std;

int main(){

    Simple_window win(Point(100,100),600,400,"Marks");



    Graph_lib::Polygon poly;
    bool over = true;
    int n = 0;

    while(over){
            Marks pp("x");
            pp.add(Point(300,200));

            cout<<"Enter number of sides (3 or more): ";
            while(!(cin>>n)){
                cin.clear();
                  cin.ignore(numeric_limits<streamsize>::max(), '\n');
                  cout<<"Enter number of sides (3 or more): ";

            }
            if(n<3){
                break;

            }

    //Finding the angle and number of sides...

        if(n%2 != 0){
        //logic for polygon...



          }
        }else{

        //logic for polygon...
            }


        }
    poly.set_color(Color::magenta);  // adjust properties of poly

    win.attach (poly);           // connect poly to the window

    win.attach(pp);

    win.wait_for_button();       // Display!


    }



}

What does this program does it make n-sided polygons. After I set my polygon. I wish to reset it. After i enter the amount of sides in a polygon. I click on "next" it will ask me how many polygons again and repeat the process. Someone told me to call delete. However I'm not sure how I would type that in. As in delete poly.points? or delete polygon ? I tried a few but I kept getting errors.

My second question is on line 38. Originally I had it so If i entered a number less than 3 the program will end. But the program won't end. So i had to use break;

if(n<3){
    over=false;
    cout<<"why wont this end ?"<<endl;

}

boolean over is set to true. While true loop. If n < 3 over is false. thus end the program? it Wont end.


Solution

  • There is no need to reset the poly if you stick the declaration inside the first while loop.

    while(over){
        Graph_lib::Polygon poly;
        ...
        win.wait_for_button();
    }
    

    Re: why your program won't end. Break is good enough but if you don't like using break

    • use continue
    • put the rest of the loop body in an else clause

    Edit: Re: changing the title: I don't know the Simpe_Window interface: it is not part of FLTK - it is a Stroustrup creation. If it is derived from FL_Window, use label(new title).