Question

#include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here2.11 LAB 2.2 - File I/O Characters Implement the function charCnt. charCnt is passed in a name of a file and a single charact

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

//implementation of charCnt

int charCnt(string filename,char ch){

ifstream inputfile(filename);

char inChars;

int count=0;

if(!inputfile.is_open()){

cout<<"Error opening "<<filename<<endl;

exit(1);

}else{

//reading from the input file character by character

//There is no need to use getc function you can use the >> parameters to read from a ifstream that is very easy

while(inputfile>>inChars){

if (inChars==ch){

count++;

}

}

}

return count;

}

Please copy and paste the above funtion and try to run your code with a valid filename

Please give it thumbsup and comment below for any problem in the answer

Add a comment
Know the answer?
Add Answer to:
#include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT