
class cave { public: cave(); void cavemap(); void set_numrows(); void make_move(); private: ifstream infile; char letterz; int numrows; // This is the number of rows int numcols; // This is the number of columns char **multArray; }; void cave::cavemap() { infile.open("cavemap.txt"); //Block of code to find number of columns //////////////////////////// string str; getline(infile, str); numcols=str.length(); infile.close(); //////////////////////////// infile.open("cavemap.txt"); while(!infile.eof()) //while-loop to count rows { string temp; getline(infile, temp); numrows++; } multArray = new char * [numrows]; // allocate the rows for (int row = 0; row < numrows; row++) //allocate columns multArray[row] = new char [numcols]; for (int r = 0; r < numrows; r++) //double for-loop to populate the 2D array { for (int c = 0; c < numcols; c++) { infile>>letterz; multArray[r][c] = letterz; cout << setw(2) << multArray[r][c]; } cout << endl; } for (int row2 = 0; row2 < numrows; row2++) delete[] multArray[row2]; // destroys memory of a single row delete[] multArray; // destroys the pointers to each row }
//Block of code to find number of columns //////////////////////////// string str; //numrows should have a value of zero here getline(infile, str); //you somehow need to give numrows the value of one here numcols=str.length(); infile.close(); //////////////////////////// //infile is already open here, don't reopen it. infile.open("cavemap.txt"); //don't use eof() as the terminating condition for the while loop. If you want to know the technical reason why, ask. Otherwise just accept the advice. Use while(getline(infile, str)) instead and don't call getline() within the loop body. while(!infile.eof()) //while-loop to count rows { //you really don't need another variable, just keep using str string temp; getline(infile, temp); numrows++; } /*This loop will stop when EOF is found or something else causes infile to go into a failed state. To be sure all of file was read you can check .eof(). If it is true, then you know you successfully read the whole file. However, you want to reread the file now char by char. To do that you need to get back to the beginning of the file. To do that I would call the istream clear() method on infile (because finding EOF put infile into a failed state) and then I would either use the istream seekg() method with the appropriate parameters or close infile and reopen it (when you reopen it it should default to the beginning of the file you associate it with). Now you can reread the file char by char to fill in multarray using the nested loop. */
#include <iostream> // for cout #include <iomanip> // for setw #include <fstream> #include <string> #include <cstring> using namespace std; class cave { public: void cavemap(); void set_numrows(); void make_move(); private: ifstream infile; char letterz; int numrows; // This is the number of rows int numcols; // This is the number of columns char **multArray; }; void cave::cavemap() { infile.open("cavemap.txt"); //Block of code to find number of columns //////////////////////////// string str; // getline(infile, str); // numcols=str.length(); // //////////////////////////// while(!infile.eof()) //while not end of file get each line and increase the number of rows by 1. { string temp; getline(infile, temp); numrows++; } multArray = new char * [numrows]; // allocate the rows for (int row = 0; row < numrows; row++) // now allocate the columns multArray[row] = new char [numcols]; for (int r = 0; r < numrows; r++) //double for-loop to populate the 2D array { for (int c = 0; c < numcols; c++) { // :) infile>>letterz; multArray[r][c] = letterz; cout << setw(2) << multArray[r][c]; } cout << endl; } cin>>letterz; for (int row2 = 0; row < numrows; row++) delete[] multArray[row2]; // destroys memory of a single row delete[] multArray; // destroys the pointers to each row } void cave::set_numrows() { numrows = 1; } void cave::make_move() { } int main () { cave caveobj; caveobj.cavemap(); return 0; }
#include <iostream> // for cout #include <iomanip> // for setw #include <fstream> #include <string> #include <cstring> using namespace std; class cave { public: cave(); void cavemap(); void set_numrows(); void make_move(); void set_letterz(); void map_output(); private: ifstream infile; char letterz; int numrows; // This is the number of rows int numcols; // This is the number of columns char **multArray; // Note: We use int ** because we want multArray to be a // a pointer to pointer to a int (an array of arrays...) int x, y; }; cave::cave() { x = 0; y = 0; } void cave::set_letterz() { letterz=' '; } void cave::cavemap() { infile.open("cavemap.txt"); if(infile.fail()) { cout<<"File opening fail"<<endl; } //Block of code to find number of columns /////////////////////////////////////////////////////////////// string str; // getline(infile, str); // numcols=str.length(); // // // while(getline(infile, str)) //while-loopo to count rows // { // numrows++; // } // infile.clear(); // //////////////////////////////////////////////////////////////// multArray = new char * [numrows]; // allocate the rows for (int row = 0; row < numrows; row++) // now allocate the columns multArray[row] = new char [numcols]; for (int r = 0; r < numrows; r++) //double for-loop to populate the 2D array { for (int c = 0; c < numcols; c++) { infile>>letterz; multArray[r][c] = letterz; cout << setw(2) << multArray[r][c]; } cout << endl; infile.close(); } for (int row2 = 0; row2 < numrows; row2++) delete[] multArray[row2]; // destroys memory of a single row delete[] multArray; // destroys the pointers to each row } void cave::set_numrows() { numrows = 1; } void cave::make_move() //sequence of if-else statements checks all 8 directions for "o" and then sets the previous //"y" = "o" and the next "o" equal to "y" { if(multArray[x+1][y+1]='o') //Check Northeast { multArray[x][y]='*'; multArray[x+1][y+1]='y'; } else if(multArray[x+1][y]='o')//Check East { multArray[x][y]='*'; multArray[x+1][y]='y'; } else if(multArray[x+1][y-1]='o') //check Southeast { multArray[x][y]='*'; multArray[x+1][y-1]='y'; } else if(multArray[x][y-1]='o') //Check South { multArray[x][y]='*'; multArray[x][y-1]='y'; } else if(multArray[x-1][y-1]='o')//check Southwest { multArray[x][y]='*'; multArray[x-1][y-1]='y'; } else if(multArray[x-1][y]='o') //Checks west { multArray[x][y]='*'; multArray[x+1][y]='y'; } else if(multArray[x-1][y+1]='o') //Checks NorthWest { multArray[x][y]='*'; multArray[x-1][y+1]='y'; } else if(multArray[x][y+1]='o') //Checks North { multArray[x][y]='*'; multArray[x+1][y]='y'; } } void cave::map_output() { for (int r = 0; r < numrows; r++) //double for-loop to populate the 2D array { for (int c = 0; c < numcols; c++) { cout << setw(2) << multArray[r][c]; } cout << endl; } } int main () { cave caveobj; caveobj.set_numrows(); caveobj.cave_map(); caveobj.make_move(); caveobj.map_output(); return 0; }
| DaniWeb Message | |
| Cancel Changes | |