【发布时间】:2017-10-02 19:48:37
【问题描述】:
大家好,我正在尝试编写一个程序,它可以在我们的 ifstream 文件中找到最长的行和单词。我有程序来查找最长的行,但在查找最长的单词时遇到了麻烦。这就是我目前所拥有的。
这是我得到的错误。
program1.cpp:44:30: error: ‘get’ was not declared in this scope
while (get(file,word))
据我所知,getline 用于获取实际行,而 get 应该获取该行中的字符。
#include <iostream>
#include<stdlib.h>
#include<getopt.h>
#include"stdio.h"
#include<fstream>
//#include<sstream>
using namespace std;
int main(int argc, char *argv[]) {
size_t longest = 0;
string longestWord;
int Lcount = 0;
int Wcount = 0;
int option;
string line;
string word;
if(argc <= 1)
{
cout << "NO FILES\n";
return 0;
}
else
{
for(int i = 1;i<argc;i++){
ifstream file (argv[i]);
if (!file.is_open() ){
cout << argv[i] << " FILE NOT FOUND\n"; // watch out for /n
}
//else if(!file.close()){
// cout << argv[i] << "NO FILES\n"; }
else{
while (getline(file,line))// Length of Longest Line
{
if(line.size() > longest){
longest = line.size();
}
else if(line.size() == longest){// Number of lines with longest length
++Lcount;}
}
while (get(file,word))
{
if(word.size() > longestWord.size()){
longestWord = word;
}
else if(word.size() == longestWord.size()){
++Wcount;}
}
}
}
}
while ((option = getopt (argc, argv, "c:")) != -1){
switch (option)
{
case 'c':
{
for(int i = 1;i<argc;i++){
ifstream file (argv[i]);
cout << argv[i] << "\n";
cout << longestWord << " (" << Wcount << ")" << "\n";
cout << longest << " (" << Lcount << ")" << "\n";
break;
}
}
default:
//cout << "UNRECOGNIZED FLAG\n";
if(option != 'c'){
cout << "UNRECOGNIZED FLAG\n";
return 0;}
}
}
//if (option != 'c')
//cout << "UNRECOGNIZED FLAG\n";
return 0;
}
【问题讨论】:
-
请发布您的
get(file, word)方法的内容。 -
我打算使用 ifstream
-
什么是line-英文句子?或“到下一个'\n'”
-
什么是单词(或者,什么子字符串特征可能不是“单词”)?也许单词没有冒号、下划线、逗号、数字,不能以数字开头。或者一个词可能是两个空格之间的任何字符串?