#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
ifstream inf(argv[1]);
if (!inf)
{
cerr << "Error opening " << argv[1] << endl;
return 1;
}
char ch;
size_t count = 0;
string vowels = "aAeEiIoOuU";
size_t p;
p = vowels.find(ch);
inf >> ch;
while(!inf.eof())
{
if (p != string::npos)
{
count++;
}
inf >> ch;
}
inf.close();
cout << "File " << argv[1] << " includes " << count << " vowels." << endl;
return 0;
}
I have problem with the part
inf >> ch;
while(!inf.eof())
{
if ( p != string::npos)
{
count++
}
inf >> ch;
}
Basically, the program look up the text.txt file and count how many vowels it have. I want to repeat inside of while loop. If I include "inf >> ch;" at the end of the while loop, the program counts the vowel wrong. If I do not, the program freezes when I run it. Can you please help me? Thank you.
hints :
I have to use
string vowels = "aAeEiIoOuU";
the function call vowels.find(ch) will return an index that is not string::npos if ch is a vowel.