Amanda Henry
Amanda Henry

Reputation: 11

My character counting code is miscounting characters. Why?

The input file has 4 items but the program seems to be counting more items. I was trying to create a function that would count the items that were capitals within the text file.

#include<iostream>
#include<fstream>
using namespace std;
bool ckeckCap(char ch){
    if((ch>='A')&&(ch<='Z')){
        return true;
        return false;
    }
}
int main(){
    ifstream inputFile;
    char ch;
    bool cap;
    int capa=0;
    int count=0;
    inputFile.open("input.txt");
    if(!inputFile.is_open()){
        cout<<"Error opening...Aborting"<<endl;
        return 0;
    }
    while(!inputFile.eof()){
        inputFile>>ch;
        cap=ckeckCap(ch);
        if(cap==true)
        capa=capa+1;
        //inputFile>>ch;
    }
    cout<<capa;
    return 0;
}

Upvotes: 1

Views: 80

Answers (2)

A M
A M

Reputation: 15277

Basically all problems have been mentioned already.

And, besides of eliminating the obvious bugs, I will show additionally a more modern C++ solution. By using modern language constructs, errors like the above can be avoided.

Please see:

#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>
#include <cctype>

int main() {

    // Open the input file and check, if it could be opened
    if (std::ifstream inputFileStream("r:\\input.txt"); inputFileStream) {

        // Iterate over the characters in the input file and output count of uppercase characters
        std::cout << std::count_if(std::istreambuf_iterator<char>(inputFileStream), {}, std::isupper);
    }
    else {
        // If the input file could not be opened, then inform user
        std::cerr << "\n*** Error: Could not open input file\n";
    }
    return 0;
}

Upvotes: 1

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 122133

You see inconsistent number of entries read from the file because of

while(!inputFile.eof())

This is wrong. See Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?

Further, this:

bool ckeckCap(char ch){
    if((ch>='A')&&(ch<='Z')){
        return true;
        return false;
    }
}

Should be

bool ckeckCap(char ch){
    if((ch>='A')&&(ch<='Z')){
        return true;
    }
    return false;
}

or rather

bool ckeckCap(char ch){
    return ((ch>='A')&&(ch<='Z'));
}

Note how this is shorter, easier to read and you cannot make the same mistake as above.

In your code, the return false; can never be reached. If the condition evaluates to false you do not return from a function declared to return a bool. This invokes undefined behavior.

Not the problem in your code, but this looks suspicious:

char ch;
bool cap;

Using variables before they are initialized is a common mistake (potentially also UB). It can be avoided by declaring variables only when you can initialize them with some meaningful value, or if this is not possible do initialize them with some value, eg:

char ch = ' ';
bool cap = false;    

Upvotes: 3

Related Questions