Solved

Stroring file text into array

Posted on 2007-04-02
7
327 Views
Last Modified: 2013-12-14
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
0
Comment
Question by:CharleneS77
  • 4
  • 2
7 Comments
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 50 total points
ID: 18841656
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 18841660
>> 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
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 75 total points
ID: 18841684
>>>> 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:CharleneS77
ID: 18854632

>>>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
 
LVL 53

Expert Comment

by:Infinity08
ID: 18855995
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 18858056
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 18858107
>> 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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now