0

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;
}
2
Contributors
2
Replies
3
Views
11 Years
Discussion Span
Last Post by Jon182
0

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/

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.