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;
}

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.