Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8561145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:26:12+00:00 2026-06-11T16:26:12+00:00

Here is my whole program, I am supposed to calculate the average number of

  • 0

Here is my whole program, I am supposed to calculate the average number of letters in words from an input file called hw4pr11input.txt. I have only been programming for a couple of weeks so I would appreciate simple answers that I could possibly implement with my small amount of knowledge. I do not know what arrays are yet, the chapter I am doing the homework for is on file io.

#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;

//function declaration 
void average_letters(ifstream& fin);
//Precondition: there is a input file with text ready to be read
//postcondition: Text from the input file is read then the average length of
//words is calculated and output to the screen

//start main program
int main()
{
ifstream fin;

         fin.open("hw4pr11input.txt");                                               //opening input file
         if (fin.fail())                                                            //checking for input file opening failure
         {
            cout << "Input file open fail";
            exit(1);                                                               //terminating program if check fails
         }
         cout << "File Open\n";
         average_letters(fin);                                                     //calling function to remove spaces
         system("pause");
         return 0;
}
                                                                                   //function definition, uses iostream and fstream
void average_letters(ifstream& fin)
{
char next, last_char = 0;
double letter_count = 0, word_count = 0;
double average = 0;
     while(!(fin.eof()))
     {
         fin.get(next);
         if(!(next == ' ' || next == ',' || next == '.' || next == '/'             
         || next =='(' || next == ')')) 
         {
                   letter_count++;                                                                    
         }

         else
         {   
             if((next == ' ' || next == ',' || next == '.' || next == '/'         
             || next =='(' || next == ')') && (last_char == ' ' || next == ','    
             || next == '.' || next == '/' || next =='(' || next == ')' ))

             {
                     continue;
             }
             else
             {
                     word_count++;
             }
         }
         last_char = next;                  //stores previous value of loop for comparison
     }
     average = letter_count/word_count;
     cout << "The average length of the words in the file is:" << " " <<average;
     cout << endl;
}

I believe this program works to accomplish the assignment, but my main concern is with the part of function average_letters that checks to see if it is a letter or a symbol. I chose this symbol list by looking at the .txt file. I deleted comments because they make copy and pasting here difficult I apologize if that makes my logic more difficult to understand.

Thanks for your help. Go easy on me :).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:26:13+00:00Added an answer on June 11, 2026 at 4:26 pm

    You can use a std::bitset<255> using the character converted to an unsigned integer and preset only those characters to true that are word characters. In your loop, you just lookup if its a valid word.

    Note, this presupposes a char being 255 bits instead of unicode. You can up-size your bit set accordingly.

    This allows you a very fast check of whether the character is a word character and allows you to define which characters you want to include (if the requirement changes suddenly to include ‘-‘, for example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The program is supposed to calculate the number of arguments, iterate over the list
Here i am moving the whole li elements from one list to another list.What
I'm working on a program for class that reads data from a file, the
So, this program is supposed to take stats from Auburn's football season and compute
Here's what my program is supposed to do: create 4 child processes: process 0
This program is supposed to run from command line like this: python Filename Folder_photos_are_in
Hello i am making a program that is supposed to copy a text file.
The program is supposed to receive an input through cin, tokenize it, and then
I've got some input files from my teacher wich we are supposed to test
Here's the whole page: <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Strict//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd> <html> <head>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.