I have to write a code that allows "x" to move between 4 rooms to find a treasure. I think I have the code done except that I initilize the variable room = 1 (Line 114) to allow the program to work correctly. After the first function the result gets overwritten again by the 1 and I don't know how to fix this. Yes, this is a homework assignment. I am just asking for help on this one problem. I have looked everywhere and can't figure it out. The only thing that works is the ones when room = 1 so the function returns the return and then it is overwritten by the room = 1 statement (Line 114)(I think). I don't know how to solve this problem.
Because you need to pass room by reference, not by value. Simply, you need to tell the compiler that you want to all the variable, room, to be modified by the function, and also modify the local variable. This is quite simple in your program. In your render function definition, change this line: float render(int x, int room)
To: float render(int x, int &room)
Also, why are you defining render as a float function? You're not returning any values from the function. Would void not work?
Thank you so much. I have been working on this for over a week. I just started programming and only started the class 3 weeks ago. I tried to use void but it would not allow me to since I was returning a reference. I tried float and it worked so I left it. Thanks again.
Remove your return statement from the function since you're using room as a reference. That will allow you to use void render.
Another alternative is to leave your code exactly how it was but change float render to int render, since room is an int, and assign room to render(x, room) like so room = render(x, room);
That would replace your regular call to render in main. The function returns the value of room, and assigns it to the variable room.
Thanks for the help. You have no idea how much I appreciate it. I had another problem with my code but with your last reply I was able to solve it. Thanks again.
I have revised the code but I had to add some features. I thought I had it solved until I had to add the new features and then it has me stumped again. When you in a certain room and try to move to another room that as you cannot move to it outputs a code but when I added that code I can no longer enter room 4 where the treasure is.
// Room must be 1-4
if (room >=1 && room <= 4)
{
// User wants to go either to the west or north
if( x == 4 || x == 8 )
{
// User is in room 2 or 3 and wants to go either west or north
if(room == 2 || room == 3)
{
// If the user is in room 2 and goes north
// How do they end back up in room 1?
room = 1;
cout << "---------" << endl;
cout << "| X | |" << endl;
cout << "---------" << endl;
cout << "| | T |" << endl;
cout << "---------" << endl;
cout << "You are in room " << room << "." << endl;
cout << endl;
}
I understand what your saying. I also have this code that everything works except that after it says "you cannot move in that direction" it needs to cout the last room they were in. Thats when I started getting messed up.
I would suggest creating a function that displays the room, it will help shorten up your code for render.
1 2 3 4
void DisplayRooms(int room, int treasure) {
// Display the room the user is in
// If the room and treasure are the same number, display that the user found the treasure
}
This saves from having to display each room multiple times.
Thank you so much for your help. After about a week an half of struggling with this you have finally helped me solve this problem and I submitted it and it passed all the tests. Thanks again. Here is the finally code that I used.