Counting lines from a file!

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
1
2
3
4
5
6
7
int count = 0;
string line;
while( getline(cin, line) ){
   vector<string> words = separate(line, ' ');
   //...
   ++count;
}
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
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.