Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

Explanation / 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;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote