Increasing Scope help?

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
You're using the wrong = on line 3. I think that's your real issue, not scope.

-Albatross
= is an assignment operator, == is comparison operator.
Either way, it is printing 6...
That's what makes me think that perhaps the OP was just using an example and that his real code is somewhat different.

-Albatross
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?
No. Just declare the variable in the appropriate scope.
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.
Well, and that's exactly what the code does.
So where is the problem?
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
@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?
So provide a proper example.
So far it seems that your "situation" exists only in your imagination.
Last edited on
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.
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
I have a while statement right after that with a poll event. Should I place the switch statement in that while statement?
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?
Well, how would I create a loop so once I click the mouse button it just keeps moving and not resetting?
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).
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.