This is a C programming assignment This program is supposed

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) cerr

Solution

#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;
}

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 vo

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site