Hey guys I am just wondering why the following code gives the wrong number of characters from a file when the character count is the default? Thanks.

void main()
{
    int blank_count = 0;
    int char_count = 0;
    int sentence_count = 0;
    char ch;

    ifstream iFile("c:\test.txt");

    if (! iFile)
    {
        cout << "Error opening input file" << endl;
       
    }

    while (iFile.get(ch))
    {
        switch (ch) {
            case ' ':
                blank_count++;
                break;
            case '.':
                sentence_count++;
                break;
            default:
                char_count++;
                break;
        }
    }

    cout << "There are " << blank_count << " blanks" << endl;
    cout << "There are " << char_count << " characters" << endl;
    cout << "There are " << sentence_count << " sentences" << endl;
}

Dani AI

Generated

Two quick, concrete points explain why the counts stay at their default (zero) and how to fix them.

First, the filename literal in 's code uses a single backslash: "c:\test.txt". In C++ that invokes escape sequences (for example "\t" is a tab), so the stream fails to open. The program prints the error but does not exit, so the input loop never runs and every counter stays at its initialized value. See C++ string literal escape rules for details (escape sequences). The practical fixes are: escape the backslash ("c:\\test.txt"), use a forward slash ("c:/test.txt"), or use a raw string literal (R"(c:\test.txt)"); also return or exit after a failed open.

Second, decide what "characters" means (as hinted). The posted switch counts spaces and periods separately, so char_count only gets non-space, non-period bytes. If the intent is to count every byte read, increment a total counter on every successful get. If the intent is to count printable characters, use std::isprint/std::isspace (cast to unsigned char before calling). Also consider treating '.', '!' and '?' as sentence terminators if required.

Minimal corrected pattern (illustrative):

std::ifstream in("c:\\test.txt");
if (!in.is_open()) { std::cerr << "Error opening file\n"; return 1; }

std::size_t total = 0, blanks = 0, sentences = 0;
char ch;
while (in.get(ch)) {
    ++total;
    if (ch == ' ') ++blanks;
    if (ch == '.' || ch == '!' || ch == '?') ++sentences;
}

Note: byte counts differ from character counts for multi-byte encodings (UTF-8); decode first if a Unicode-aware character count is needed.

Recommended Answers

All 2 Replies

why do you think it is incorrect? Is the char count supposed to include spaces and periods? How about end-of-line terminating characters?

Why not consider question mark ? and exclaimation point! as end-of-sentence terminators/

Thanks for your reply Ancient Dragon, if you get a moment can you please see my other post. Thanks again

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.