Thanks for your help almost there
Here is my latest code. I did the get command as you suggested. My book didnt have a good syntax for its usage. I now have the following input file to test the program.
My minimum line and maximum line is off by one digit , for example line 2 says 17 should be 16.
Any suggestions on fixing this. Also the average number of characters per line seems high to me.
Program
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
using namespace std;
int main()
{
unsigned int i=0, line=0, minLine=0, minChars=65535, maxLine=0, maxChars=0, totChars=0;
ifstream inStream;
inStream.open("input.txt", ios::in);
assert( inStream.is_open() );
if(!inStream)
{
cerr << "File could not be opened!" << "\n";
exit(1);
}
while(!inStream.eof())
{
i++;
char ch;
ch = inStream.get();
if ( ch == '\n')
{
line++; /* increment when a newline is found*/
totChars += i-1; /* Correction for \n (newline) character*/
if (i<minChars)
{
minChars = i;
minLine = line;
}
else if (i>maxChars || minChars>maxChars)
{
maxChars = i;
maxLine = line;
}
i=0;
}
}
cout << "The input file has " << line << "\n";
cout << "The average number of characters per line is " << totChars << "\n";
cout << "The line that is the shortest and its characters is " << minLine << " " << minChars << "\n";
cout << "The line that is the longest and its characters is " << maxLine << " " << maxChars << "\n";
return 0;
}
Input.txt
Code:
aaaaaaaaaaaaaaaa
0100111101010010
01000111
0100010001000101
01000011
0100100001000101
01011000
0100010101001110
01000100
My output is:
input file has 9 lines
average number of characters per line is 112
The line that is the shortest and its characters 3 9
The line that is the longest and its characters 2 17