我正在计算文件中字符的数量,但我想计算少于5和6或更大的单词数 [英] im counting the number of characters in a file but i want to count the number of words that are less than 5 and 6 or greater
问题描述
我想这样做: 一次读取文件中的单词. (使用字符串执行此操作) 计算三件事:文件中有多少个单字符单词,文件中有多少个短字符(2至5个字符),文件中有多少个长字符(6个或更多字符).在这里帮助
i want to do this: reads the words in the file one at a time. (Use a string to do this) Counts three things: how many single-character words are in the file, how many short (2 to 5 characters) words are in the file, and how many long (6 or more characters) words are in the file. HELP HERE
我不确定如何将文件读入字符串.我知道我必须要做类似的事情,但是我不理解其余的事情.在这里帮助
im not sure on how about reading file into a string. i know i have to something like this but i dont understand the rest. HELP HERE
ifstream infile;
//char mystring[6];
//char mystring[20];
int main()
{
infile.open("file.txt");
if(infile.fail())
{
cout << " Error " << endl;
}
int numb_char=0;
char letter;
while(!infile.eof())
{
infile.get(letter);
cout << letter;
numb_char++;
break;
}
cout << " the number of characters is :" << numb_char << endl;
infile.close();
return 0;
推荐答案
我不太确定从哪里开始...
I'm not quite sure where to start...
您的循环:
while(!infile.eof())
{
infile.get(letter);
cout << letter;
numb_char++;
break;
}
由于额外的break;
此代码看起来也像是尝试读取文件中的字符数,而不是计算5个字母或大于6个字母的单词数.
Also this code looks like it is trying to read the number of characters in a file, and not count up the number of words that are 5 letters or greater than 6 letters.
尝试类似的东西:
ifstream infile;
int main(){
infile.open("file.txt");
if(!infile.good()){
cout << " Error " << endl;
return 1;
}
int shortCount = 0;
int mediumCount = 0;
int longCount = 0;
int charCount = 0;
char letter;
while(!infile.eof()){
infile >> letter;
if(letter == ' ' || char == EOF){ // end of word or file.
if(charCount == 1)
shortCount++;
else if(charCount < 6)
mediumCount++;
else
longCount++;
charCount = 0;
}else{
charCount++;
}
}
cout << "Short Words: " << shortCount << endl;
cout << "Medium Words: " << mediumWords << endl;
cout << "Long Words: " << longWords << endl;
infile.close();
return 0;
}
这篇关于我正在计算文件中字符的数量,但我想计算少于5和6或更大的单词数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!