Program Crashing

Hello, im just starting to learn c++ and im writing a text based adventure game and its crashing after it was finished.

UPDATE:

I updated the code, getting error Room1 was not declared, was still crashing before I started getting this error also.



Any help greatly appreciated thank you.

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <iostream>
#include <string>

using namespace std;


//                                   GAME FUNCTIONS
//---------------------------------------------------------------------------------------
class Game {
public:
    void startGame();
};

void Game::startGame()
{
    cout << "V0.0 \nWelcome to Lights Deceit, a text based RPG.\n\n\n"
            "In a prosperous kingdom a Lord stood upon the highest tower in "
            "the City of Xioni.\n"
            "He gazed upon his prosperous kingdon in awe, their was something "
            "about the way all the trees, and every spec of grass flowed "
            "in the wind like water.\n"
            "\nEvery time the Lord stood atop the tower it left him breathless, "
            "he couldn't believe that such a place could possibly exist."
            "\nThe castle was built up on the wall of a giant mountain, facing "
            "north their were colossal towers and stairwars leading to the "
            "seperate districts of the city."
            "\nTo the North-East their was "
            "On the third day of the third month, as spring was setting in and "
            "the trees would flow once again, he noticed something"
            "\n\n\nOnce their was a mighty Hero, in a war torn land. "
            "His journey began here... \n";	
}
//=======================================================================================



//                                 LOCATION FUNCTIONS
//---------------------------------------------------------------------------------------
class Location{
public:
	string Room1();
	string Chest1();
	string Window1();
	string Door1();
};

string Location::Room1()
{
	
	
cout << "You look around your small room, you see a chest, window, and a door.\n";

string action1;	

getline(cin, action1);

	if(action1 == "open chest")
	{Chest1();}
	else if(action1 == "open window")
	{Window1();}
	else if(action1 == "open door")
	{Door1();}
	
}
//=======================================================================================



//                                 OBJECT FUNCTION
//---------------------------------------------------------------------------------------
class Object{
public:	
	string Chest1();
	string Window1();
	string Door1();	
};

string Object::Window1()
{
cout << "You peek your head out the window and the sunshine greets your face" << endl;
	
		string openwindow1;
	
getline(cin, openwindow1);
	
if(openwindow1 == "leave")
{cout << "You jump out of the window and look around you, to the North there is a forest of dead trees and murk.\n" <<
"To the East the forest cuts off onto the mountainside, there seems to be a trail along the side of the mountain.\n" <<
"To the West the forest cuts off into a large plain.\n";
}
	return 0;
}

string Object::Chest1()
{
 cout << "\nYou find your rusty knife and clothes and gear up to set out" <<
 Room1();
	return 0;
}

string Object::Door1()
{
cout << "You step out into the tavern, theirs grubby men, beautiful ladies, and everything in between." << endl;
cout << "Near the barkeep a fight breaks out, a man in a tattered jacket sneaks up with a small shank to take advantage of the opportunity" << endl;
cout << "You think to yourself, could I attack him?\n Would it even be worth it?..\n";
cout << "I could just run and be on with my adventure, or perhaps I could wait and take the thief after hes had his way with his victims";
}
		
		
		

//=======================================================================================


//                                 CREATURE FUNCTION
//---------------------------------------------------------------------------------------
class Creature{
public:	
		
};
		
		
		

//=======================================================================================




//                                  ITEM FUNCTIONS
//---------------------------------------------------------------------------------------
			class Item
	{		public:
		
		
		
	};
//=======================================================================================

//                                  STATS FUNCTIONS
//---------------------------------------------------------------------------------------
			class Stats
	{		public:
		
		
		
	};
//=======================================================================================



//                                  CLASS FUNCTION
//---------------------------------------------------------------------------------------
			class Spec
	{		public:
		
		
		
	};
//=======================================================================================



//                                  DIALOGUE FUNCTION
//---------------------------------------------------------------------------------------
			class Dialogue
	{		public:
		
		void dialogue1()
{
cout << "You awake from your rest, you hear laughter and banter throught the door of your";
cout << "small room."<< endl;
cout << "You're in a tavern, one of the few safe havens left in this desolate world." << endl;
cout << "\nYou grasp the Stone on your neck, feeling the soothing warmth of its energy, what gift would it bestow you today?\n\n\n";
}
		
		
		
	};
//=======================================================================================



		int main()
{
	Game GameStart;
	GameStart.startGame();
	
	Dialogue Dialogue1;
	Dialogue1.dialogue1();
	
	Location Location1;
	Location1.Room1();

	return 0;
}
Last edited on
At lines 70-78

Should be either if-else or switch...

Also...it is wrong for openwindow to be string..
Openwindow should be void...and should not be outputted (that's what cause the error)
If it shouldnt be outputted how do I get that function to work where I want it to? Or my simply, why should it not be outputted?
Changed it to: void OpenWindow1 but yeah cant be outputted get compilation error
73 109[Note] cannot convert 'OpenWindow1()' (type 'void') to type 'const unsigned char*'

It was string so it could receive "leave" or "jump out" for getline(cin, openwindow1); (Line15)

Could you tell me what im doing wrong here or how to fix it?

I read I need to give it a parameter and call on the paramiter but I dont understand, Ive watched all of buckys videos on functions and classes and I still dont get it, cant find any examples using strings just numbers.An example or an explanation I could understand would be great Ive been at this for 2 days and its a little discouraging
Last edited on
In your function string OpenWindow1() you need to return a string - or change the return type to void. If you change to void then you need to remove << OpenWindow1(); from line 73
In string locationTavern1() you need to return a string not 0;
Thanks for the reply, I returned openwindow1 but nothings changed, and setting to void and removing that from line 73 would cause it not to run or do anything. Updated the code
Last edited on
Even if it was not string...it still could receive leave
What do you expect?
In GameStart.startgame(); you display the intro.
In Dialogue1.dialogue1(); you display some text
In Location1.locationTavern1(); you get a command and depending on this command you display some text or call string OpenWindow1() which displays more text and read a command. If the command is reconised you display a message and return the command.
Then the game ends

It seems you are dealing with a really large project, does it? Perhaps it could become difficult to manage it by yourself: it looks more like something that's worth be done with some friends. But that's not my business, of course.

Reading your comments, I had the impression your are a bit uncertain about the differences between classes and functions.
I've re-arranged your code to make things clearer. I hope it can be of some help.

Happy coding!

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <iostream>
#include <string>

using namespace std;

//                                   CLASS GAME
//------------------------------------------------------------------------------
class Game {
public:
    void startGame();
};

void Game::startGame()
{
    cout << "V0.0 \nWelcome to Lights Deceit, a text based RPG.\n\n\n"
            "In a prosperous kingdom a Lord stood upon the highest tower in "
            "the City of Xioni.\n"
            "He gazed upon his prosperous kingdon in awe, their was something "
            "about the way all the trees, and every spec of grass flowed "
            "in the wind like water.\n"
            "\nEvery time the Lord stood atop the tower it left him breathless, "
            "he couldn't believe that such a place could possibly exist."
            "\nThe castle was built up on the wall of a giant mountain, facing "
            "north their were colossal towers and stairwars leading to the "
            "seperate districts of the city."
            "\nTo the North-East their was "
            "On the third day of the third month, as spring was setting in and "
            "the trees would flow once again, he noticed something"
            "\n\n\nOnce their was a mighty Hero, in a war torn land. "
            "His journey began here... \n";	
}
//==============================================================================



//                                 CLASS LOCATION
//------------------------------------------------------------------------------
class Location {
public:
    string locationTavern1();
    string openWindow1();
};
    
string Location::locationTavern1()
{
    cout << "You get up and look around, you see a chest, window, and a door.\n";
    cout << "What do you do?"
            "\n   - Do you open the chest (\"open chest\")?"
            "\n   - Do you open the window (\"open window\")?"
            "\n   - Do you open the door (\"open door\")?\n";
    string action1;
    getline(cin, action1);

    if(action1 == "open chest") {
        cout << "\nYou find your rusty knife and clothes and gear up to set out "
                "on an adventure.\n";
    } else if(action1 == "open window") {
        cout << "\nYou peek your head out the window and peer into the distance."
                "\nWhat do you do?\n" << openWindow1();
    } else if(action1 == "open door") {
        cout << "\nYou step out into the tavern, theirs grubby men, beautiful "
                "ladies, and everything in between.\n"
                "Near the barkeep a fight breaks out, a man in a tattered "
                "jacket sneaks up with a small shank to take advantage of "
                "the opportunity\n"
                "You think to yourself, could I attack him?\n"
                "Would it even be worth it?..\n"
                "I could just run and be on with my adventure, or perhaps "
                "I could wait and take the thief after hes had his way with "
                "his victims";
    }
}

string Location::openWindow1()
{
    cout << "\nOk, the window is opened. Now what?"
            "\n   - Flee (\"leave\")!\n";
    string openwindow1;
    
    getline(cin, openwindow1);
    
    if(openwindow1 == "leave") {
        cout << "\nYou jump out of the window and look around you. To the North "
                "there is a forest of dead trees and murk.\n"
                "To the East the forest cuts off onto the mountainside, "
                "there seems to be a trail along the side of the mountain.\n"
                "To the West the forest cuts off into a large plain.\n";
    }
    return openwindow1;
}
//==============================================================================



//                                CLASS CREATURE
//------------------------------------------------------------------------------
class Creature {
public:
};
//==============================================================================



//                                 CLASS ITEM
//------------------------------------------------------------------------------
class Item {
public:
};
//==============================================================================



//                                  CLASS STATS
//------------------------------------------------------------------------------
class Stats {
public:
};
//==============================================================================



//                                  CLASS SPEC
//------------------------------------------------------------------------------
class Spec {
public:
};
//==============================================================================



//                                 CLASS DIALOGUE
//------------------------------------------------------------------------------
class Dialogue {
public:
    void dialogue1();
};

void Dialogue::dialogue1()
{
    cout << "\nYou awake from your rest, you hear laughter and banter throught "
            "the door of your small room.\n"
            "You're in a tavern, one of the few safe havens left in this "
            "desolate world."
            "\nYou grasp the Stone on your neck, feeling the soothing warmth "
            "of its energy, what gift would it bestow you today?\n\n\n";
}
//==============================================================================




int main()
{
    Game GameStart;
    GameStart.startGame();
    
    Dialogue Dialogue1;
    Dialogue1.dialogue1();
    
    Location Location1;
    Location1.locationTavern1();

    return 0;
}

I see that you've incorporated classes since your last thread. That's a good move. However, I think you need to consider what a class should represent. Classes should represent objects in your program. For example, a location can be an object, but a dialogue is generally not an object.

I don't know if you've learned inherited classes yet. If not, I would encourage you to look at them. I would make a Tavern class that derives from Location. The Tavern class would have all the actions (functions) that you could do in the Tavern. A Forest class would also derive from your Location class.

Likewise, I would make a Object class. Window and Chest classes would derive from the Object class. Window and Chest classes would have the actions (functions) you could perform upon a Window or Chest.

Your base classes (Location and Object) would have common functions used by their derived classes, such as getAction().

You also need to think about what your functions return and why they're returning something. For example, Location::locationTavern1() is declared to return a string, but you don't return a string, nor do you do anything with the return value when you call the function.

In numerous places you don't do anything if an input doesn't match any of the options. If an input is invalid, you should display an error message and repeat the question.
Last edited on
Thank you all for the replies, ill go over everything you guys told me and let you know how it turns out. Again much appreciated my dudes.
And it will be a large project but its for learning. After Im more confident in reading other peoples code I might consider getting some help on this thanks for the the input
Last edited on
Okay I updated the code, was working pretty well until I tried to link back to my room1, if I add


76 class Object{
77 public:
78 string Chest1();
79 string Window1();
80 string Door1();
81 string Room1();


I get error returned 1 exit status.
Been messing with this all day, looking at all sorts of projects that are similar. And still I dont know what the hell im doing wrong, I need to be able to link back to my previous functions but its seeming to me like this isnt possible.
Last edited on
Thank you all for the replies but I want to get into android app development with C++ and from what ive gathered making games in the console wont help me much with that, if you have an argument to that shoot but otherwise thread closed. Thank you all!
I don't know what the hell I'm doing wrong

If you provided the rest of the diagnostic information which appeared alongside
"returned 1 exit status"
that is, if you actually posted the error message, maybe someone could help you.

From what I've gathered making games in the console wont help me much

What do you believe would be different about C++ if you were developing a mobile app?

You should consider using Java if you're interested in Android development.
Topic archived. No new replies allowed.