Infinite Loop and Outputting Incorrectly?

Hello! I am currently trying to write a program that will take a file, check if that file has any words surrounded by quotes(") and print out all of the characters between each quote. The program I have now just infinitely loops and outputs " instead of the characters.

Here is the code:

#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();
for(i=0; s != 0; i++){ //as long as s isn't equal to NULL, keep going
if(s == '"'){//if s == ", start printing everything after that
cout << s << endl;
if(s == '"')//if we find " again, stop printing and 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++;
}
}
Last edited on
1
2
3
4
5
6
7
8
		for (i = 0; s != 0; i++)
		{
			if (s == '"')
			{
				cout << s << endl;
				if (s == '"') break;
			}
		}
please comment each line of that snip with what you think it is doing.
Okay. I put the comments in my original code!
Topic archived. No new replies allowed.