Need help with RPG, calling functions

What I have so far is two rooms, A and B and each room has it's own function.. how am I supposed to make it so the rooms can be called amongst eachother so the player can navigate, without getting this error:

In function 'voidA()'
'B' undeclared (first use this function)
In function 'void B()'
'void B()' used prior to declaration

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
90
91
92
93
#include<iostream>
#include<windows.h>
using namespace std;

    char buffer;
    bool skeleton = true;
    bool rat = true;
    bool Ckey = false;
    bool Ekey = false;
    bool boss = true;
    int a, b;

void A()
{
    system("CLS");
    cout << "This is the spawn room " << endl;
    cout << "Possible exits are E and N " << endl;
    cin  >> buffer;
    
    if(buffer == 'E' || buffer == 'e')
    {
        B();
    }
    
    if(buffer == 'N' || buffer == 'n')
    {
        if(Ckey == false)
        {
            system("CLS");
            cout << "The door is locked" << endl;
            system("PAUSE");
            A();
         }
    }
}        

void B()
{
    srand(time(0));
    
    if(rat == true)
{
        system("CLS");
        cout << "This is just some room... " << endl;
        Sleep(3000);
        system("CLS");
        cout << "..." << endl;
        Sleep(2000);
        cout << "A rat attacks you! " << endl;
        cout << "You have a 90% chance to win. " << endl << endl;
        system("PAUSE");
        
        a = rand()%100;
        
        if(a > 9)
        {
            rat = false;
            B();
        }
        else if(a < 9)
        {
            Ckey = true;
            rat = true;
            system("CLS");
            cout << "You defeated the rat!" << endl;
            cout << "The rat was carrying a key!" << endl;
            B();
        }
}

    else if(rat == false)
    {
        system("CLS");
        cout << "This is the room you fought the rat in " << endl;
        cout << "Not much else is in here..." << endl;
        cout << "Only exit is W" << endl;
        cin  >> buffer;
        
        if(buffer == 'W' || buffer == 'w')
        {
            A();
        }
    }
}
            
int main()
{
    A();
    system("PAUSE");
    return 0;
}

get rid of ALL the system functions. second, this is a perfect example of why there is a standard in c++ for declaring functions. the standard is this and will fix your problem.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int addition(int i, int j);//declare functions and formal arguments here
int subraction(int i, int j);

int main(){
    std::cout << addition(7,3);
    std::cout << '\n' << subraction(4, 2);
    return 0;
}
//code for what the function does here. 
int addition(int i, int j){
    return i+j; 
}
int subraction(int i, int j){
    return i-j; 
}
also a tip, try not to name variables THIS close to your functions. int a, b;
Last edited on
Hey, just try declaring your variables as prototypes before you call them.
All i did was move a couple things around and your program worked just fine.

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
90
91
92
93
94
#include<iostream>
#include<windows.h>
using namespace std;

void A();
void B();

    char buffer;
    bool skeleton = true;
    bool rat = true;
    bool Ckey = false;
    bool Ekey = false;
    bool boss = true;
    int a, b;
            
int main()
{
    A();
    system("pause");
    return 0;
}

void A()
{
    system("cls");
    cout << "This is the spawn room " << endl;
    cout << "Possible exits are E and N " << endl;
    cin  >> buffer;
    
    if(buffer == 'E' || buffer == 'e')
    {
        B();
    }
    
    if(buffer == 'N' || buffer == 'n')
    {
        if(Ckey == false)
        {
            system("cls");
            cout << "The door is locked" << endl;
            system("pause");
            A();
         }
    }
}        

void B()
{
    srand(time(0));
    
    if(rat == true)
    {
        system("cls");
        cout << "This is just some room... " << endl;
        Sleep(3000);
        system("cls");
        cout << "..." << endl;
        Sleep(2000);
        cout << "A rat attacks you! " << endl;
        cout << "You have a 90% chance to win. " << endl << endl;
        system("pause");
        
        a = rand()%100;
        
        if(a > 9)
        {
            rat = false;
            B();
        }
        else if(a < 9)
        {
            Ckey = true;
            rat = true;
            system("cls");
            cout << "You defeated the rat!" << endl;
            cout << "The rat was carrying a key!" << endl;
            B();
        }
    }

    else if(rat == false)
    {
        system("cls");
        cout << "This is the room you fought the rat in " << endl;
        cout << "Not much else is in here..." << endl;
        cout << "Only exit is W" << endl;
        cin  >> buffer;
        
        if(buffer == 'W' || buffer == 'w')
        {
            A();
        }
    }
}
1
2
3
4
  int a, b;

void A()
{

try to name your functions and variables better. DO NOT USE GLOBAL VARIABLES, and DO NOT USE SYSTEM COMMANDS.
try reworking this mess
1
2
3
4
5
6
7
8
9
system("cls");
        cout << "This is just some room... " << endl;
        Sleep(3000);
        system("cls");
        cout << "..." << endl;
        Sleep(2000);
        cout << "A rat attacks you! " << endl;
        cout << "You have a 90% chance to win. " << endl << endl;
        system("pause");

and all your other system commands.
If you're using windows, heres a handy clear screen function that clears the screen without using system commands:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void cls() //windows h function to replace screen with nulls
{
  DWORD n;
  DWORD size;
  COORD coord = {0};
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
  GetConsoleScreenBufferInfo ( h, &csbi );
  size = csbi.dwSize.X * csbi.dwSize.Y;
  FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
  GetConsoleScreenBufferInfo ( h, &csbi );
  FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );
  SetConsoleCursorPosition ( h, coord );
}


as for your system pause, use cin.get() or simply cin to pause for the user.
Last edited on
remember the sleep function should also be remade.
Topic archived. No new replies allowed.