Problem with a tutorial

Hi guys I am new to C++ but I have completed some basic tutorials and started a tutorial to make my own text based RPG. The link to the tutorial is http://www.penguinprogrammer.co.uk/c-rpg-tutorial/the-title-screen

Every time I try and follow this tutorial I seem to get a problem when it comes to adding in the functions as directed in the tutorial. No matter what I do I seem to keep getting errors when I add in the function under the int main

void titleFunc() {

cout << "\t\t\t\t---Fantasee---\n\n\n"; // Display the title

cout << "\t\t\t\t 1: Play\n"; // Display the options

return;

I have already declared this function above main and have followed the tutorial to the letter numerous times and I was wondering if anyone can spot what I am doing wrong or is this a problem in the tutorial.

Thanks all in advance.

iam using codeblocks 12.11
Last edited on
Have you included the following?

1
2
#include <iostream> 
using namespace std;


If you have, you're going to need to show us more of your code.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.

Last edited on
Heya thanks for a quick reply, I tried changing various things to make it work but I think I have set it back to what the tutorial wanted.

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
#include <iostream>

using namespace std;

bool running = 1;

void titleFunc();

int userInput = 0;

int main()
{
    void titleFunc() {
    cout << "\t\t\t\t---Fantasee---\n\n\n";
    cout << "\t\t\t\t   1: Play\n";

    cin >> userInput;

    if(userInput == 1) {
    }
    else {
        running = 0;
    }
    return;
    }

    titleFunc();

    while(running) {
        }

    return 0;
}
You cannot define a function inside another function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void titleFunc();

int main(){
   titleFunc();
   return 0;
}

void titleFunc(){
    cout << "\t\t\t\t---Fantasee---\n\n\n";
    cout << "\t\t\t\t   1: Play\n";

    int userInput(0);
    cin >> userInput;

    if(userInput == 1) {
    }
    else {
        running = 0;
    }
    //return;
}


Random Note:
I commented out the return in titleFunc() because it is pointless to have one at the end of a void function.
Don't put the definition of titleFunc() inside main(), put it outside, at the same scope as, say, line 7.
The function titleFunc() should not be defined in main().

put it before or after.

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
#include <iostream>

using namespace std;

bool running = 1;

void titleFunc();

int userInput = 0;

int main()
{
    

    titleFunc();

    while(running) {
        }

    return 0;
}

void titleFunc() {
    cout << "\t\t\t\t---Fantasee---\n\n\n";
    cout << "\t\t\t\t   1: Play\n";

    cin >> userInput;

    if(userInput == 1) {
    }
    else {
        running = 0;
    }
    return;
    }


Like so.

Could you tell us what errors you are getting?
hey thx again, the errors I where getting were - error a function-definition is not allowed before "{" and when I originally tried fixing it I got many more errors (I think I broke it more).

I am a little worried about this tutorial now if it is showing me step by step the wrong code. Does anyone know of any good beginning game design tutorials for a text based RPG.
Last edited on
I am a little worried about this tutorial now if it is giving showing me step by step the wrong code.

Looks fine to me.

This is what I get when I follow the instruction on the page you posted a link to. It compiles and runs fine, though you have to ctrl-C to get out of it. (And looks pretty much the same as Superdude's take.)

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
#include<iostream>
using namespace std;
 
bool running = 1;

int userInput = 0;
 
void titleFunc(); // Handles the title screen
 
int main() {
    
    titleFunc();

    while(running) {
    }
 
    return 0;
}

void titleFunc() {
    cout << "\t\t\t\t---Fantasee---\n\n\n";
    cout << "\t\t\t\t   1: Play\n";
 
    cin >> userInput;
 
    if(userInput == 1) {
        /* We will insert the new game code here and then the
           program will continue in its loop, playing the game */
    }
    else {
        running = 0; // If the user does not enter a valid command, we will terminate the program
    }
    return;
}


Like Daleth, I think the return; at the end of titleFunc() is pointless, though benign.

Andy
Last edited on
ahh ok thanks alot Andy, I think its me making silly mistakes and reading things wrong. Ive spent the last 7 days non stop reading code and doing tutorials I think my brain is melting.

I will try and get through the rest of the tutorial, wish me luck.
Last edited on
Hi guys just a quick question, I have followed the tutorial even further and I was just wondering why the else (Line 38) in my if statement does not appear to be running correctly. I can press any button and the program will carry on through the next choices.

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
#include <iostream>
using namespace std;

bool running = 1;   //allows easy termination of the program
void titleFunc();   //Title Screen Function
int userInput = 0;
void newGameFunc(); //Handles creating a new game
int playerInfo[2];  //change to however many options the player is going to pick
int playerLocation = 0; //Stores a numerical reference to the location of the player

int main(){
    titleFunc();
    newGameFunc();
    while(running){ //Will continue to loop while running is one
        if(playerLocation == 1){
            cout << "You are standing in the middle of a forest. A path veers off to the East and to the West\n";
            cout << " 1: Go East\n 2: Go West\n";
            cin >> userInput;

            if(userInput == 1) playerLocation = 2; //East
            else if (userInput == 2) playerLocation = 3; //West
        }
    }

    return 0;
}

    void titleFunc() {
        cout << "\t\t\t\t---Dungeon Raiders---\n\n\n";  //displays title
        cout << "\t\t\t\t   1: Play\n";                 //displays the options

        cin >> userInput;

        if(userInput == 1) {
                newGameFunc();  //creates a new game
        }

        else {
            running = 0;    //terminate if user does not enter a valid command
        }
        return;
    }
    void newGameFunc() {
        cout << "Welcome to Dungeon Raiders, a world of adventure and danger.\n";
        cout << "Since you are a new hero, why don't you tell me a little about yourself?\n";
        cout << "For starters, are you Male or Female?\n 1: Male\n 2: Female\n";
        cin >> userInput;
        playerInfo[0] = userInput;  //store the players gender in the relevant section of the array

        cout << "And what kind of person are you?\n 1: Warrior\n 2: Archer\n 3: All-rounder\n";
        cin >> userInput;
        playerInfo[1] = userInput; // store what class the player is in the array
        playerLocation = 1; // Place the player in the start area

    return;
    }


Iam also seeing that I need to select my character class and gender twice before it lets me enter the game.
Last edited on
Titlefunc calls newgamefunc. then in main, you call it again.
ahh that makes sense now, thanks superdude.
Topic archived. No new replies allowed.