Simple Concordance

Hi,

I'm a new student in programing and I have this project of writing a concordance program and I don't really know how to do it.

The program is to read a file containing:

2:1 #Ah, distinctly I
2:1 Ah, #distinctly I remember,
2:1 Ah, distinctly #I remember, it
2:1 distinctly I #remember, it was
2:1 I remember, #it was in
2:1 remember, it #was in the
2:1 it was #in the bleak
2:1 was in #the bleak December,
2:1 in the #bleak December,
2:1 the bleak #December,
2:2 #And each separate
2:2 And #each separate dying
2:2 And each #separate dying ember
2:2 each separate #dying ember wrought
2:2 separate dying #ember wrought its
2:2 dying ember #wrought its ghost
2:2 ember wrought #its ghost upon
2:2 wrought its #ghost upon the
2:2 its ghost #upon the floor.
2:2 ghost upon #the floor.
2:2 upon the #floor.
2:3 #Eagerly I wished
2:3 Eagerly #I wished the
2:3 Eagerly I #wished the morrow;
2:3 I wished #the morrow; vainly
2:3 wished the #morrow; vainly I
2:3 the morrow; #vainly I had
2:3 morrow; vainly #I had sought
2:3 vainly I #had sought to
2:3 I had #sought to borrow
2:3 had sought #to borrow
2:3 sought to #borrow
2:4 #From my books
2:4 From #my books surcease
2:4 From my #books surcease of
2:4 my books #surcease of sorrow,
2:4 books surcease #of sorrow, sorrow
2:4 surcease of #sorrow, sorrow for
2:4 of sorrow, #sorrow for the
2:4 sorrow, sorrow #for the lost
2:4 sorrow for #the lost Lenore.
2:4 for the #lost Lenore.
2:4 the lost #Lenore.
2:5 #For the rare
2:5 For #the rare and
2:5 For the #rare and radiant
2:5 the rare #and radiant maiden
2:5 rare and #radiant maiden whom
2:5 and radiant #maiden whom the
2:5 radiant maiden #whom the angels
2:5 maiden whom #the angels name
2:5 whom the #angels name Lenore,
2:5 the angels #name Lenore,
2:5 angels name #Lenore,
2:6 #Nameless here forevermore.
2:6 Nameless #here forevermore.
2:6 Nameless here #forevermore.
100000:100000 #nine times six
100000:100000 nine #times six is
100000:100000 nine times #six is forty-two
100000:100000 times six #is forty-two
100000:100000 six is #forty-two

and output onto the screen:

Work 2
1:Ah, distinctly I remember, it was in the bleak December,
2:And each separate dying ember wrought its ghost upon the floor.
3:Eagerly I wished the morrow; vainly I had sought to borrow
4:From my books surcease of sorrow, sorrow for the lost Lenore.
5:For the rare and radiant maiden whom the angels name Lenore,
6:Nameless here forevermore.

Work 100000
100000:nine times six is forty-two



So far I've tried this program:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

bool valid (string str)
{
for (int i = 0; i < str.length(); i++)
if (str[i] == '#') return true;
return false;
}

string retstr(string temp)
{
string retval = temp;
for (int i = 0; i < retval.length(); i++)
return retval;
}

bool doesexist(vector<string> list, string temp)\
{
for (int i = 0; i < list.size(); i++)
if (retstr(list[i]) == retstr(temp)) return true;
return false;
}

bool validchar(char c)
{
if (c == '#') return true;
if (c >= 'a' && c <= 'z') return true;
if (c >= 'A' && c <= 'Z') return true;
if (c == ',') return true;
if (c == '.') return true;
return false;
}

void filename(string ifname)
{
ifstream input;
string temp;
string word;
char buffer[128];
vector<string> allwords;
input.open(ifname.c_str());
if (!input.good())
{
cout << "Doesn't Exist!" << endl;
return;
}
while (!input.eof())
{
getline(input, temp);
for (int i = 0; i < temp.length(); i++)
{
if (!validchar(temp[i]))
{
if (valid(word))
{
if (!doesexist (allwords, word))
{
allwords.push_back(word);
cout << word << " ";
}
}
word = "";
} else word += temp[i];
}
if (valid(word))
{
if (!doesexist (allwords, word))
{
allwords.push_back(word);
cout << word << " ";
}
}
word = "";
}
input.close();
}

int main()
{
string ifname = "containingfile.txt";
string temp;
cout << "Input File: ";
getline (cin, temp);
if (temp != "") ifname = temp;
filename(ifname);

cout << endl << endl << endl;
system("pause");
return 0;
}


and this is my output:

#Ah, #distinctly #I #remember, #it #was #in #the #bleak #December, #And #each #separate #dying #ember #wrought #its #ghost #upon #the #floor. #Eagerly #I #wished #the #morrow; #vainly #I #had #sought #to #borrow #From #my #books #surcease #of #sorrow, #sorrow #for #the #lost #Lenore. #For #the #rare #and #radiant #maiden #whom #the #angels #name #Lenore, #Nameless #here #forevermore. #nine #times #six #is #forty-two


Please help and thank you.
Oh and I'm using C++ with the dev-C++ program
Check this out to drop the '#': http://www.cplusplus.com/reference/string/string/erase/

As far as parsing the lines goes, this is where it gets fun. I think it has to do with analysing Poe's writting style I might be wrong though I never got to do anything like this. That verse is 10 words, 11 words, 11 words, 11 words, 11 words, 3 words. This feels to static of a way to code it but I don't know of a way to distiguish sylabols in C++.

EDIT: Also, don't use dev-C++, don't even use wxDev-C++ if you haven't settled in with it yet. BloodShed is not being developed anymore and has fallen behind and wxDev-C++ has bug issues of it's own. I'm in the process of converting to Code::Blocks myself. http://www.codeblocks.org/
Last edited on
Topic archived. No new replies allowed.