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 8410291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:08:13+00:00 2026-06-10T00:08:13+00:00

I wrote a program for counting the number of alphanumeric characters in a text

  • 0

I wrote a program for counting the number of alphanumeric characters in a text file. However, the number it returns is always larger than the number that online character counters return.

For example, the program will calculate the number of alphanumeric characters in this text:

if these people had strange fads and expected obedience on the most
extraordinary matters they were at least ready to pay for their
eccentricity

to be 162. Running the program again, it’ll say there are 164 characters in the text. Running it again, it’ll say there are 156 characters. Using this online character counter, it seems that the character count ought to be lower than 144 (the online character counter includes spaces as well).

Here is the code:

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

int main() {
    char line[100];
    int charcount = 0;
    ifstream file("pg1661sample.txt");
    while (!file.eof()) {
        file.getline(line, 99);
        for (int i = 0; i < 100; i++) {
            if (isalnum(line[i])) {
                charcount++;
            }
        }
    }

    cout << endl << "Alphanumeric character count: " << charcount;
    cin.get();
    return 0;
}

What am I doing wrong?

  • 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-10T00:08:14+00:00Added an answer on June 10, 2026 at 12:08 am

    Try:

    #include <iterator>
    #include <algorithm>
    #include <iostream>
    #include <cctype>
    bool isAlphaNum(unsigned char x){return std::isalnum(x);}
    int main()
    {
        std::cout << "Alphanumeric character count: " <<
        std::count_if(std::istream_iterator<char>(std::cin),
                      std::istream_iterator<char>(),
                      isAlphaNum
                     ) ;
    }
    

    Problems with your code:

    EOF is not true until you read past the end of file:

     // this is true even if there is nothing left to read.
     // If fails the first time you read after there is nothing left.
     while (!file.eof()) {
    
     // thus this line may fail
         file.getline(line, 99);
    

    It is better to always do this:

     while(file.getline(line, 99))
    

    The loop is only entered if the getline actually worked.

    You are also using a bad version of getline (as lines may be larger than 100 characters).
    Try and use the version that works with std::string so it auto expands.

    std::string  line;
    while(std::getline(file, line))
    {
         // stuff
    }
    

    Next you assume the line is exactly 100 characters.
    What happedn if the line is only 2 characters long?

    for (int i = 0; i < 100; i++)
    

    Basically you will scan over the data and it will count letters that were from left over from a previous line (if a previous line was longer than the current) or completely random garbage. If you are still useing file.getline() then you can retrieve the number of characters from a line using file.gcount(). If you use the std::getline() then the variable line will be the exact size of the line read (line.size()).

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

Sidebar

Related Questions

I wrote a program that rolls a die with a user-specified number of sides.
I wrote a program called Hello.py that looks like this: import pygame, sys from
I wrote this program: #include <stdio.h> /*Part B Write a program that: defines an
I wrote a program that forks some processes with fork(). I want to kill
I wrote a program that simulates soft bodies using springs. It looks nice but
I wrote a program that uses OLE and it was working fine until I
I wrote a program that worked perfectly until the market required me to add
I wrote a program using AutoIT to fetch information from a number of websites
I wrote a program out, which was all in one file, and the methods
I wrote a program in Java that uses a special font that by default

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.