Name: Anonymous 2009-04-07 17:10
I keep getting errors compiling and I can't seem to resolve them anyone spot anything?
/*
* This program counts the number of characters in each line including white
* spaces, tabs etc.
* In case of a tie the preceding line is chosen as either the shortest or
* the longest line.
*/
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
int main (int argc, char *argv[])
{
unsigned int lines=0, minline=1, maxline=1;
unsigned int minChars=65535, maxChars=0, totChars=0;
ifstream inpfile (argv[1]);
string inpline;
unsigned int len;
if (!inpfile.is_open())
{
cout << "Can't open argument file : " << argv[1] << endl;
exit(1);
}
getline(inpfile, inpline, '\n');
while (!inpfile.eof()) // Read until EOF is reached.
{
lines++; // Read another line.
len = inpline.length();
totChars += len;
if (len < minChars)
{
minChars = len;
minline = lines;
}
if (len > maxChars)
{
maxChars = len;
maxline = lines;
}
getline(inpfile, inpline, '\n');
}
inpfile.close();
if (lines == 0 && (len = inpline.length()) > 0)
{
totChars = len;
minChars = maxChars = len;
lines = 1;
}
// The file has been read.
if (lines > 0) // Print the summary if file has atleast 1 line.
{
cout << "Line " << minline << " is the shortest line with " << minChars << " characters\n";
cout << "Line " << maxline << " is the longest line with " << maxChars << " characters\n";
cout << "The Input file has " << lines << " lines\n";
cout << "Average number of characters per line = " << totChars/(float)lines;
} else {
cout << "Input file is empty!\n";
}
return 0;
}