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;
//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);
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.