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.
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include <cstdio>
usingnamespace 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).
#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.
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.
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.