Counting lines from a file!
Apr 23, 2013 at 3:01am UTC
Hello does anyone know how I would go about counting lines from a file at the same time as extracting words? I need to know the line number to output what line the word is misspelled on. I tried getline and using sstreams but I could not get it to work. Any pointers would be appreciated!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
void SpellCheck::checkValidWords(const string& inFileName)
{
string eachword;
ifstream istream;
istream.open( inFileName.c_str() );
if ( !istream.is_open() )
{
cout <<"ERROR:FILE NOT FOUND"
<<endl
<<"PLEASE CONTACT CUSTOMER SUPPORT"
<<endl
<<endl;
exit(1);
}
string line;
cout <<"File Opened! Congratulations!" << endl;
while ( !istream.eof() )
{
//SO SOMWHERE IN HERE COUNT THE LINES AND IF IT SPELLED WRONG NOTE
string copy; // THE COUNT
istream >> eachword;
copy = eachword;
//cout << copy << " ";
clean(copy);
toLower(copy);
//cout << copy << endl;
string altered = copy;
TreeItemType item;
bool win;
KeyType eachwordo = altered;
dictionary.searchTreeRetrieve(eachwordo,item,win);
if (win == false )
{
cout << "Misspelled word: " << eachword << endl;
}
}
istream.close();
cout << "Spell Check Completed" << endl;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
Last edited on Apr 23, 2013 at 3:02am UTC
Apr 23, 2013 at 3:06am UTC
1 2 3 4 5 6 7
int count = 0;
string line;
while ( getline(cin, line) ){
vector<string> words = separate(line, ' ' );
//...
++count;
}
Apr 23, 2013 at 3:09am UTC
Well that's the thing, I can't use vectors because we have not gone over them but thanks for the help anyway. My proffessor just told us that there was a way to do it with <sstream> but I couldn't use them correctly
Apr 23, 2013 at 6:03am UTC
well I figured it out of anyone was interested(doubtedly)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
unsigned int linecount = 0;
string inputBuffer = "" ;
while ( !istream.eof() )
{
getline(istream,inputBuffer);
istringstream inputStream(inputBuffer);
string word = "" ;
linecount++;
while (inputStream >> word)
{
string copy;
copy = word;
clean(copy);
toLower(copy);
string altered = copy;
TreeItemType item;
bool win;
KeyType eachwordo = altered;
//linecount++;
dictionary.searchTreeRetrieve(eachwordo,item,win);
if (win == false )
{
cout << linecount << " Misspelled word: " << word << endl;
}
}
}
istream.close();
cout << endl << "Spell Check Completed" << endl;
}
Topic archived. No new replies allowed.