3

I have made a program that takes a series of commands, e.g. square 150 160 and draws some shapes in a window. I want to do it for an arbitrary number of commands. So, I use a loop to do this. The thing is that when the commands that create the shapes and attach them to the window are in a loop, then at the end window has no content, e.g. nothing is attached to it. I think this may happen because the calls that create the shapes inside the loop are then destroyed after the end of their block (scope).

How can I overcome this problem?

Here is my code:

#include "Simple_window.h"
#include "Graph.h"
#include <string.h>
#include "Point.h"
#include "Window.h"
#include "GUI.h"

string shape[100],color[100],name;
int x[100],y[100],side[100],width[100],height[100],radius[100],i=0;

int main()
{

  using namespace std;
  using namespace Graph_lib;

  //---------------Reading Input------------------------------------------------//
  while (( cin >> name)&&(i<=3))
  {

  if (name.compare("square") == 0 ){
    i++;

    shape[i]="square";
    cin >> x[i] >> y[i] >> side[i] >> color[i];
  }else if (name.compare("circle") == 0){
    i++;

    shape[i]="circle";
    cin >> x[i] >> y[i] >> radius[i] >> color[i];
  }else if (name.compare("rectangle") == 0){
    i++;

    shape[i]="rectangle";
    cin >> x[i] >> y[i] >> width[i] >> height[i] >> color[i] ;
  }


}

Simple_window win(Point(100,100), 900, 600, "test");

for( i=1;i<=3;i++){

if (shape[i].compare("circle") == 0){
    Circle r(Point(x[i],y[i]),radius[i]);
    win.attach(r);
    cout << "circle";


}else if(shape[i].compare("rectangle")==0){
    Rectangle l(Point(x[i],y[i]),width[i],height[i]);
    win.attach(l);
    cout << "rect";

}else if(shape[i].compare("square")==0){
    Rectangle k(Point(x[i],y[i]),side[i],side[i]);
    win.attach(k);
    cout << "square";
 }

}

win.wait_for_button();
return 0;
}
Daimba
  • 31
  • 2
  • Are you reading Stroustrup's "Principle and Practice Using C++"? Because I ran into exactly the same issue and it wasn't explained before exercises in the book. Very confused right now. – diversario Jan 13 '13 at 02:54

3 Answers3

1

Your variables need to be created dynamically to avoid being destroyed when they go out of scope.

Circle *r = new Circle(Point(x[i],y[i]),radius[i]);
win.attach(*r);
stark
  • 12,615
  • 3
  • 33
  • 50
  • I Just tried it and now i get this :: ask3.cpp:49:50: error: conversion from ‘Graph_lib::Circle*’ to non-scalar type ‘Graph_lib::Circle’ requested ask3.cpp:51:65: error: conversion from ‘Graph_lib::Rectangle*’ to non-scalar type ‘Graph_lib::Rectangle’ requested ask3.cpp:53:62: error: conversion from ‘Graph_lib::Rectangle*’ to non-scalar type ‘Graph_lib::Rectangle’ requested – Daimba Dec 07 '12 at 20:58
0

It depends on how your attach function is written, when it passes the arguments by reference, no copy is made of the object when the function is called, and indeed the added object will be destroyed when its scope runs out ie. the for loop block.
When this is the case, you could rewrite the window.attach() function or you'll need to use pointers and the "new" statement to create the objects. When you use new, you should also use delete to destroy the objects when they are not longer needed! Probably best to add the destruction of the attached objects in the destructor of the window class...

Circle r = new Circle(Point(x[i],y[i]),radius[i]);
win.attach(*r)

If you didnt know this, you should really learn more about pointers in c++

Emile Vrijdags
  • 1,550
  • 2
  • 15
  • 24
0

In the FLTK demo directory there is an example how to make a custom-shape widget. That is IMHO the way to do what you want to do. Once you make all shapes (subclasses of the Widget class) it is easy to put them in a fltk::Group (a fltk::Window for an example).

The demo I am talking about is about star-shaped widget. I do not remember exactly the name of the file.

Update: An easier way would be to simply override draw() method for the window, and simply paint all shapes... It is extremely easy to do - read the following: http://www.fltk.org/doc-1.3/drawing.html .

DejanLekic
  • 18,787
  • 4
  • 46
  • 77