C++ End Function

Hi there.

I'm pretty new to C++ and am trying to make a text-based RPG. I'm having some trouble making an exit function. Here is a snippet

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
#include <iostream>
#include <string>
#include <ctime>
#include <windows.h>
using namespace std;

bool done = false;

void CheckUpdate();
void StartScreen();
void CreateName();
void CreatePlayer();
void MainMenu();
void Menu();
void Multiplayer();
void Difficulty();
void Exit();
void Tutorial();
void Game();
void Casino();
void Credits();
void Store();
void DisplayStats();

string name;
string race;
string difficulty;

int str, attack, def, hp, money, speed, expr, expcap;
int mana, lvl, maxhp, maxmana;

time_t ttime = (NULL);
int itime = ttime;
void main()
{
	srand(itime);
	CheckUpdate();
	StartScreen();
	MainMenu();

	while(!done)
	{
		MainMenu();
	}
}

void Exit()
{
	system("cls")
	string choice;
	cout << "RAGEQUITZ????? y/n";
	cin >> choice;

	if(choice == "y" || choice == "Y")
	{
		done = true;
	}
	else
	{
		system("cls");
	}
}




Please help. when executed, it does nothing.

Thanks in advanced!!!
Last edited on
use return;
To exit the code whenever you want.
void main()

This is wrong. The standard recognises no such construct. If your compiler doesn't even warn you, get a compiler that does. If you had a better compiler, it would have told you about this and then you would have realised yourself that you're missing a

return;
you are just defining Exit() Function nowhere you r executing it like Exit();, while does'nt get any return
unless you plan to add a GUI then you don't need to #include <windows.h> .
@king214
i'm using the Sleep command so i used #include <windows.h>
i'm using the Sleep command so i used #include <windows.h>


ok.
You need call atexit() with the function you want register to be called when you execute exit().

#include <stdlib.h>

int atexit(void (*function)(void));
Try this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Exit()
{
	system("cls")
	char choice;
	cout << "RAGEQUITZ????? y/n";
	cin >> choice;

	if(choice == 'y' || choice == 'Y')
	{
		done = true;
	}
	else
	{
		system("cls");
	}
}
Topic archived. No new replies allowed.