How to count number of letters in words that start with d?

So I need help with this code im doing for an assignment. I have the first part down which was to open and process a file and report the number of words with the letter D irrespective of case. What i need help with now is how many letters those words starting with D have, with a maximum of 10 letters.

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
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include <cstdio>

using namespace std;
//main body (driver function)
int main(void)
{
        int count;
        char curr, prev;
        char wordLength [10];
        string inputFileName;
        string s;
        ifstream fileIn;
        //input a txt file's name here
        cout<<"Enter name of file of characters :";
        cin>>inputFileName;
        fileIn.open(inputFileName.data());
        assert(fileIn.is_open() );
        curr=' ';
        count=0;
        while(!(fileIn.eof())){
                prev=curr;
                curr=fileIn.get();
                if(prev==' '){
                        //switch to catch both cases of D
                         switch (curr){
                         case 'd':
                         case 'D':
                                 count++;
                        }
                }
        }

        cout<<"The number of words starting with the letter D are "<<count<<endl;
        return 0;
}
you have made it a little weirder by reading 1 letter at a time, but that is OK. In general it is usually better to read at least a whole line or a good chunk of letters at once, if not the entire file all at once.

as you have it, though.. a starting point:
line 27
1
2
3
4
5
6
7
8
9
10
                       if(prev==' ')
                       {
                        currentwordlength = 1; //one because it started with a letter...
                        //switch to catch both cases of D
                         switch (curr){
                         case 'd':
                         case 'D':
                                 count++;
                        }
                       else currentwordlength++;

that should be close, but it is the idea anyway. Note that you count the letters of all words with what I have here, but you need to discard that value if it didn't start with the d... see if you can work this idea into what you have correctly.

line 13 seems to be nonsense. What was that supposed to be (its currently a c-style string).
Last edited on
For line 13 my professor advised me to create a char array called WordLength with a size of 10
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
30
31
32
33
34
35
#include <iostream>
#include <fstream>
#include <string>

int main() {
    std::string inputFileName;

    std::cout << "Enter name of file of characters :";
    std::cin >> inputFileName;

    std::ifstream fileIn(inputFileName);

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

    unsigned cnt[10] {};
    unsigned totCnt {};

    for (std::string word; fileIn >> word; )
        if (word[0] == 'D' || word[0] == 'd') {
            ++totCnt;

            if (word.size() <= 10)
                ++cnt[word.size() - 1];
        }

    std::cout << "The number of words starting with the letter D are " << totCnt << '\n';
    std::cout << "By size, the number with 10 or fewer letters are:\n";

    for (unsigned i = 0; i < 10; ++i)
        if (cnt[i])
            std::cout << i + 1 << "  " << cnt[i] << '\n';

    std::cout << '\n';
}


This somewhat depends, of course, as to what is defined as a 'word'. Here words are separated by white-space chars.
Last edited on
would there be a way to specify it and display it "the number of words with 1 letter is: (number of D words with 1 letter) " and "the number of words with 2 letters is: " and so on until 10 because he wants us to be as specific as possible with the code.
Yes - just change L31-32 as required.

It currently just displays the number of words that have a count of > 0. But if you comment out L31 then it will display the count for all values of 1 to 10.

Obviously L32 can be changed to display in any required style.
I got it to work, thank you all!
Topic archived. No new replies allowed.