What is wrong with my code?

Hello, I was trying to create this program for school, and I am not sure whats going wrong... Here's what I see in the debugger:

Checking for existence: C:\Users\Connor\Documents\C++ Files\EGTH -- Adventure\bin\Debug\EGTH -- Adventure.exe
Executing: "C:\Program Files\CodeBlocks/cb_console_runner.exe" "C:\Users\Connor\Documents\C++ Files\EGTH -- Adventure\bin\Debug\EGTH -- Adventure.exe" (in C:\Users\Connor\Documents\C++ Files\EGTH -- Adventure\.)
Process terminated with status 0 (0 minutes, 2 seconds)


The problem is that is closes once the code initiates the function Begin. And for the life of me, I can't figure out why.


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
81
82
83
84
85
86
87
88
89
#include <iostream>
using namespace std;
int Begin();
void ClassSelection();
int Answer;
char Name [20];
int Class;
int MainMenu();
int Version()
{
    cout << "Version: 0.0.2" << endl;
}
int Close()
{
    cout << "Thanks for playing!" << endl;
    return 0;
}

// -------------------
int main()
{
    Version();
    cout << "\n\n\n1. Play Game." << endl << "2. Description." << endl << "3. Exit" << endl;
    cout << endl;
    cin >> Answer;

    switch ( Answer )
    {
        case 1:
        Begin();
        break;
        case 2:
        cout << "In this game you go on missions and level up your player." << endl;
        main();
        break;
        case 3:
        Close();
        break;
        default:
        break;
    }
    Begin();
}
int Begin()
{
    ClassSelection();
}
void ClassSelection()
{
    cout << "Hello! Before we start, what's your name?" << endl;
    cin >> Name;
    cout << "Welcome, " << Name << " please choose a class." << endl << endl;
    cout << "CLASS --------- (STRENGTH) ---- (DEXTERITY) ---- (INTELLIGENCE) ---- (LUCK) ----" << endl;
    cout << endl;
    cout << "(1.) Soldier ----- [10] ---------- [06] ------------- [02] ---------- [02] -----" << endl;
    cout << "(2.) Thief ------- [02] ---------- [10] ------------- [03] ---------- [05] -----" << endl;
    cout << "(3.) Archer ------ [03] ---------- [12] ------------- [02] ---------- [03] -----" << endl;
    cout << "(4.) Scientist --- [01] ---------- [03] ------------- [15] ---------- [01] -----" << endl;
    cin >> Class;
    switch ( Class )
    {
        case 1:
        cout << "You have chosen [Soldier]." << endl;
        MainMenu();
        break;
        case 2:
        cout << "You have chosen [Thief]." << endl;
        MainMenu();
        break;
        case 3:
        cout << "You have chosen [Archer]." << endl;
        MainMenu();
        break;
        case 4:
        cout << "You have chosen [Scientist]." << endl;
        MainMenu();
        break;
        default:
        cout << "Error: Bad Input." << endl;
        ClassSelection();
        break;
    }
}
int MainMenu()
{
    cout << "-------------------Main Menu--------------------" << endl;
    cout << "\n(1.) Missions" << endl;
    cout << "(2.) Fight" << endl;
}
Last edited on
Line 46 should have empty parenthesis to be a function call. As it is it is probably being treated as a function pointer.
Hey webJose, thanks a lot! I have another question... Now that it works like it should, why is it that it repeats the function ClassSelection?

Like after i select the class I want, I get:

-------------------Main Menu--------------------
(1.) Missions
(2.) Fight

Hello! Before we start, what's your name?



Why do i get that?
I don't know. Put a breakpoint at the beginning of the ClassSelection() function and find out. When the breakpoint is reached, examine the call stack. It will show you exactly why it was called again. If you need to "see" what is happening, then set a breakpoint earlier in the code and then execute step by step.
closed account (o3hC5Di1)
Hi there,

I believe it might be because you are calling Begin() again at the end of your main() function.

Once the program reaches the switch construct it calls Begin(), once that has finished executing, it will move on to the first statement after the switch construct, in your case being Begin().

Hope that helps.

All the best,
NwN
Topic archived. No new replies allowed.