C++ Help Finding Occurrences of letter d

Have to do this and not sure how to do it. Need help with doing it.
Instructions:
Query the user for the name of a file and then count and
report the number of words that do not begin with the letter d, irrespective of case and the
number of words that do begin with the letter d, irrespective of case.


Currently the code, finds vowels in the text file that is inputted, it needs to be fixed so that it can count and report the number of words that do not begin with the letter d, irrespective of case and the number of words that do begin with the letter d, irrespective of case.

#include <iostream> // cin, cout
#include <fstream> // ifstream, ofstream
#include <string> // string
#include <cassert> // assert()
using namespace std;

int main()
{
string fileName;
cout << "Enter name of file: ";
getline(cin, fileName);

ifstream inFile(fileName.data());
assert(inFile.is_open());

unsigned vowelCount = 0;
char ch;

for (;;)
{
inFile >> ch;
if (inFile.eof()) break;

if (islower(ch))
ch = toupper(ch);

switch (ch)
{
case 'A': case 'E': case 'I':
case 'O': case 'U': vowelCount++;
}
}

cout << "\nThere are " << vowelCount << " vowel(s) in the file "
<< fileName << endl;
}



The logic should be simple:
while read word
  if word starts with 'd'
  then increase d_word_count
  else increase other_word_count
Perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cctype>

int main() {
	std:: string fileName;

	std::cout << "Enter name of file: ";
	std::getline(std::cin, fileName);

	std::ifstream inFile(fileName.data());

	if (!inFile)
		return (std::cout << "Cannot open file\n"), 1;

	unsigned dCount {}, wrdCount {};

	for (std::string line; std::getline(inFile, line);) {
		std::istringstream lss(line);

		for (std::string word; lss >> word; ++wrdCount)
			dCount += static_cast<char>(std::toupper(static_cast<unsigned char>(word.front()))) == 'D';
	}

	std::cout << "There are " << dCount << " words that begin with d\n";
	std::cout << "There are " << wrdCount - dCount << " words that do not begin with d\n";
}

Nice I like that code there, Can someone show me the way to do it with, static arrays, or switch/case statements, or the logic keskiverto said,

while read word
if word starts with 'd'
then increase d_word_count
else increase other_word_count

I am still learning c++ and I am beginner and trying to understand this simple program that we have to do.

Thanks.

Last edited on
The only real difference is that I count all the words and those starting with d. Those not starting with d are obviously the difference.

1
2
3
4
5
while read line
   while read word in line
      increment total word count
      if word starts with d
          then increment d count


L20 reads each line in the file
L21, 23 reads each word in the line and increments wrdCount
L24 increments dCount if the word starts with d
Topic archived. No new replies allowed.