Link to home
Start Free TrialLog in
Avatar of CharleneS77
CharleneS77Flag for United States of America

asked on

Stroring file text into array

I am attempting to write a program that inputs lines of text from a file.  The program then displays the total occurrences of each letter, including the apostrophe.  I am having trouble storing the letters into an array.

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::iostream;
using std::ostream;

#include <string>
using std::getline;

#include<iomanip>
using std::setw;

#include <cstring>
using std::string;

#include <fstream>
using std::fstream;
using std::ifstream;
using std::ofstream;

//Prototype
int readData( char *, char *, int [ ]);

//constant
const char apostrophe = '\'';

int main( )
{
                     // get the total number of EACH LETTER
      int letterTotals[ 28 ] = { 0 };
      int numberOfWords;
      char *wordArrayPtr = { 0 };
      numberOfWords = readData( myInFilePtr, wordArrayPtr, letterTotals );

                     return 0;

} // end of main

int readData( char *fileNamePtr, char *wWordArrayPtr, int letterTotals[ ] )
{
      int wordCount = 1;
      char currentLetter;
      ifstream wMyInFile( fileNamePtr );
      int letterIndex = 0;
      
      wWordArrayPtr = new char;
      //int counter = 0;

      while( !wMyInFile.eof() )
      {
            currentLetter = wMyInFile.peek();      // look at first letter
            //tolower( currentLetter );
            
            cout << " ----> Press Enter to add letter totals of next word to current totals.\n\n";
            getchar();      // used to stop the program before each word

            while( isalpha( currentLetter ) || ( currentLetter == apostrophe ) )
            {      
                  // increment the appropriate index of letterTotals array
                  if (currentLetter >= 'A' && currentLetter <= 'Z')
                  {
                        letterTotals[ int(currentLetter - 'A') + 1 ]++;
                  }
                  else if (currentLetter >='a' && currentLetter <= 'z')
                  {
                        letterTotals[ int(currentLetter - 'a') + 1 ]++;
                  }
                  else if (currentLetter == '\'')
                  {
                        letterTotals[0]++;
                  }
                  //letterTotals[ wordCount ] += wMyInFile.get();
                  //letterTotals[ counter ];
                  cout << "\n\n ---> current letter = " << currentLetter << "\n\n";                  
                  wWordArrayPtr[ wordCount ] += wMyInFile.get( );      // store letter in array
                  currentLetter = wMyInFile.peek();      // look at the next char      

                  /*
                  char letterValue = 96;
                  cout << "letter: " << letterValue << "     ";
                  int index = 0;
                  cout << "letterTotals[ " << index << " ] = " << letterTotals[ index ] << "\n";
                  letterValue = 65;
                  
                  for( index = 1; index < 27; index++ )
                  {
                        cout << "letter: " << letterValue << "     ";
                        cout << "letterTotals[ " << index << " ] = " << letterTotals[ index ] << "\n";
                        letterValue++;
                  }
                  */      
            }
            
            //counter++;

            //wWordArray[ wordCount ] = '\0';
            //letterIndex = 0;
            cout << "\n wordCount before letterTotals = " << wordCount << "\n\n";

            char letterValue = 96;
            cout << "letter: " << letterValue << "     ";
            int index = 0;
            cout << "letterTotals[ " << index << " ] = " << letterTotals[ index ] << "\n";
            letterValue = 65;
                  
            for( index = 1; index < 27; index++ )
            {
                  cout << "letter: " << letterValue << "     ";
                  cout << "letterTotals[ " << index << " ] = " << letterTotals[ index ] << "\n";
                  letterValue++;
            }

            cout << "\n wordCount after letterTotals = " << wordCount << "\n\n";
            
            // !!! -----> the following while is an infinite loop without the break
            while( !isalpha( currentLetter ) && ( currentLetter != apostrophe ) ) // make this an if
            {
                  cout << "\n Entered while...\n";
                  wMyInFile.ignore( 1 );
                  currentLetter = wMyInFile.peek();      // look at the next char
                  break;      // without this break, this while is an infinite loop
            }

            cout << "\n Exited while...\n\n";
            //currentLetter = wMyInFile.peek();      // look at the next char
            wordCount++;
            wWordArrayPtr = new char;
      }
      //cout << "\n Is it reaching this point?\n";
      return wordCount;

} // end of readData
SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> I would suggest to go the C++ way, and use :
>> 
>>       std::string *wWordArrayPtr

Or use a vector like this :

      std::vector<std::string> wWordArrayPtr;

Which is even better !
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CharleneS77

ASKER


>>>else if ((unsigned char)c <= ' ')
                 {
                       if (i > lpos + 1)
                             wordCount++;
                      lpos = i;  
                 }

Can you explain what this is doing?  What is unsigned char used for?
unsigned char is used in this case to make sure that the values above 127 are not negative. We only want to have this else statement treat the values 0x00 - 0x20. (0x20 is the ASCII value for the space). The characters before the space (0x00 - 0x1F) are all control characters (like a bell, a backspace, a newline, etc.). Take a look at an ASCII table to learn more :

        http://www.asciitable.com/

Now, what is that piece of code doing ?

                 else if ((unsigned char)c <= ' ')       // we only want the control characters and the space
                 {
                       if (i > lpos + 1)                         // lpos is the location of the last such character we found. This checks if we found the next word border (ie. we filter out two consecutive spaces with this check)
                             wordCount++;                   // if so, the word count is included
                      lpos = i;                                    //  we reset lpos to the current location
                 }
Infinity, thank you for explanation. It's better than I've had could do  ;-)  (I am on a Kenia trip right now and had no time to answer).

>>>else if ((unsigned char)c <= ' ')

You also could do

   else if (isspace(c))

what checks for whitespace chars like space or tab or linefeed (0x09 - 0xD and 0x20) but not for other controls like 0x00. So personally I prefer  the cast to unsigned char and catch all controls by taking all that are less equal to space (0x20, 32, or ' ').

Regards, Alex
>> I am on a Kenia trip right now
Oooh, nice !! And you still take the time to visit EE, while you should be enjoying yourself with the lions :)