I am trying to write a program in c++ that will read the text from a fle and cou
ID: 3550008 • Letter: I
Question
I am trying to write a program in c++ that will read the text from a fle and count the number of times it reads the letter 'e' . My code now is not reading 'e' at all. I am not sure if I am close or not but any help would be greatly appreciated.
here is my code:
#include<iostream>
#include <fstream>
using namespace std;
int main()
{
int text;
int count(0);
fstream inputStream;
inputStream.open("foo.txt");
inputStream >>text;
int i, score[20];
char e;
for (i=1;i<20;i++)
{
if (score[i]=='e')
cout<<count++<<" ";
else if (score[i]!='e')
cout<<count<<" ";
}
return 0;
}
Explanation / Answer
Dear,
Here is the modified code:
#include<iostream>
#include <fstream>
using namespace std;
int main()
{
char ch;
int count=0;
fstream inputStream;
inputStream.open("foo.txt");
//Validate whether file exists
if(!inputStream)
{
cout<<"Cannot open " << "foo.txt"<<endl;
}
//process file
while(!inputStream.eof())
{
inputStream.get(ch);
if(ch== 'e')
count++;
}
//Display 'e' count
cout<<"Number of 'e' character's in file :"<<count<<" ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.