Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When i assign int r, s, t, u, v; none of the if conditions are working.
When i assign int r=0, s=0, t=0, u=0, v=0; all the if conditions are working.
I am not able to find the error.
int r, s, t, u, v;
    if((mouseXPos < 130 && mouseXPos > 30 && mouseYPos > 365 && mouseYPos < 410)&&(s==0)&&(t==0)&&(u==0)&&(v==0))
        {
            drawMenu(0);
            r=1;
        }
   if((mouseXPos < 230 && mouseXPos > 130 && mouseYPos > 365 && mouseYPos < 410)&&(r==0)&&(t==0)&&(u==0)&&(v==0))
        {
            drawMenu(1);
            s=1;
        }
   if((mouseXPos < 330 && mouseXPos > 230 && mouseYPos > 365 && mouseYPos < 410)&&(r==0)&&(s==0)&&(u==0)&&(v==0))
        {
            drawMenu(2);
            t = 1;
        }
    if((mouseXPos < 430 && mouseXPos > 330 && mouseYPos > 365 && mouseYPos < 410)&&(r==0)&&(s==0)&&(t==0)&&(v==0))
        {
            drawMenu(3);
            u = 1;
        }
    if((mouseXPos < 530 && mouseXPos > 430 && mouseYPos > 365 && mouseYPos < 410)&&(r==0)&&(t==0)&&(u==0)&&(s==0))
        {
            drawMenu(4);
            v = 1;
        }
    if(mouseXPos < 130 && mouseXPos > 30 && mouseYPos < 185 && mouseYPos > 140)
        {
            drawMenu(5);
        }

void drawMenu(short b)
   {

       int k = 0, i = 0, j = 0;
       if(b == 0)
          {
              a = &touch_menu[0];
             for(k=0; k<a->c; k++)
              {
                 setColor(GREY);
                 drawRectangle(a->startXPos, a->startYPos - (k+1)*a->height,a->width,a->height);
              }
                  setColor(CYAN);
                  for(i=0; i<a->c; i++)
                    {
                      drawText(38,150+j,a->sub_Menu[i],0);
                      j = j+45;
                    }


          }

        else if(b==1)
          {
             a = &touch_menu[1];
             for(k=0; k<a->c; k++)
              {
                 setColor(GREY);
                 drawRectangle(a->startXPos, a->startYPos - (k+1)*a->height,a->width,a->height);
              }
                  setColor(CYAN);
                   for(i=0; i<a->c; i++)
                  {
                    drawText(138,195+j,a->sub_Menu[i],0);
                    j = j+45;
                  }

          }
         else if(b==2)
          {
             a = &touch_menu[2];
             for(k=0; k<a->c; k++)
              {
                 setColor(GREY);
                 drawRectangle(a->startXPos, a->startYPos - (k+1)*a->height,a->width,a->height);
              }
                  setColor(CYAN);
                   for(i=0; i<a->c; i++)
                  {
                    drawText(238,240+j,a->sub_Menu[i],0);
                    j = j+45;
                  }

          }
        else if(b==3)
          {
             a = &touch_menu[3];
             for(k=0; k<a->c; k++)
              {
                 setColor(GREY);
                 drawRectangle(a->startXPos, a->startYPos - (k+1)*a->height,a->width,a->height);
              }
                  setColor(CYAN);
                   for(i=0; i<a->c; i++)
                  {
                    drawText(338,195+j,a->sub_Menu[i],0);
                    j = j+45;
                  }
          }
        else if(b==4)
          {

             a = &touch_menu[4];
             for(k=0; k<a->c; k++)
              {
                 setColor(GREY);
                 drawRectangle(a->startXPos, a->startYPos - (k+1)*a->height,a->width,a->height);
              }
                  setColor(CYAN);
             for(i=0; i<a->c; i++)
                  {
                    drawText(438,285+j,a->sub_Menu[i],0);
                    j = j+45;
                  }
          }
        else if(b==5)
          {
              a = &touch_menu[5];
              for(k=0; k<a->c; k++)
              {
                 setColor(GREY);
                 drawRectangle(a->startXPos, a->startYPos - (k+1)*a->height,a->width,a->height);
              }
             for(i=0; i<a->c; i++)
             {
                 drawText(138,150+j,a->sub_Menu[i],0);
                    j = j+45;

             }
          }
   }
Posted
Comments
Stefan_Lang 3-Apr-14 5:41am    
In addition to OriginalGriffs solution I would like to add that you really should extract some of those repetitive code blocks into seperate functions and reduce redundancy. As it is now, the code is nearly unreadable and unmaintainable.

Next you should replace all those "magic numbers" you use with self-explanatory constants, e. g. MENU_1_LEFT, MENU_1_TOP, etc..*

Third, always initialize any variable you declare with some value. You shouldn't even consider using your first variant!

Also: use meaningful names for your variables! I have no idea what your r, s, t,... means, and I wonder whether you do.

* P.S.: better yet, use structs for your menu items, where each struct holds the x/y limits of the item, and then write a function that tests whether a given point is within that menu items area. That would greatly help to clarify the purpose of the if tests.

1 solution

When i assign int r, s, t, u, v; none of the if conditions are working.
When i assign int r=0, s=0, t=0, u=0, v=0; all the if conditions are working.

Well, yes, that is what I would expect.

In C, when you declare a variable:
C++
int x;
you allocate space for it, but you do not assign a value, and the system will not create one for you: whatever is in that memory from the last time it was used - which could be anything from part of a string to a pointer depending on what your code did at run time up to that point - is left in the variable.
When you declare a variable
C++
int x = 0;
You allocate space for it, and say "I want this to be zero" so the system obliges.

In your code, you declare the variables, but do not assign any value to them before you use them in the if conditions: so the values they contain and that you test against are random, and unlikely to be zero!
 
Share this answer
 
Comments
atul329 2-Apr-14 6:15am    
So, then how should i assign the variables to run if condition?
OriginalGriff 2-Apr-14 6:18am    
To be honest, I can't tell you - because you don't want to "assign the variables" to a fixed value: you need them to be values which control what happens in the conditions: and I have no idea where such values should come from! Or even what you are trying to do here...

You wrote the code: you should know what "s" is supposed to do, not me! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900