C++ 无限While循环并获得不正确的输出

C++ 无限While循环并获得不正确的输出,c++,function,if-statement,for-loop,while-loop,C++,Function,If Statement,For Loop,While Loop,我目前正试图建立一个程序,读取一个文件,扫描该文件,并输出该文件中被“标记”包围的所有单词。我目前感到困惑,希望得到一些帮助 #include <iostream> // For file I/O: #include <fstream> #include <cstdlib> #include <iomanip> using namespace std; // Prototype the count function so we can have

我目前正试图建立一个程序,读取一个文件,扫描该文件,并输出该文件中被“标记”包围的所有单词。我目前感到困惑,希望得到一些帮助

#include <iostream>
// For file I/O:
#include <fstream>
#include <cstdlib>
#include <iomanip>

using namespace std;

// Prototype the count function so we can have it 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 the file specified by argv[1] for reading:
  // Constructs a ifstream object called "in":
  ifstream in(argv[1]);
  // Was there a problem opening the file?
  if (!in.good()) {
   cerr << "Unable to open file [" << argv[1] << "] for 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 the 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 the end of file is reached, or there was an error:
  while (!in.eof()) {
   // Read a character from the input stream "in":
    s = in.get(); //Set char s = in.get
     for(i=0; s != 0; i++){ //Loop to iterate through the characters
      while(s == '"'){ //While s is equal "
       cout << s << endl; // Print s
        if(s == '"') // If we hit another ", then we 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++;
    }
    }
#包括
//对于文件I/O:
#包括
#包括
#包括
使用名称空间std;
//对count函数进行原型化,这样我们就可以在它的第一个
//在main()中使用。
无效计数(istream&in、int&lines、int&words、int&characters);
/*
*厕所
*/
int main(int argc,char*argv[])
{
如果(argc<2){

cerr您的算法似乎是错误的..在for循环中,您将与's'进行比较,但您没有更新它…在主循环(QnD)中尝试类似的操作:

while(!in.eof()&&(s=in.get())!=''”;//读取到第一个引号字符
/*这是我们想要的单词..运行到结尾引号*/
而(!in.eof()&&(s=in.get())!=“”){

我可以这样做,但我似乎仍然是无限循环。它也没有打印任何东西,它似乎只是cout-ing-endl。忽略最后的评论,我找到了它为什么是无限循环。我现在得到了seg错误。有没有评论为什么会这样?
while (!in.eof() && (s = in.get()) != '"'); // read to first quote char
/* this is the word we want.. run to end quote marks.. */
while (!in.eof() && (s = in.get()) != '"') {
    cout << s;
}
cout << endl;