Hey people! Can you please help me with this problem? I am so stuck on it and my instructor gave me hints on what I should do. No arrays or anything crazy yet, I'm still a beginer.

1.) Write a program that will compute average word length (average number of characters per word) for a file that contains some text. A word is defined to be any string of symbols that is preceded and followed by one of the following at each end: a blank, a comma, a period, the beginning of a line, or the end of a line. Your program should define a function that is called the input-file stream as an argument.

2.) Write a program that prompts the user to input the name of a text file and then outputs the number of words in the file. You can consider a "word" to be any text that is surrounded by whitespace (for example, a space, carriage return, newline) or borders the beginning or end of the file.

This is my code so far, I know its not much but I really need help with this problem:

And apparently I can try to modify the same code for Problem 1 and Problem 2. Thank you for trying to help me out.

#include "iostream"//include statement(s)
#include "iomanip"
#include "fstream"

using namespace std;//using namespace statement(s)

int main()
{
        char filename[17];
        cin >> filename;

        ifstream in_stream;
        ofstream out_stream;

        in_stream.open(filename);
        out_stream.open("results.txt");

        char myChar;
        bool isWhiteSpace (char myChar);
        {
                switch (myChar)
                {
                case '\n': 
                case '\t': 
                case ' ' : 
                case '\r': 
                case ',':
                case '.':
                case '?':
                case '!':
                        return true;
                        break;

                default:
                        return false;
                }

        in_stream.close();
        out_stream.close();


        system ("PAUSE");
        return 0;//return statement(s)
}

Recommended Answers

All 6 Replies

Why not use C++ string since you are using C++ ...
and something like below ...

ifstream fin( fnameCppStr.c_str() );
if( fin )
{
    int numWords = 0, numChars = 0;
    string word;
    // input each word until end of file ... //
    while( fin >> word )
    {
        ++numWords;
        int len = word.size();
        if( ".?!,;".find( word[len-1] ) != string::npos )
            word.erase( len-1 );
        numChars += word.size();
    }
    fin.close();
    // report counts, etc...

}
else
{
    // print some error message
}

And NOT ...

#include "iostream"
#include "fstream"

Instead use ...

#include <iostream>
#include <fstream>

What's the difference between using:

#include <iostream>
#include "iostream"

//aren't they the same meaning???

Also, I tried modifying my code for Problem 1 and this is what I came up with but I have so many errors and I'm not sure how to fix them.

#include <string> 
#include <fstream> 
#include <iostream> 
using namespace std; 
void repeat_line(ifstream& in_stream);
int main() 
{ 
string word; 
string letters;
ifstream fin;
ifstream in_stream; 
int CountWord = 0;
int CountLetters = 0;
char next;
fin.open("Results.txt"); 
while(fin >> word) 
    { 
        CountWord++; 
    } 
fin.close();
fin.open("Results.txt");
in_stream.get(next);
while(! in_stream.eof( ))
{
    cout << next;
    in_stream.get(next);
    repeat_line(in_stream, next)
}
if(!fin.eof( ))
   cout << "Not Done yet.";
else
    cout << "End of the file.";
cout <<"Total Words: "<< CountWord << endl;
cout <<"Total Letters: "<< CountLetters << endl;
cout <<"Average Characters per word: "<< (CountLetters/CountWord) <<endl;
      system("PAUSE");
      return 0;
} 
void repeat_line(ifstream& in_stream)
{
   char symbol;
   do
   {
      if(in_stream.eof())
        return;
      in_stream.get(symbol);
   } while(symbol!='\n');
}

This is what my Results.txt looks:

Hello! Computer Science rocks! :) WOOHOOO!

If you took what I gave you above as a start ... you might have coded something like the following:

// countCharsWords.cpp //

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std; 

// can easily change FNAME here ... //
const string FNAME = "Results.txt";
/*
Hello! Computer Science rocks! :) WOOHOOO!
*/

// can easily adjust char's to be included as PUNC ... here //
const string PUNC = ".?!,;";



int main() 
{ 
    ifstream fin( FNAME.c_str() );
    if( fin )
    {
        int numWords = 0, numChars = 0;
        string word;

        cout << fixed << setprecision(0); // show NO decimal

        // input each word until end of file ... //
        while( fin >> word )
        {
            ++numWords;
            int len = word.size();
            // if last char is in string PUNC ... //
            if( PUNC.find( word[len-1] ) != string::npos )
                word.erase( len-1 );
            numChars += word.size();
            // show words ...//
            cout << ' ' << left << setw( 15 ) << word;
        }
        fin.close();

        cout << "\n\nNumber of words was " << numWords
             << "\nNumber of letters was " << numChars
             << "\nAverage letters per word was "
             << double(numChars) / numWords << endl;
    }
    else
        cout << "\nThere was a problem opening file "
             << FNAME << endl;

    cout << "\nPress 'Enter' to continue/exit ... ";
    cin.get();
}

Hey David, how do I make your coding into a function though? "Your program should define a function that is called the input-file stream as an argument."

One 'quick way' that comes to mind ...

// countCharsWords2.cpp //

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std; 

// can easily change FNAME here ... //
const string FNAME = "Results.txt";
/*
Hello! Computer Science rocks! :) WOOHOOO!
*/

// can easily adjust char's to be included as PUNC ... here //
const string PUNC = ".?!,;";

void processFile( ifstream& fin )
{
    int numWords = 0, numChars = 0;
    string word;

    cout << fixed << setprecision(0); // show NO decimal

    // input each word until end of file ... //
    while( fin >> word )
    {
        ++numWords;
        int len = word.size();
        if( PUNC.find( word[len-1] ) != string::npos )
            word.erase( len-1 );
        numChars += word.size();
        // show words ...//
        cout << ' ' << left << setw( 15 ) << word;
    }

    cout << "\n\nNumber of words was " << numWords
         << "\nNumber of letters was " << numChars
         << "\nAverage letters per word was "
         << double(numChars) / numWords << endl;
}


int main() 
{ 
    ifstream fin( FNAME.c_str() );
    if( fin )
    {
        processFile( fin );
        fin.close();
    }
    else
        cout << "\nThere was a problem opening file "
             << FNAME << endl;

    cout << "\nPress 'Enter' to continue/exit ... ";
    cin.get();
}

Thank you so much! You are awesome! :)

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.