Adding More Functionality To Occurrence Program.

Oct 2, 2021 at 8:50am
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;
}
Oct 2, 2021 at 10:46am
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";
}

Oct 2, 2021 at 10:03pm
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?
Oct 3, 2021 at 9:30am
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 Oct 3, 2021 at 9:37am
Oct 5, 2021 at 9:41pm
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?
Oct 5, 2021 at 9:53pm
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.
Oct 5, 2021 at 10:43pm
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;
}

Oct 6, 2021 at 5:02pm
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
Oct 7, 2021 at 5:29pm
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?
Oct 7, 2021 at 6:09pm
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 Oct 7, 2021 at 6:10pm
Topic archived. No new replies allowed.