1

So, I currently work on an OOP flow chart project.

Point here is that I want to make sure that the program doesn't draw 2 shapes over each others "As well as for window boundaries", so I should give an IF CONDITION, such that within it, the Program draws in the interface !

Here's a part of my code.

 case  ADD_START:
         pO->PrintMessage("Action: add start statement, Click anywhere in the drawing Area");
         Statement*statS;
         pIn->GetPointClicked(P);   
         if ( (for (int i = 0; i < NumOfFigures; i++)
         { //Test
            i == NumOfFigures;
         } ) 
             && (P.x>30) && (P.x< UI.width-(30+UI.ELLIPSE_R1)) && (P.y>2*UI.TlBrWdth+3) 
         && (P.y<UI.height - (UI.StBrWdth+UI.ELLIPSE_R2)) )
             {   
                 statS=new Start();
                 statS->setPoint(P);
                 statS->DrawStatement(pO,pIn);
                 pStat[NumOfFigures] = statS;
                 NumOfFigures++;
             }
      break;

Here you can see that pStat is array of pointers to an abstract class Statement, however, I want to add the address of the shapes drawn at the run-time into that array of pointers, so that withing the if condition I could create a for loop to check upon each shape in that array with the boundaries of it as to check that P (which is a point)

P.x != pStat[NumOfFigure]->x;

But the problem here that it gives me an error for the for loop inside the if statement, saying that

ERROR : Expected an expression "

What should I do ?

7
  • 1
    Wait, what? It's not entirely clear why you're adding a for loop inside an if condition. What is that if condition testing? Commented May 14, 2014 at 9:15
  • It's not very clear for my what you try to express with if ( (for (int i = 0; i < NumOfFigures; i++), but why not just doing it the other way round? Commented May 14, 2014 at 9:16
  • 2
    You need to rethink the solution and what you want to do. A for statement is just that, a statement, and can't ever be used as an expression which is what if expects as condition. Commented May 14, 2014 at 9:16
  • 1
    @user3054349 I still don't get it. Why not having this for loop to traverse all those shapes, and put the boundaries check inside. You don't need to shout on me BTW, OK! Commented May 14, 2014 at 9:48
  • 1
    Supposing that was syntactically valid, what would you expect the value of for(int i = 0; i < NumOfFigures; i++) { i == NumOfFigures; } to be? Commented May 14, 2014 at 9:55

2 Answers 2

3

The syntax you are following is invalid syntax to compare set of values in loop

You can test for NumOfFigures in separate loop and maintain some flag variable, and use that variable to compare in if condition

bool flag=true;
for (int i = 0; i < NumOfFigures; i++)
{ 
    //Test
    if(i != figuresArray[i]){  //here you'l need to use your array from where you want to compare values
        flag=false;
        break;// its not matching, so break loop to test for further values
    }
}

//use flag variable in your if condition
if (flag==true && (P.x>30) && (P.x< UI.width-(30+UI.ELLIPSE_R1)) && (P.y>2*UI.TlBrWdth+3) 
     && (P.y<UI.height - (UI.StBrWdth+UI.ELLIPSE_R2)) )
         {   
             statS=new Start();
             statS->setPoint(P);
             statS->DrawStatement(pO,pIn);
             pStat[NumOfFigures] = statS;
             NumOfFigures++;
         }
Sign up to request clarification or add additional context in comments.

2 Comments

Alternatively, put the for-loop logic in a function returning bool.
I would use bool and a bit more meaningful name for flag, but otherwise I think your answer fits.
0

You can not add loop in any conditional statement. The concept is you can check any condition and if it is true do something, if not, do some other thing.

if( /*expression*/){

  //do  something, including method call, looping construct etc.
}
else if( /*another condition*/){
  //do some other thing
}
 //..... other condition
else{
  // do something
}

EDIT

To write loop inside the if block:

if( condition ){
 for( int i=0;i<NumOfFigures;i++)
 {
  statS=new Start();
  statS->setPoint(P);
  statS->DrawStatement(pO,pIn);
  pStat[i] = statS; // should be i , not numOfFigures
 }
}

1 Comment

Ok, so how can I check for my array ? I want it to enter the array from 0 to the NumOfFigures and check for every shape inside the array, get it ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.