-
Max Word Count
Im trying to write a program that reads from a text file and then outputs the total words, most words per line, total bumber of lines and average words per line. The problem is that ive got a total mental block on how to get the most words per line thing working. All i can do so far is get it to be the same as the word count, and its starting to do my head inso any help will be appreciated. I get the feeling that its one of those things where ill kick myself once i know the anwer.
Code:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
void main(int argc, char **argv)
{
// make sure that the user has specified a filename
if(argc < 2)
{
cout << endl << "You must enter a filename" << endl << endl;
return;
}
string filename = argv[1];
ifstream fin(filename.c_str()); // define our file input stream
// make sure that the file exists
if(!fin)
{
cout << endl << "Cannot find file" << endl << endl;
return;
}
/********************************************************************
Process the text data file
********************************************************************/
cout << endl << "Reading input file: " << filename << endl << endl;
int words = 0, max = 0; // to count words and longest line
string temp;
for (int line = 0; !fin.eof(); line ++)
{ // for each line in file ...
getline(fin, temp); // get the line into temp string
istringstream sstr(temp); // create string stream tokeniser
while (sstr >> temp) { // for each word from line ...
words++; // increment word count for line
max++; // update total words so far
}
// update longest line if latest was longer
}
/********************************************************************
Output the stats
********************************************************************/
cout << endl << "Total lines:" << line ;
cout << endl << "Total words:" << words ;
cout << endl << "Max line: " << max << " words" ;
cout << endl << "Average: " << (words/(double)line) << " words per line" ;
cout << endl << endl ;
}
Thanks for your help, by the way i know its a bit messy but ill tidy it up when finished lol
-
Havn't looked at your code to much because I stopped at the void main. main is not void it should be int main.
As for your problem you could just create a variable(bigLine for ex) to hold the current line number you are reading then compare the line you just read to the last one you just read and if its bigger than that you change bigLine to that line number
-
ok so what if i did leave main at say void. What would that mean? Im new to C++ as i used to use java.
-
It would mean you should click on the FAQ link and go read why it's wrong.
Quzah.
-
Hello,
Along-side with the FAQ, here is more documentation of why returning main() as void is undefined.
In the C language, the void main() construct is undefined. C++ somewhat is safer in this regard; any definition of the main function whose return type is other than int requires a diagnostic. However, the behavior is still ultimately undefined.
Here are 3 key points why void main() is undefined:
- The standard says so.
- The startup routines that call main() could be assuming that the return value will be pushed onto the stack. If main() does not do this, then this could lead to stack corruption in the program's exit sequence, and cause it to crash.
- You are likely to return a random value to the invokation environment. This is bad, because if someone wants to check whether your program failed, or to call your program from a makefile, then they won't be able to guarantee that a non-zero return code implies failure.
Remember, declaring a function as void does not merely shut off or rearrange warnings: it may also result in a different function call/return sequence, incompatible with what the caller (in main's case, the C run-time startup code) expects. The ANSI standard says "no" to void main(), which should be an end of it.
- Stack Overflow
-
Thank you, still cant seem to get most words per line working though-i know its something simple and i think its something i know i should be able to do which is why it is so annoying
-
How about creating a program to find the largetst value in an array? Once you have a handle on that, try adapting that method to work with what you want to do?
Quzah.
-
Ok i now realise i have to create a new variable-say int words_per_line and assign that to max if it is bigger with each line. But my question now is that in the code i have, how do i take it on a line by line basis, because all it seems to do is read it all in and i want to be able to have a count at the end of the line.
Code:
int words = 0, max = 0; // to count words and longest line
string temp;
for (int line = 0; !fin.eof(); line ++)
{ // for each line in file ...
getline(fin, temp); // get the line into temp string
istringstream sstr(temp); // create string stream tokeniser
while (sstr >> temp) { // for each word from line ...
words++; // increment word count for line
max++; // update total words so far
}
// update longest line if latest was longer
}
I think its because of the while loop i have used but i cant think of another way of doing it. Any help would be appreciated
-
If you set 'words' to zero before counting the words in each line, then when the while loop is finished you would know exactly how many words were in only that line. If its greater than the current max line, update the current max otherwise don't.
-
Right ive sorted it-well kind of. The problem now is that unless the return key is pressed at the end of a line, the words per line continues to count unless return has been pressed. This means that when word wrap is turned on, the number of lines is calculated correctly, but the words per line is not, as it is really a words till return key is pressed calculation. Does anyone know how i could solve that given this code:
Code:
// make sure that the user has specified a filename
if(argc < 2)
{
cout << endl << "You must enter a filename" << endl << endl;
return;
}
string filename = argv[1];
ifstream fin(filename.c_str()); // define our file input stream
// make sure that the file exists
if(!fin)
{
cout << endl << "Cannot find file" << endl << endl;
return;
}
/********************************************************************
Process the text data file
********************************************************************/
cout << endl << "Reading input file: " << filename << endl << endl;
int words = 0, max = 0; // to count words and longest line
string temp;
for (int line = 0; !fin.eof(); line ++)
{ // for each line in file ...
getline(fin, temp); // get the line into temp string
istringstream sstr(temp); // create string stream tokeniser
for (int wc=0; sstr>>temp; wc++) {
// for each word from line ...
// increment word count for line
words++;
// update total words so far
}
if (wc>max) {
max = wc;
}