我正在尝试编写一个程序,将输入文件与填充了大量单词的字典文件进行比较。比较单词后,我想输出拼写不正确的单词。这是我的代码:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
using namespace std;
void trim(string s)
{
size_t p = s.find_first_not_of(" \t");
s.erase(0, p);
p = s.find_last_not_of(" \t");
if (string::npos != p)
s.erase(p+1);
}
int main()
{
ifstream input;
ifstream words;
input.open("/Users/jordan/Desktop/CS60/Word_dictionary_check/input.txt");
if(input.fail())
{
cout<<"Input file opening failed";
exit(1);
}
words.open("/Users/jordan/Desktop/CS60/Word_dictionary_check/words.txt");
if(words.fail())
{
cout<<"Words file opening failed";
}
vector <string> wordCheck;
vector <string> misspelledWord;
string temp = "";
while(!input.eof())
{
input>>temp;
wordCheck.push_back(temp);
}
ofstream output;
output.open("/Users/jordan/Desktop/CS60/Word_dictionary_check/output.txt");
if(output.fail())
{
cout<<"Output file opening failed";
exit(1);
}
for(int j = 0; j < wordCheck.size(); j++)
{
bool dontprint = false;
while(!words.eof())
{
words>>temp;
if(temp == wordCheck[j])
{
dontprint = true;
}
}
if(dontprint == false)
{
misspelledWord.push_back(wordCheck[j]);
}
}
for(int i = 0; i < misspelledWord.size() ; i++)
{
output<<misspelledWord[i]<<endl;
}
return 0;
}
我相信使用空格或比较字符串是一个问题。谢谢你帮助我!
答案 0 :(得分:0)
我可以看到几个明显的问题。我添加了评论。这应该可以解决您的问题,但我不打算为您编写代码。
for(int j = 0; j < wordCheck.size(); j++)
{
bool dontprint = false;
//Make your words file pointer to point to start of file. USe seek function
while(!words.eof())
{
words>>temp;
if(temp == wordCheck[j])
{
dontprint = true;
//You can break here. As once word is found, you don't need to check the word file further
}
}
if(dontprint == false)
{
misspelledWord.push_back(wordCheck[j]);
}
}