I am trying to read file input into an array so I can tally up the number of times each letter appears (including the apostrophe). I am not sure what I am doing wrong, but my file data is not getting assigned to the array outside the function.

#include<iostream>
using std::cin;
using std::cout;
using std::endl;
//using::left;
//using::right;
using std::istream;
using std::ostream;
#include<string>
using std::getline;
#include<iomanip>
using std::setw;
//#include <cstdlib>
//#include <ctime>
#include<cstring>// prototype for strtok, strlen, strcmp, strncmp, strcpy, strncpy, strcat, strncat
using std::string;
#include<cctype>
#include<fstream>
using std::fstream;
using std::ifstream;
using std::ofstream;
// prototypes
//int readData( char *, char * );
int readData( char *, char *, int [ ]);
void tokenizeData( char *, char * );
constchar apostrophe = '\'';
char *tokenPtr;
/*
// initialize alaphabet array
const char *alphabet[ 27 ] = { '\'', 'A', 'B', 'C', 'D', 
'E', 'F', 'G', 'H', 'I', 
'J','K', 'L', 'M', 'N', 
'O','P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z', '\0' };
*/
//------------------------------------------------------------------------------
int main()
{
/*
// initialize alaphabet array
char alphabet[ 28 ] = { '\'', 'A', 'B', 'C', 'D', 
'E', 'F', 'G', 'H', 'I', 
'J','K', 'L', 'M', 'N', 
'O','P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z', '\0' };
*/
int letterTotals[ 28 ] = { 0 };
 
int numberOfWords;
//int numberOfTokenizedWords;
//ifstream myInFile( "data.txt" );
char *myInFile = ( "data.txt" );
char *wordArray = { 0 };
//char *tokenizedWordArray = { 0 };
//numberOfWords = readData( myInFile, wordArray );
numberOfWords = readData( myInFile, wordArray, letterTotals );
//cout << "\nnumberOfWords = " << numberOfWords;
for( int index = 0; index < numberOfWords; index++ )
{ 
cout << "----------------------------" << wordArray[ index ] << "\n";
//cout << wordArray[ index ] << "\n";
}
 
/*
numberOfTokenizedWords = tokenizeData( myInFile, tokenizedWordArray );
//cout << "\nnumberOfTokenizedWords = " << numberOfTokenizedWords;
for( int index = 0; index < numberOfTokenizedWords; index++ )
{ 
cout << "----------------------------" << tokenizedWordArray[ index ] << "\n";
//cout << tokenizedWordArray[ index ] << "\n";
}
*/
return 0;
}
//------------------------------------------------------------------------------
//int readData( char *dataFileName, char *wWordArray /*int letterTotals[ ] */)
int readData( char *dataFileName, char *wWordArray, int letterTotals[ ] )
{
int wordCount = 1;
char currentLetter;
ifstream wMyInFile( dataFileName );
int letterIndex = 0;
 
/*
if( wMyInFile.eof() ) // if no words exist in file
{
return 0;
}
*/
 
wWordArray = new char;
int counter = 0;
while( !wMyInFile.eof() )
{
currentLetter = wMyInFile.peek(); // look at first letter
 
tolower( currentLetter );
while( isalpha( currentLetter ) || ( currentLetter == apostrophe ) )
{ 
//letterTotals[ wordCount ] += wMyInFile.get();
//letterTotals[ counter ];
cout << "\n\ncurrent letter = " << currentLetter << "\n\n"; 
wWordArray[ wordCount ] += wMyInFile.get( ); // store letter in array
currentLetter = wMyInFile.peek(); // look at the next char 
switch( currentLetter )
{
case '\'': letterTotals[ 0 ]++;
break;
case 'a':
case 'A': letterTotals[ 1 ]++;
break;
case 'b': 
case 'B': letterTotals[ 2 ]++;
break;
case 'c': 
case 'C': letterTotals[ 3 ]++;
break;
case 'd': 
case 'D': letterTotals[ 4 ]++;
break;
case 'e': 
case 'E': letterTotals[ 5 ]++;
break;
case 'f': 
case 'F': letterTotals[ 6 ]++;
break;
case 'g': 
case 'G': letterTotals[ 7 ]++;
break;
case 'h': 
case 'H': letterTotals[ 8 ]++;
break;
case 'i': 
case 'I': letterTotals[ 9 ]++;
break;
case 'j': 
case 'J': letterTotals[ 10 ]++;
break;
 
case 'k': 
case 'K': letterTotals[ 11 ]++;
break;
case 'l': 
case 'L': letterTotals[ 12 ]++;
break;
case 'm': 
case 'M': letterTotals[ 13 ]++;
break;
case 'n': 
case 'N': letterTotals[ 14 ]++;
break;
case 'o': 
case 'O': letterTotals[ 15 ]++;
break;
case 'p': 
case 'P': letterTotals[ 16 ]++;
break;
case 'q': 
case 'Q': letterTotals[ 17 ]++;
break;
case 'r': 
case 'R': letterTotals[ 18 ]++;
break;
case 's': 
case 'S': letterTotals[ 19 ]++;
break;
case 't': 
case 'T': letterTotals[ 20 ]++;
break;
case 'u': 
case 'U': letterTotals[ 21 ]++;
break;
case 'v': 
case 'V': letterTotals[ 22 ]++;
break;
case 'w': 
case 'W': letterTotals[ 23 ]++;
break;
case 'x': 
case 'X': letterTotals[ 24 ]++;
break;
case 'y': 
case 'Y': letterTotals[ 25 ]++;
break;
case 'z': 
case 'Z': letterTotals[ 26 ]++;
break;
default: break;
}
char letterValue = 44;
cout << "letter: " << letterValue << "\n\n";
int index = 0;
cout << "letterTotals[ " << index << " ] = " << letterTotals[ index ] << "\n";
letterValue = 65;
for( index = 1; index < 27; index++ )
{
cout << "letter: " << letterValue << "\n";
cout << "letterTotals[ " << index << " ] = " << letterTotals[ index ] << "\n\n";
letterValue++;
}
}
 
counter++;
wWordArray[ wordCount ] = '\0';
//letterIndex = 0;
cout << "\n----- wordCount = " << wordCount << " -----\n\n";
 
while( !isalpha( currentLetter ) && ( currentLetter != apostrophe ) )
{
wMyInFile.ignore( 1 );
currentLetter = wMyInFile.peek(); // look at the next char
}
 
wordCount++;
wWordArray = new char;
}
return wordCount;
}
//------------------------------------------------------------------------------
void tokenizeData( char *dataFileName, char *wTokenizedWordArray )
{
int tokenizedWordCount = 1;
cout << "\nThe string to be tokenized is:\n " << wTokenizedWordArray
<< "\n\nThe tokens are: \n\n";
// begin tokenization of sentence
tokenPtr = strtok( wTokenizedWordArray, " " );
// continue tokenizing sentence until tokenPtr becomes NULL
while( tokenPtr != NULL )
{
cout << tokenPtr << '\n';
tokenPtr = strtok( NULL, " " ); // get next token
tokenizedWordCount++;
}
cout << "\nAfter strtok, sentence = " << wTokenizedWordArray << endl;
//cout << "\nnumberOfTokenizedWords = " << numberOfTokenizedWords;
for( int index = 0; index < tokenizedWordCount; index++ )
{ 
cout << "*************" << wTokenizedWordArray[ index ] << "\n";
//cout << tokenizedWordArray[ index ] << "\n";
}
//return tokenizedWordCount;
}

Recommended Answers

All 2 Replies

Sheesh! Why do you make it so complicated?

Take a look at the ASCII table. Each letter has in fact a numeric value. So use this to your advantage. Create an int array of 128 elements, as there are 128 characters in ASCII. Now look how easy it is:

lookupTable['a']++;

That simply increments the value of 'a', which happens to be the value 97. So if you want to make it simple, just loop through the string, and plug each letter into the array and increment that element. Then all you have to do is print out the elements in the array, and you're done.

Also see this for why eof is bad.

it is much easier if u use the c++ std library.

#include <iostream>
#include <fstream>
#include <iterator>
#include <map>
#include <algorithm>
#include <cctype>
using namespace std;

struct print_it
{
   inline void operator() ( const pair<char,int>& p ) const
   { cout << p.first << " occurs " << p.second << " times\n" ; }
};

int main()
{
   const char* const file_name = "data.txt" ;
   ifstream file(file_name) ;
   if( !file ) return 1 ;
   const char apostrophe = '\'' ;
   map<char,int> char_cnt ;
   char c ;
   while( (file>>c) && ( isalpha(c) || (c==apostrophe) ) ) ++char_cnt[c] ;
   for_each( char_cnt.begin(), char_cnt.end(), print_it() ) ;
   return 0 ;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.