C++ function to stop a number

Hello,

I searched around some and couldn't find out what im looking for. Somewhat because im not sure what to call it, but im sure experienced coders like yourselves will be able to solve this in a instant.

I'm looking for a way to assign a specific number to a variable, but once it is assigned, I want the variable to stop receiving new numbers. so it would only hold the first number.

Specifically for my situation,
I'm creating a function that reads the mouse input and places a box where the mouse is pressed ( with respect to x and y ), but once the box is place, I don't want it to move. Where ever the user clicked the mouse, i want a box there and nowhere else, and when the user releases the mouse i want the box to vanish and another box can be created if the mouse is pressed again. I feel as though there has to be a structure function that does this.

Thanks for any help!
Maybe if you show some code we could help easier, because then we have to imagine and create / write this as you tell us it.

So you have two type ints, identifiers x, y . Then you have the function getmousepos(int a, int b); for example and you're having x and y display the mouse position in some kind of always true, repetitive iteration so their values are always changing when you move your mouse? But when you click your, for example Mouse1 button you will then run the function getmousepos(int a, int b); with x and y being arguments and trying to display that position through a cout statement? But then want to save those values into some variable so they never change?


Edit : You can set a bool , while true capture the positions of x / y. then when your function is called change the bool to false. Then blah , its pretty hard for me as a beginner also to help without code =[. Good luck, or shoot me your code through pm and I can take a look.
Last edited on
yea man, your description seems on the dot. the problem (like you said) is that getmousepos(int a, int b) changes, so if i do x = mousexvalue, then x will constantly change to be the mouse's position, where as i just want x to stay the first number it was assigned. And sorry I don not have any code to show yet since this little hiccup is stopping me from actually beginning my code...


I was able to do a work around: here is my code for anybody who is curious:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int num;
int x;
int y;
void setup() {
  size(600, 600);
}

void draw() {
  background(255,255,255);
  if(num<0)
{num=0;}
if(mouseButton == LEFT && mousePressed == true)
{num -= 1;}
  if (mouseButton == RIGHT && mousePressed == true || num == 1) {
    menu();
    num = 1;
}

}
void menu()
{
if(mousePressed == true)
{
x = mouseX;
y = mouseY;
}
rect(x, y, 60, 120);
return;
}



By the way, this is for a program called proccesing, that is why the script looks a little different from the one used in other c++ programs
Last edited on

Based on the below criteria;
- You want the box to exist while the mouse is down.
- You don't want the box to move once placed
- You want the box to go away after the mouse is released.


This is a very simple problem. You can do the following:

- Have a bool to indicate whether or not the box exists (I'll call it 'boxexists')
- Have a position/size for the box.
- When the user clicks, place the box at the desired coords and set boxexists to true
- When the user releases the mouse button, set boxexists to false
- Draw the box or otherwise treat it is it exists only when boxexists is set to true.
Disch,

that is very similar. To put it simply, I wanted a box to appear and behave like a menu. (i will eventually make it a menu). But just as an example, I wanted a box to appear exactly like it does when you right click on the windows desktop. A menu appears, and it stays where you originally right clicked your mouse. And it goes away if you left click, or relocates itself if you right click somewhere else. Just wanted clear up your criteria incase anybody else needed this to help them!

Thanks for your help guys
Topic archived. No new replies allowed.