Stroustrup has an exercise in which the reader writes a program for the wumpus hunt game. A first draft of my code is below. It compiles and runs partly correctly. If you choose to move to a different room the program runs as expected. If you choose to shoot at the wumpus, I get something called run-time error #3. It seems that the room I call 'current' is being perceived as a variable. The error description is that the variable 'current' is not initialized.

I don't understand that error because the 'current' room variables are not used in the lines in question.

Here is my code. It is not a final version.

#include "../../std_lib_facilities.h"

class Room {
public:
	double rand_wumpus;
	double rand_pit;
	double rand_bat;
	bool wumpus_in_room;
	bool pit_in_room;
	bool bat_in_room;
	int room_num;
};

int main() {
cout << "You are safe and in room 100!" << endl;

for (int i = 0; i < 5; i++) {
Room adjacent1;
    srand((unsigned)time(0));
    adjacent1.room_num = 120;
    adjacent1.rand_wumpus = 100.0 * rand()/RAND_MAX;
    adjacent1.rand_pit = 100.0 * rand()/RAND_MAX;
    adjacent1.rand_bat = 100.0 * rand()/RAND_MAX;

    if (adjacent1.rand_wumpus > 30.1 && adjacent1.rand_wumpus < 47.8) {
		adjacent1.wumpus_in_room = true;
	} else {
		adjacent1.wumpus_in_room = false;
	}

    if (adjacent1.rand_pit > 83.1 && adjacent1.rand_pit < 95.5) {
		adjacent1.pit_in_room = true;
	} else {
		adjacent1.pit_in_room = false;
	}

	if (adjacent1.rand_bat > 22.7 && adjacent1.rand_bat < 40.4) {
		adjacent1.bat_in_room = true;
	} else {
		adjacent1.bat_in_room = false;
	}

Room adjacent2;
    srand((unsigned)time(0));
    adjacent2.room_num = 150;
    adjacent2.rand_wumpus = 100.0 * rand()/RAND_MAX;
    adjacent2.rand_pit = 100.0 * rand()/RAND_MAX;
    adjacent2.rand_bat = 100.0 * rand()/RAND_MAX;

    if (adjacent2.rand_wumpus > 56.0 && adjacent2.rand_wumpus < 68.3) {
		adjacent2.wumpus_in_room = true;
	} else {
		adjacent2.wumpus_in_room = false;
	}

    if (adjacent2.rand_pit > 5.8 && adjacent2.rand_pit < 22.4) {
		adjacent2.pit_in_room = true;
	} else {
		adjacent2.pit_in_room = false;
	}

	if (adjacent2.rand_bat > 28.2 && adjacent2.rand_bat < 46.5) {
		adjacent2.bat_in_room = true;
	} else {
		adjacent2.bat_in_room = false;
	}

Room adjacent3;
    srand((unsigned)time(0));
    adjacent3.room_num = 160;
    adjacent3.rand_wumpus = 100.0 * rand()/RAND_MAX;
    adjacent3.rand_pit = 100.0 * rand()/RAND_MAX;
    adjacent3.rand_bat = 100.0 * rand()/RAND_MAX;

    if (adjacent3.rand_wumpus > 52.3 && adjacent3.rand_wumpus < 75.7) {
		adjacent3.wumpus_in_room = true;
	} else {
		adjacent3.wumpus_in_room = false;
	}

    if (adjacent3.rand_pit > 57.4 && adjacent3.rand_pit < 75.2) {
		adjacent3.pit_in_room = true;
	} else {
		adjacent3.pit_in_room = false;
	}

    if (adjacent3.rand_bat > 44.9 && adjacent3.rand_bat < 60.2) {
		adjacent3.bat_in_room = true;
	} else {
		adjacent3.bat_in_room = false;
	}

Room current;
    cout << "There are passage ways to rooms " << adjacent1.room_num <<", " << adjacent2.room_num <<", and " << adjacent3.room_num <<"." << endl;

    if (adjacent1.wumpus_in_room == true || adjacent2.wumpus_in_room == true || adjacent3.wumpus_in_room == true) {
		cout << "I smell the wumpus." << endl;
	}

    if (adjacent1.pit_in_room == true || adjacent2.pit_in_room == true || adjacent3.pit_in_room == true) {
		cout << "I feel a breeze." << endl;
	}

	if (adjacent1.bat_in_room == true || adjacent2.bat_in_room == true || adjacent3.bat_in_room == true) {
		cout << "I hear a bat." << endl;
	}

    cout << endl;
    cout << "Do you wish to move or shoot?" << endl;

    string play = " ";
    cin >> play;

	srand((unsigned)time(0));
	double prob_wumpus = rand()/RAND_MAX;

	if (play == "s120") {
		if (adjacent1.wumpus_in_room == true) {
			cout << "You shot the wumpus!  YOU WIN!!!" << endl;
		}
	    if (adjacent1.wumpus_in_room == false) {
		    if (prob_wumpus <= 0.70) {
			    cout << "You missed the wumpus.  Play again." << endl;
		    }
		    if (prob_wumpus > 0.70) {
			    cout << "You missed the wumpus.  It has come into your room and eaten you!" << endl;
	            }
	    }
	}

	if (play == "s150") {
		if (adjacent1.wumpus_in_room == true) {
			cout << "You shot the wumpus!  YOU WIN!!!" << endl;
		}
	    if (adjacent1.wumpus_in_room == false) {
		    if (prob_wumpus <= 0.50) {
			    cout << "You missed the wumpus.  Play again." << endl;
		    }
		    if (prob_wumpus > 0.50) {
			    cout << "You missed the wumpus.  It has come into your room and eaten you!" << endl;
	            }
	    }
	}

	if (play == "s160") {
		if (adjacent1.wumpus_in_room == true) {
			cout << "You shot the wumpus!  YOU WIN!!!" << endl;
		}
	    if (adjacent1.wumpus_in_room == false) {
		    if (prob_wumpus <= 0.60) {
			    cout << "You missed the wumpus.  Play again." << endl;
		    }
		    if (prob_wumpus > 0.60) {
			    cout << "You missed the wumpus.  It has come into your room and eaten you!" << endl;
	            }
	    }
	}

	if (play == "m120") {
		current.room_num = 120;
		current.wumpus_in_room = adjacent1.wumpus_in_room;
		current.pit_in_room = adjacent1.pit_in_room;
		current.bat_in_room = adjacent1.bat_in_room;
	}

	if (play == "m150") {
		current.room_num = 150;
		current.wumpus_in_room = adjacent2.wumpus_in_room;
		current.pit_in_room = adjacent2.pit_in_room;
		current.bat_in_room = adjacent2.bat_in_room;
	}

	if (play == "m160") {
		current.room_num = 160;
		current.wumpus_in_room = adjacent3.wumpus_in_room;
		current.pit_in_room = adjacent3.pit_in_room;
		current.bat_in_room = adjacent3.bat_in_room;
	}

	if (current.wumpus_in_room == true) {cout << "The wumpus is in the room.  You are eaten!" << endl;} 
	
	if (current.pit_in_room == true) {cout << "You have fallen into a pit and died!" << endl;}

	if (current.bat_in_room == true) {cout << "A bat has taken you to another room!" << endl;}

	if (current.wumpus_in_room == false && current.pit_in_room == false && current.bat_in_room == false) {
		cout << "You are safe and in room " << current.room_num <<"!" << endl;
	}
}

	keep_window_open();
        return 0;
}

Please explain the error to me and whether I am close to or far from the right track for this game. Thanks in advance.

By this, it would suggest that either the variable has not yet been defined, or has not been defined in the same scope as where you are trying to call it (global scope, function scope etc.). Could you post the error and tell us the line which gives the error?

OK. I found my error. When a player decides to shoot the wumpus instead of move into an adjacent room, lines 159 through 178 are not executed. These are the lines in which the variables in the current room, such as current.wumpus_in_room, are determined. Lines 180 through 188 should also be by-passed but, in error, are not. When the computer tries to execute those lines, it finds that there are no values for those variables. I am going to put a conditional on them.

So is your problem resolved now?

So is your problem resolved now?

No. The code is far from finished. I am having some loop difficulties. I want to get the code to a state where a major revision is the next appropriate stage. Then I will consider this thread solved and start another one.

OK so what is the current issue you are having?

I changed Room from class to struct and I call srand() just once.

The issue I have now relates to the loop I am trying to make. For testing purposes I used a for loop, but the game needs a while loop. When I try to implement a while loop using a flag to indicate when the loop should end, the program goes straight to the end. It does not enter or execute the loop at all. The flag code is commented out in this version.

I would rather that the program go directly to the "Game Over" line immediately when the flag is set to 1, but first things first.

Here is the revised code.

#include "../../std_lib_facilities.h"

struct Room {
	double rand_wumpus;
	double rand_pit;
	double rand_bat;
	bool wumpus_in_room;
	bool pit_in_room;
	bool bat_in_room;
	int room_num;

};

int main() {
//int flag = 0;
cout << "You are safe and in room 100!" << endl;
srand((unsigned)time(0));

for (int i = 0; i < 5; i++) {
//while (flag = 0) {
Room adjacent1;
	adjacent1.room_num = 120;
    adjacent1.rand_wumpus = 100.0 * rand()/RAND_MAX;
    adjacent1.rand_pit = 100.0 * rand()/RAND_MAX;
    adjacent1.rand_bat = 100.0 * rand()/RAND_MAX;

    if (adjacent1.rand_wumpus > 30.1 && adjacent1.rand_wumpus < 47.8) {
		adjacent1.wumpus_in_room = true;
	} else {
		adjacent1.wumpus_in_room = false;
	}

    if (adjacent1.rand_pit > 83.1 && adjacent1.rand_pit < 95.5) {
		adjacent1.pit_in_room = true;
	} else {
		adjacent1.pit_in_room = false;
	}

	if (adjacent1.rand_bat > 22.7 && adjacent1.rand_bat < 40.4) {
		adjacent1.bat_in_room = true;
	} else {
		adjacent1.bat_in_room = false;
	}

Room adjacent2;
	adjacent2.room_num = 150;
    adjacent2.rand_wumpus = 100.0 * rand()/RAND_MAX;
    adjacent2.rand_pit = 100.0 * rand()/RAND_MAX;
    adjacent2.rand_bat = 100.0 * rand()/RAND_MAX;

    if (adjacent2.rand_wumpus > 56.0 && adjacent2.rand_wumpus < 68.3) {
		adjacent2.wumpus_in_room = true;
	} else {
		adjacent2.wumpus_in_room = false;
	}

    if (adjacent2.rand_pit > 5.8 && adjacent2.rand_pit < 22.4) {
		adjacent2.pit_in_room = true;
	} else {
		adjacent2.pit_in_room = false;
	}

	if (adjacent2.rand_bat > 28.2 && adjacent2.rand_bat < 46.5) {
		adjacent2.bat_in_room = true;
	} else {
		adjacent2.bat_in_room = false;
	}

Room adjacent3;
	adjacent3.room_num = 160;
    adjacent3.rand_wumpus = 100.0 * rand()/RAND_MAX;
    adjacent3.rand_pit = 100.0 * rand()/RAND_MAX;
    adjacent3.rand_bat = 100.0 * rand()/RAND_MAX;

    if (adjacent3.rand_wumpus > 52.3 && adjacent3.rand_wumpus < 75.7) {
		adjacent3.wumpus_in_room = true;
	} else {
		adjacent3.wumpus_in_room = false;
	}

    if (adjacent3.rand_pit > 57.4 && adjacent3.rand_pit < 75.2) {
		adjacent3.pit_in_room = true;
	} else {
		adjacent3.pit_in_room = false;
	}

    if (adjacent3.rand_bat > 44.9 && adjacent3.rand_bat < 60.2) {
		adjacent3.bat_in_room = true;
	} else {
		adjacent3.bat_in_room = false;
	}

Room current;
	cout << "There are passage ways to rooms " << adjacent1.room_num <<", " << adjacent2.room_num <<", and " << adjacent3.room_num <<"." << endl;

    if (adjacent1.wumpus_in_room == true || adjacent2.wumpus_in_room == true || adjacent3.wumpus_in_room == true) {
		cout << "I smell the wumpus." << endl;
	}

    if (adjacent1.pit_in_room == true || adjacent2.pit_in_room == true || adjacent3.pit_in_room == true) {
		cout << "I feel a breeze." << endl;
	}

	if (adjacent1.bat_in_room == true || adjacent2.bat_in_room == true || adjacent3.bat_in_room == true) {
		cout << "I hear a bat." << endl;
	}

    cout << endl;
    cout << "Do you wish to move or shoot?" << endl;

    string play = " ";
    cin >> play;

	srand((unsigned)time(0));
	double prob_wumpus = rand()/RAND_MAX;

	if (play == "s120") {
		if (adjacent1.wumpus_in_room == true) {
			cout << "You shot the wumpus!  YOU WIN!!!" << endl;
//			flag = 1;
		}
	    if (adjacent1.wumpus_in_room == false) {
		    if (prob_wumpus <= 0.70) {
			    cout << "You missed the wumpus.  Play again." << endl;
		    }
		    if (prob_wumpus > 0.70) {
			    cout << "You missed the wumpus.  It has come into your room and eaten you!" << endl;
//			    flag = 1;
	        }
		}
	}

	if (play == "s150") {
		if (adjacent1.wumpus_in_room == true) {
			cout << "You shot the wumpus!  YOU WIN!!!" << endl;
//			flag = 1;
		}
	    if (adjacent1.wumpus_in_room == false) {
		    if (prob_wumpus <= 0.50) {
			    cout << "You missed the wumpus.  Play again." << endl;
		    }
		    if (prob_wumpus > 0.50) {
			    cout << "You missed the wumpus.  It has come into your room and eaten you!" << endl;
//			    flag = 1;
	        }
		}
	}

	if (play == "s160") {
		if (adjacent1.wumpus_in_room == true) {
			cout << "You shot the wumpus!  YOU WIN!!!" << endl;
//			flag = 1;
		}
	    if (adjacent1.wumpus_in_room == false) {
		    if (prob_wumpus <= 0.60) {
			    cout << "You missed the wumpus.  Play again." << endl;
		    }
		    if (prob_wumpus > 0.60) {
			    cout << "You missed the wumpus.  It has come into your room and eaten you!" << endl;
//			    flag = 1;
	        }
		}
	}

	if (play == "m120") {
		current.room_num = 120;
		current.wumpus_in_room = adjacent1.wumpus_in_room;
		current.pit_in_room = adjacent1.pit_in_room;
		current.bat_in_room = adjacent1.bat_in_room;
	}

	if (play == "m150") {
		current.room_num = 150;
		current.wumpus_in_room = adjacent2.wumpus_in_room;
		current.pit_in_room = adjacent2.pit_in_room;
		current.bat_in_room = adjacent2.bat_in_room;
	}

	if (play == "m160") {
		current.room_num = 160;
		current.wumpus_in_room = adjacent3.wumpus_in_room;
		current.pit_in_room = adjacent3.pit_in_room;
		current.bat_in_room = adjacent3.bat_in_room;
	}

	if (play == "m120" || play == "m150" || play == "m160") {
	if (current.wumpus_in_room == true) {
		cout << "The wumpus is in the room.  You are eaten!" << endl;
//		flag = 1;
	} 
	
	if (current.pit_in_room == true) {
		cout << "You have fallen into a pit and died!" << endl;
//		flag = 1;
	}

	if (current.bat_in_room == true) {cout << "A bat has taken you to another room!" << endl;}

	if (current.wumpus_in_room == false && current.pit_in_room == false && current.bat_in_room == false) {
		cout << "You are safe and in room " << current.room_num <<"!" << endl;
	}
	}
}
    cout << "Game Over." << endl;

	keep_window_open();
    return 0;
}

Stupid me! Nevermind. I just noticed that line 20 should be

while (flag == 0)

.

I am marking this thread as solved. The next adjustments are major revisions. For example, I need to randomize the adjacent rooms. When I have gone as far as I can with that, I will ask for comments in a new thread.

Yup. Why is your while loop inside a for loop? Surely this would mean that each time your while loop is not accepted the for loop will increase by one until five where it will not run?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.