Exiting a program properly

If I am nested within two while() loops, how would I exit the program properly without having to use exit()? I know exit() does not unstack and return does. I basically just need to leave both while loops so the program will actually get to the return 0 of the main function.
I'm not sure what you're asking, but break or continue might be what you're looking for.
Well, the thing is, I'm using SFML to test and see if the escape key was pressed, and if it was, it is supposed to exit the program. This is just for me so I have a way of ending the program while I write it. The problem is, I can get the window to close, but the program never officially ends. I have to go into the task manager and kill the process. The code in question is below.

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
	while (window.isOpen())
	{
		// handle events
		sf::Event event;
		while (window.pollEvent(event))
		{
			switch (event.type)
			{
			case sf::Event::KeyPressed:
				if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
				{
					window.close();
					break;
				}
				if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
				{
					int newX = p.tileX;
					int newY = p.tileY;
					colTest = level[newX + (newY * 10)];
					if (colTest == 0)
					{
						playerSprite.setTextureRect(sf::IntRect(576, 0, 64, 128));
					}
					else{
						p.walking(p.tileX, p.tileY, 0, window, playerSprite, map);
					}
				}
				if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
				{
					int newX = p.tileX;
					int newY = p.tileY + 2;
					colTest = level[newX + (newY * 10)];
					if (colTest == 0)
					{
						playerSprite.setTextureRect(sf::IntRect(0, 0, 64, 128));
					}
					else{
						p.walking(p.tileX, p.tileY, 1, window, playerSprite, map);
					}
				}
				if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
				{
					int newX = p.tileX - 1;
					int newY = p.tileY + 1;
					int colTest = level[newX + (newY * 10)];
					if (colTest == 0)
					{
						playerSprite.setTextureRect(sf::IntRect(192, 0, 64, 128));
					}
					else{
						p.walking(p.tileX, p.tileY, 2, window, playerSprite, map);
					}
				}
				if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
				{
					int newX = p.tileX + 1;
					int newY = p.tileY + 1;
					int colTest = level[newX + (newY * 10)];
					if (colTest == 0)
					{
						playerSprite.setTextureRect(sf::IntRect(384, 0, 64, 128));
					}
					else{
						p.walking(p.tileX, p.tileY, 3, window, playerSprite, map);
					}

				}
				break;
			}
		}

		// draw the map
		window.clear();
		window.draw(map);
		window.draw(playerSprite);
		window.display();
	}

	return EXIT_SUCCESS;
}

Basically, the while loop is only true if the window is open. When escape is pressed, the window closes. Now, because the window closes, the while loop is no longer true, so the next thing that runs is the return EXIT_SUCCESS command. That's what I'm not understanding. I still have to hard kill the program with the task manager. I don't know if this matters, but this is a windows application, not a console application.
I'm not familiar with SFML, but what you have seems like a strange way to go about exiting a program. I'm not so sure that you should even have the events inside a separate while loop to begin with either? I like to use flags to exit while loops. It seems to me it should look something more like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool play = true;
while(play)
{
    switch(input)
    {
    case escape:
        play = false;
        break;
    case w:
        turn left;
        break;
    etc. 
    }
}


Without knowing what all it does, I would think that EXIT_SUCCESS would close the window as part of its routine? I'm pretty much just guessing, but at worst I shouldn't be too far off.
In SFML just as in many others, closing a windows does not necessarily mean destroying it. So after closing the window(not showing it to the user but running in background), you must destroy it with the destroy(); member function.

@admkrk
I'm not familiar with SFML, but what you have seems like a strange way to go about exiting a program. I'm not so sure that you should even have the events inside a separate while loop to begin with either? I like to use flags to exit while loops. It seems to me it should look something more like this

SFML conventions...

EXIT_SUCCESS is a constant equal to 0

Aceix.
Last edited on
OK thanks, I figured it was a macro that ran the destructor(s) along with some other clean-up stuff. I still think the nested whiles are unnecessary and confusing though, but that might just be the way the library was designed.
Topic archived. No new replies allowed.