This is a C++ programming assignment. This program is supposed to count the numb
ID: 3933949 • Letter: T
Question
This is a C++ programming assignment. This program is supposed to count the number of vowels in a file. But it has a very subtle bug; if the last letter is a vowel, it gets counted twice. The problem is in how the WHILE loop is constructed. Fix that please.
NOTE: You only have to fix the while loop in the program. Please show me the output without any errors.
#include #include using namespace std; /This program is supposed to count the number of vowels in a file. But it // has a very subtle bug: if the last letter is a vowel, it gets counted twice. // The problem is in how the while loop is constructed. Fix that int main (int argc, char argv[]) if (inf) cerrExplanation / Answer
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main(int argc, char* argv[])
{
ifstream inf(argv[1]);
if(!inf)
{
cerr<<"Error opening "<<argv[1]<<" for input."<<endl;
return 1;
}
char ch;
size_t count = 0;
char vowels[] = "aeiouAEIOU";
while(inf>>ch)
{
count += (strchr(vowels,ch)) ? 1:0;
}
inf.close();
cout<<"File "<<argv[1]<<" includes "<<count<<" vowels."<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.