ACTUAL TITLE: CAN'T ASSIGN IN VALUE TO ARRAY OF INTS....sorry, I was issuing weapons while writing this and got distracted....
Ok...this is really irritating me...I've been putting a DOS based hangman game together and I'm at the point where I'm randomly generating a word from a text file. The code for my function is posted below. I've commented it so it's a litte easier and less frustrating to read.
Code:void Player::GetWord() { string line, line2; int NumLines(0); int length, PointPos, RandNum; ifstream File; char * buffer; File.open("example.txt"); // initialize and set our file as input file "example.txt" if(File.is_open()) { File.seekg(0, ios::end); // length = File.tellg(); // Determines the length of the file File.seekg(0, ios::beg); // buffer = new char [length]; // Creates a file buffer File.read(buffer, length); // Reads the entire file into the file buffer for(int x(0); x < length; x++) { /********************************************************************/ if(buffer[x] == '\n') /* Determines how many lines there are in the text file */ NumLines++; /********************************************************************/ } cout << NumLines << endl; int * LineLocs = new int [NumLines]; // Array that will keep track of the newline positions '\n' int LLPos(0); // Refers to the position of the Line Location array (LineLocs) for(int y(0); y < length; y++) { /*******************************************************************/ if(buffer[y] == '\n') /* When the for loop encounters a /n char, it stores the */ { /* value of 'y', which will later be used to place the pointer */ LineLocs[LLPos] = (y+1); /* in the text file. */ if(LLPos < NumLines-1) LLPos++; cout << LineLocs[LLPos] << endl; } } do /********************************************************************/ { /* Chooses a randum number between 0 and the number of lines */ RandNum = rand()%NumLines; /* Since our array goes from 0 to NumLines-1, if RandNum is */ }while(RandNum == NumLines); /* equal to NumLines, a new number will be selected. */ /********************************************************************/ if(RandNum == 0) // Sets the pointer position to the beginning of the file PointPos = 0; else PointPos = (LineLocs[RandNum]); // Sets the pointer to the value of the newline stored in the arry File.seekg(PointPos, ios::beg); // Places the pointer getline(File, line, '|'); word = line; // Reads the first string and sets player[x].word equal to it getline(File, line2, '\n'); subject = line2; // Same as above with player[x].subject File.seekg(0); // Resets the pointer } else cout << "Error opening word bank"; File.close(); }
Everything seems to work fine except for one particular for() loop which will not assign int y to a position in an int array. It's below.
Here's my problem. When I try to assign the value of y plus one to the array, it stores some off the wall number like 13239802. I've been trying to debug this one problem for about 5 hours total and have checked and rechecked to make sure that the public variables in the class are the same type. I've printed buffer[y], y, NumLines, LLPos on the screen and they all display as valid input. I don't understand why it will not let me assign y's value to LineLocs[LLPos].Code:for(int y(0); y < length; y++) { /*******************************************************************/ if(buffer[y] == '\n') /* When the for loop encounters a /n char, it stores the */ { /* value of 'y', which will later be used to place the pointer */ LineLocs[LLPos] = (y+1); /* in the text file. */ if(LLPos < NumLines-1) LLPos++; cout << LineLocs[LLPos] << endl; } }
Edit: Some of the spacing and comments got jacked up in the transfer....it WAS a lot neater![]()



LinkBack URL
About LinkBacks



