Map Game never ending loop

im in a beginners course for c++ and the assignment is to make a map game where it gets the info from an outfile. I know ive made a mistake somewhere because its a never ending loop after entering the first room. If you could tell me where i messed up i would greatly appreciate it. Thank you!




#include <iostream>
#include <string>
#include <fstream>
using namespace std;

void getNumRooms(int numRooms){
ifstream inFile;
inFile.open("gamefile.txt");

inFile>>numRooms;
cout<<"There are "<<numRooms<<" rooms."<<endl;
inFile.close();

}
int startRoom(int currentRoom){
int numRooms;
ifstream inFile;
inFile.open("gamefile.txt");

inFile>>numRooms;
cout<<"What room would you like to start in? (1-15)"<<endl;
cin>>currentRoom;
if(currentRoom < 1 || currentRoom > numRooms){
cout<<"Invalid room number. Beginning at room 1."<<endl;
currentRoom = 1;
}
return currentRoom;
}
void fileProcess(){
int temp;
int skipVal;
int currentRoom;
ifstream inFile;
int counter;
int d, u, l, r;
char dSelect;
inFile.open("gamefile.txt");
while(dSelect != 'z'){
counter = 0;
inFile>>temp;
skipVal = (currentRoom-1)*4;


while(counter < skipVal){
inFile>>temp;
counter++;
}

//Read in four values from that line
inFile >> u;
inFile >> d;
inFile >> l;
inFile >> r;

//Print out options
cout<<"From here you may choose from the following options:"<<endl;
cout<<"\tUp: "<<u<<endl;
cout<<"\tDown: "<<d<<endl;
cout<<"\tLeft: "<<l<<endl;
cout<<"\tRight: "<<r<<endl;
}


}
void userInput(char dSelect){
cout<<"Make your selection by typing u, d, l, or r. (type z to quit) "<<endl;
//Get direction selection from user
cin>>dSelect;
}
void updateState(){
int d, u, l, r;
ofstream outFile;
outFile.open("breadcrumb.txt");
int currentRoom;
char dSelect;

//Update breadcrumb trail in output file
switch(dSelect){
case 'u':
outFile << "Up to room: " << u << endl;
break;
case 'd':
outFile << "Down to room: " << d << endl;
break;
case 'l':
outFile << "Left to room: " << l << endl;
break;
case 'r':
outFile << "Right to room: " << r << endl;
break;
}

//8. Update current room (aka move player)
switch(dSelect){
case 'u':
currentRoom = u;
break;
case 'd':
currentRoom = d;
break;
case 'l':
currentRoom = l;
break;
case 'r':
currentRoom = r;
break;
}


}

int main(){
int numRooms;
int currentRoom;
int counter;
int skipVal;
int temp;
int u, d, l, r;
char dSelect;
//set up file access
ifstream inFile;
inFile.open("gamefile.txt");

ofstream outFile;
outFile.open("breadcrumb.txt");

getNumRooms(numRooms);

startRoom(currentRoom);

fileProcess();

userInput(dSelect);

updateState();



//the following two lines get us back to start at the top of the input file
inFile.clear();
inFile.seekg(0, std::ios::beg);


return 0;
}
In routine
void fileProcess()
you set up a while loop:
while(dSelect != 'z'){

However, no value of dSelect is available in this function ... so it certainly isn't 'z' ... so you will loop ... for ever!

As a guide to debugging, you may not immediately see your error, but it is clear which loop you were going through, so this must be your first point to look for bugs.
Topic archived. No new replies allowed.