Can't get cout to work in for() loop

This is a discussion on Can't get cout to work in for() loop within the C++ Programming forums, part of the General Programming Boards category; ACTUAL TITLE: CAN'T ASSIGN IN VALUE TO ARRAY OF INTS....sorry, I was issuing weapons while writing this and got distracted.... ...

  1. #1
    Registered User motarded's Avatar
    Join Date
    Feb 2006
    Location
    Djibouti, Africa
    Posts
    14

    Can't assign int value to array of ints

    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.

    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;     
                             
                   }         
              }
    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].


    Edit: Some of the spacing and comments got jacked up in the transfer....it WAS a lot neater
    Last edited by motarded; 03-03-2006 at 08:35 AM.

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Code:
    LLPos++;
    cout << LineLocs[LLPos] << endl;
    you are incrementing the index then trying to output the value at the new index which probably is undefined.
    Last edited by Darryl; 03-03-2006 at 09:02 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-17-2008, 12:36 PM
  2. How properly inherit from template?
    By 6tr6tr in forum C++ Programming
    Replies: 118
    Last Post: 04-25-2008, 05:30 AM
  3. How Does cout Work
    By jrahhali in forum C++ Programming
    Replies: 8
    Last Post: 08-25-2004, 04:19 PM
  4. Why doesnt this work
    By Jotun in forum C++ Programming
    Replies: 3
    Last Post: 04-18-2004, 05:55 PM
  5. Redirecting cout stream
    By Arrow Mk84 in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2002, 05:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21