c++ - Infinite While Loop and Getting Incorrect Output -


i trying build program reads in file, scans through file, , outputs words in file surrounded " marks. stumped , hoping help!

#include <iostream> // file i/o: #include <fstream> #include <cstdlib> #include <iomanip>  using namespace std;  // prototype count function can have below it's first // use in main(). void count(istream& in, int& lines, int& words, int& characters); /*  * wc <filename>   */  int main(int argc, char *argv[]) {  if (argc < 2) {    cerr << "usage: wc <filename>" << endl;   return 0;  }  // open file specified argv[1] reading:   // constructs ifstream object called "in":   ifstream in(argv[1]);   // there problem opening file?   if (!in.good()) {    cerr << "unable open file [" << argv[1] << "] reading." << endl;    return 1;   }     int lines = 0, words = 0, characters = 0;   count(in, lines, words, characters);    cout << setw(5) << lines << " " << words << " " <<     characters << " " <<           argv[1] << endl;     // close input stream:   in.close();    }     void count(istream& in, int& lines, int& words, int& characters)     {    int i;    char s;    int ch;    bool inword = false;    // read until end of file reached, or there error:   while (!in.eof()) {    // read character input stream "in":     s = in.get(); //set char s = in.get      for(i=0; s != 0; i++){ //loop iterate through characters       while(s == '"'){ //while s equal "        cout << s << endl; // print s         if(s == '"') // if hit ", break         break;       }     }     if (in.good() == false) return;    characters++;    if (!isspace(ch) && !inword) {     inword = true;     words++;     } else if (isspace(ch) && inword) {      inword = false;     }     if (ch == '\n') lines++;     }     } 

your algorithm seems wrong.. in loop compare 's' you're not updating it... try in main loop(qnd):

while (!in.eof() && (s = in.get()) != '"'); // read first quote char /* word want.. run end quote marks.. */ while (!in.eof() && (s = in.get()) != '"') {     cout << s; } cout << endl; 

Comments