2013-07-14 122 views
-2

好吧,即时做一个家庭作业,其中我假设读一个文本文件与100选定的单词,然后阅读另一个“文本”文件,并比较这100个选定的单词中有多少显示在“文本”文件上,以及文本文件中最长和最短的单词以及这些选定单词的时间长度。我需要完整的帮助,这是我迄今为止所获得的。顺便说一下,我仍然还没有inputed选定的文件又是那么这就是为什么那些IFS语句是不完整的我需要帮助做一个单词搜索和计数

#include "stdafx.h" 
#include <iostream> 
#include <iomanip> 
#include <fstream> 
#include <string> 


using namespace std; 


int main() 
{ 
int fileNumber; 
string Words [100]; 
string wordCounts [100]; 
string Frequencies [100]; 
string shortest = "abcdefghijklmnopqrstuvwxyz"; 
string longest = ""; 
int totalWordCount = 0; 
ifstream inData; 
int I=0; 

inData.open("SelectWords.txt"); 

while (!inData.eof()) { 
    inData >> Words [I]; 

} 
int irow; 
while (irow=0, irow<=100, irow++) { 
cout<<Words[I]<<endl; 
} 
cout<<"Choose a file you want to open"<<endl; 
cout<<"1= A Modest Proposal"<<endl; 
cout<<"2=Apology" <<endl; 
cout<<"3=The Call of the Wild"<<endl; 
cout<<"4=The Adventures of Tom Sawyer"<<endl; 
cin>> fileNumber; 
while ((fileNumber<1) || (fileNumber>4)) 

    { 
cout<<"Error please try again"<<endl; 
cout<<"Choose a file you want to open"<<endl; 
cout<<"1 - A Modest Proposal"<<endl; 
cout<<"2 - Apology" <<endl; 
cout<<"3 - The Call of the Wild"<<endl; 
cout<<"4 - The Adventures of Tom Sawyer"<<endl; 
cin>> fileNumber; 
} 

    if (fileNumber==1) 
{ 
} 
if (fileNumber==2) 
{ 
} 
if (fileNumber==3) 
{ 
} 
if (fileNumber==4) 
{ 
} 



system ("pause"); 
return 0; 
} 
+2

'while(!inData.eof())'是错误的。谁指示你写这个? –

+2

你能否澄清你到底需要什么帮助? – Schickmeister

+1

你应该检查http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong有关使用while(!inData.eof()) –

回答

2

我会做这样的事情:

std::string filename; 
switch (filenumber) 
{ 
    case 1: 
    filename = "modest_proposal.txt"; 
    break; 
    case 2: 
    filename = "apology.txt"; 
    break; 
    //... 
} 

std::ifstream target_file(filename.c_str()); 
if (!target_file) 
{ 
    std::cerr << "Error opening file " << filename << endl; 
    return EXIT_FAILURE; 
} 

std::string word; 
while (target_file >> word) 
{ 
    // Search word list 
} 

注意如何我只用switch确定文件名。无论文件名是什么,算法的其余部分都是相同的。

编辑1:建议

  1. 使用std::map<word, count>,而不是两个单独的阵列。
  2. 为菜单创建一个文本变量并将变量打印两次 而不是复制代码。
+0

继承人我的任务上的完整说明 – PopperJuan

+0

完整说明在哪里?我没有看到编辑的帖子。我没有看到任何链接。 –

+0

如果我的回答有帮助,请点击复选标记。 –