Increasing Scope help?

Jan 7, 2012 at 1:50am
Is there any way possible to increase scope? As in the following example?

1
2
3
4
5
6
int a = 5 ;

if ( a == 5 )
     a = 6;

cout << a ;

Is there any way possible that I could make the cout print 6 instead of 5?
Last edited on Jan 7, 2012 at 1:33pm
Jan 7, 2012 at 2:12am
You're using the wrong = on line 3. I think that's your real issue, not scope.

-Albatross
Jan 7, 2012 at 4:21am
= is an assignment operator, == is comparison operator.
Jan 7, 2012 at 4:49am
Either way, it is printing 6...
Jan 7, 2012 at 5:52am
That's what makes me think that perhaps the OP was just using an example and that his real code is somewhat different.

-Albatross
Jan 7, 2012 at 1:33pm
That was an example and a mistake on my hand. But just in general, how do I increase scope? Is there an operator for that or something?
Jan 7, 2012 at 1:37pm
No. Just declare the variable in the appropriate scope.
Jan 9, 2012 at 12:07am
But I want the variable to change according to the appropriate condition. In the example, I don't want to make a = 6 unless it equals 5.
Jan 9, 2012 at 12:17am
Well, and that's exactly what the code does.
So where is the problem?
Jan 9, 2012 at 12:25am
closed account (zb0S216C)
Kingsman142 wrote:
Is there any way possible to increase scope? (sic)

Unless you have explicit access to your program's stack, then no, there isn't a way. Your scope is limited to the conditional expression and the statement that follows it.

Wazzak
Last edited on Jan 9, 2012 at 12:27am
Jan 9, 2012 at 12:41am
@Athar I guess my example was a bad one for the situation I'm in. It might be an SFML problem.

@Framework Then how do I change a variable by using an if statement and keep it that way?
Jan 9, 2012 at 12:44am
So provide a proper example.
So far it seems that your "situation" exists only in your imagination.
Last edited on Jan 9, 2012 at 12:45am
Jan 9, 2012 at 12:46am
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
30
31
32
33
            //Bullet Sprite
            sf::Texture BulletImg;
            BulletImg.LoadFromFile("Sprite_Bullet.jpg");
            sf::Sprite Bullet;
            if(!BulletImg.LoadFromFile("Sprite_Bullet.jpg"))
                return 1;
            Bullet.SetTexture(BulletImg);
            Bullet.SetPosition(186,305);
            Bullet.SetScale(.05,.05);

            //Cubez Sprite
            sf::Texture CubezImg;
            CubezImg.LoadFromFile("Sprite_Cubez.jpg");
            sf::Sprite Cubez;
            if(!CubezImg.LoadFromFile("Sprite_Cubez.jpg"))
                return 1;
            Cubez.SetTexture(CubezImg);
            Cubez.SetPosition(670,295);
            Cubez.SetScale(.5,.5);

            //Script for Bullet and Cubez colliding
            float bulletSpeed = 152.5;
            float cubezSpeed = 170;
            sf::Vector2f bulletPos = Bullet.GetPosition();
            sf::Vector2f cubezPos = Cubez.GetPosition();

            sf::Event Event;
            switch(Event.MouseButton.Button)
            {
                case sf::Mouse::Left:
                    Bullet.SetPosition(bulletPos.x + bulletSpeed , bulletPos.y);
                    Cubez.SetPosition(cubezPos.x - cubezSpeed , cubezPos.y);
            }


I used SFML for this and the switch statement says that once I click my left mouse button, the 2 sprites should move to a different position. It's fine until I move my mouse. Once I do that, they go back to their original position. I was thinking it's because of scope.
Jan 9, 2012 at 12:58am
You aren't retrieving any events. You need an event loop, check the SFML tutorials for that.

I was thinking it's because of scope.

If this function is repeatedly called and you don't want to have your bullet etc. reset every time, then yes, you're creating those objects at the wrong place. If that's not the case, then the scope is fine.
Last edited on Jan 9, 2012 at 1:03am
Jan 9, 2012 at 1:05am
I have a while statement right after that with a poll event. Should I place the switch statement in that while statement?
Jan 9, 2012 at 1:11am
If you just check the mouse button of an event once (a default-constructed and empty event, no less), how do you expect your objects to move every time a button is pressed?
Jan 9, 2012 at 1:22am
Well, how would I create a loop so once I click the mouse button it just keeps moving and not resetting?
Jan 9, 2012 at 1:28am
You can store the mouse button state in a bool variable, for example.
Set it to true when the button is pressed and to false when it is released.
Then use the variable to decide whether you should move your objects each frame.
There's also a function to check the state of a mouse button (sf::Mouse::IsButtonPressed).
Jan 9, 2012 at 1:43am
Thank you for cooperating with me through this whole topic. I'm a c++/sfml newb so I'm sorry if you got frustrated with me. It was a class problem the whole time lol. Thanks again.
Topic archived. No new replies allowed.