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
CharleneS77Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
I didn't check the whole code, so there might be other problems, but I assume your problem originates from this line :

      wWordArrayPtr[ wordCount ] += wMyInFile.get( );      // store letter in array

wWordArrayPtr is an array of char.
wWordArrayPtr[ wordCount ] is the char at position wordCount in the array.
What you do, is add the ASCII value of a character to the ASCII value of another character, forming a new character - that's not what you want to do.

You need an array of strings !

I would suggest to go the C++ way, and use :

      std::string *wWordArrayPtr

And then you can use :

      wWordArrayPtr[ wordCount ] += wMyInFile.get( );      // store letter in array


If there are other problems with the code, please let me know, and I'll take a closer look at the code.
0
Infinity08Commented:
>> 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 !
0
itsmeandnobodyelseCommented:
>>>> wWordArrayPtr[ wordCount ] += wMyInFile.get( );      // store letter in array
That is wrong. You can't use char arrays like string (class) objects.

Why do you want to store the words at all? I don't see that you are using words aor word array?

The whole thing can be made much easier e. g. like that:

    int lpos = 0;
    char line[1024] = { '\0' };
    while (wMyInFile.getline(line, sizeof(line)))
    {
          int n = strlen(line);
          for (int i = 0; i < n; ++i)
          {
                 char c = toupper(line[i]);
                 if (c >= 'A' && c <= 'Z')
                       letterTotals[c - 'A' + 1]++;
                 else if (c == apostrophe)
                        letterTotals[0]++;
                 else if ((unsigned char)c <= ' ')
                 {
                       if (i > lpos + 1)
                             wordCount++;
                      lpos = i;  
                 }
          }
    }            

If using std::string for line buffer (recommedned) it is    

    std::string line;
    while (std::getline(wMyInFile, line))
    {
          int n = line.length();
          ....

Using std::string you easily could add the letters to words. With std::vector you easily could add full words to a dynamic array:        

                 std::string word;
                 std::vector<std::string> wordArr;
                 ...
                             
                       ...
                  if (c >= 'A' && c <= 'Z')
                       letterTotals[c - 'A' + 1]++;
                 else if (c == apostrophe)
                        letterTotals[0]++;
                else if ((unsigned char)c <= ' ')
                 {
                       if (i > lpos + 1)
                      {
                             // wordCount++;   // don't need if using vector
                             wordArr.push_back(word);
                      }
                      lpos = i;  
                      continue;   // next loop cycle if space
                 }
                 word += line[i];   // add original char to word
                   

Regards, Alex
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

CharleneS77Author Commented:

>>>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?
0
Infinity08Commented:
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
                 }
0
itsmeandnobodyelseCommented:
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
0
Infinity08Commented:
>> 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 :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.