Adding More Functionality To Occurrence Program.

This is my current code, it counts the number of words that do not begin with the letter d, no matter the case. and the number of words beginning with letter d, no matter the case.

I want to add this functionality, I need help I am beginner and trying to learn, it likes it is simple but I don't understand it fully still
Query the user for file, Reporting the number of words beginning with the letter d (irrespective of case) that have one letter, the number of words having two letters, the number of words having three letters, and so on.

#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
using namespace std;

int main(void)
{
string inputFileName, s;
ifstream fileIn;
int word_count = 0, dCount = 0;

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

fileIn.open(inputFileName.data());
assert(fileIn.is_open());

while (fileIn >> s) {
word_count++;
dCount += s[0] == 'd' || s[0] == 'D';
}

cout << "Number of words starting with d or D is " << dCount << endl;
cout << "Number of words not starting with d or D is: " << word_count - dCount << endl;
}
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
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
using namespace std;

int main(void)
{
	constexpr int maxWrdSze {20};
	string inputFileName, s;
	ifstream fileIn;
	int dCount = 0;
	int wrdsze[maxWrdSze] {};

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

	fileIn.open(inputFileName.data());
	assert(fileIn.is_open());

	while (fileIn >> s)
		wrdsze[s.size() - 1] += s[0] == 'd' || s[0] == 'D';

	for (int c = 0; c < maxWrdSze; ++c)
		if (wrdsze[c])
			std::cout << wrdsze[c] << " words of length " << c + 1 << " start with 'd or 'D'\n";
}

Thats nice, but i believed it wanted u to count the numbers of times the words begin with d that have one letter, then other words in the file that have two , 3 , 4 letters, 5 letters etc.

How do we modify it to do that?
Well d/D by itself is not a word. If a word begins with d/D that have one letter, then that is just d/D with no following characters. Do you mean d/D followed by just 1 char (eg do)? What is actually required?

Have you got a sample input and expected output?
Last edited on
No its suppose to count like that only the first letter has to be D or d dont count for anything else,
So its suppose to have a maximum size for the max word length can be, so if i had a word Bundeedle i need it to count the number of letters there are in that word, has to be done for all the words in the text file, assuming max word length is whatever u want it to be. How can I make that work what is the best way can u show me the code?
The instructions aren't clear but thats what I feel like is right,

these are the instructions:
Query the user for file, Reporting the number of words beginning with the letter d (irrespective of case) that have one letter, the number of words having two letters, the number of words having three letters, and so on.
Similar to this example not sure how to modify it,

#include <iostream>
#include <fstream>
#include <string>
#include <cassert>

using namespace std;

int main(void)
{ string inputFileName;
string s;
ifstream fileIn;
int word_count;
word_count=0;
cout<<"Enter name of file of characters: ";
cin>>inputFileName;
fileIn.open(inputFileName.data());
assert(fileIn.is_open());
while (fileIn>>s)
{cout<<s<<" "<<s.length()<<endl;
word_count++;
}
cout<<"Number of words in file: "<<word_count<<endl;
return 0;
}

I think this would be best tackled with a map (AKA associative container, dictionary).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include <map>
#include <string>
using namespace std;

int main()
{
	ifstream fin("test.txt");
	
	map<int, int> length_occurrences;
	
	string word;
	while (fin >> word)
	{
		length_occurrences[word.length()]++;
	}
	
	for (const auto& item : length_occurrences)
	{
		cout << item.second << " words have " << item.first << " letters\n";
	}
}

Example output:
1 words have 2 letters
4 words have 3 letters
5 words have 4 letters
5 words have 5 letters
3 words have 6 letters
2 words have 7 letters
1 words have 8 letters
Yes, I also agree with that, but it is only suppose to do it for letters that begin with d so if they have 1 letter report it if they have 2 letters report it if they have 3 letters report it, etc how do we do that? How would i specify a maximum word length of a word to count if I wanted to do that as well?
I know, it was an example. I didn't want to write the entire assignment.
how do we do that?
An if statement would be a good start.

1
2
3
4
5
	while (fin >> word)
	{
		if (/* word begins with d [fill this in] */)
			length_occurrences[word.length()]++;
	}


How would i specify a maximum word length of a word to count if I wanted to do that as well?
1
2
3
4
5
if (word.length() <= SomeMaxLength))
{
	if (/* word begins with d [fill this in] */)
		length_occurrences[word.length()]++;
}
Last edited on
Topic archived. No new replies allowed.