It either prints correct value of vowels or the correct value for 'to' & 'these' but never both

#include<fstream>
#include<iostream>

using namespace std;
int main()
{char ch[20],c; int count1=0; int count2 =0; int count3 =0;
ifstream fin; ofstream fout;
fout.open ("data.txt ", ios::out);
for(int i=0;i<8; i++)
{cin>>ch;
fout<<ch<<" ";
}
fout.close();

fin.open ("data.txt ", ios::in);
while(fin)
{fin.get(c);
if (c == 'a' || c == 'e' || c == 'o' || c == 'i' || c == 'u'||c == 'A' || c == 'E' || c == 'O' || c == 'I' || c == 'U')
count3++;
}
fin.open ("data.txt ", ios::in);
while(fin)
{
fin>>ch;
if((ch[0])=='A'||ch[0]=='a')
count1++;
if(strcmp(ch,"to")==0||strcmp(ch,"the")==0)
count2++;

};
fout.open("data.txt",ios::out|ios::app);
fout<<'\n'<<"words starting with 'a' or 'A' is "<<count1<<'\n'<<"number of times 'to' or 'the' occurs is "<<count2<<'\n'<<"number of vowels is "<<count3;
fout.close();
return 0;
}
In the middle loop you read from fin until fin enters an error state. You never clear the error state or close the file, but you then attempt to reopen the file and read from it again. That isn't going to be productive.
Any suggestions on how to do it in one loop itself? That would me most helpful..
Topic archived. No new replies allowed.