Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Reading File in C++


  • Please log in to reply

#1
Kevin39

Kevin39

    Member

  • Member
  • PipPip
  • 18 posts
I have created a program for my college course in C++ in reading files. The requirement is to open a file and read the data and display the following information from the file. Number of characters per line, length of the longest and shortest lines and the average number of characters per line.

I have the number of characters per line, longest line, and the average characters per line. Any ideas how i can determine the shortest line in the file?

Here is the code so far.

/*This program reads a text file and calculates the charaters
* per line, length of shortest and longest lines, and average
* number of charaters per line.
*
*Input: Text file to read
*Output: Number of characters per line, Average, and longest and
* shortest lines.
*****************************************************************/

#include <iostream> //cin, cout, <, >
#include <fstream> //ifstream, ofstream
#include <string> //string, getline()

using namespace std;

int main()
{
string Characters;

cout << "This program reads a text file and calculates the\n"
"total count of characters per line, length of the longest\n"
"and shortest lines and Average characters per line.\n\n";

cout << "Open the Lines.txt file: \n";

ifstream inStream;

inStream.open("Lines.txt"); //Opens the Text file.
inStream.is_open(); //verfies the file is open

if (inStream.fail()) //If opening the file fails
cerr << "Error! Couldn't open " << "Lines.txt" << " file for reading ";

int Character = 0;
int totalCharacters = 0;
int lineNumber = 1;
int totallineNumber = 0;
double Average, longest, shortest;


for (;:tazz: //Loop to read the text file data.
{
getline(inStream, Characters);
Character = Characters.size() -1; //Convert characters to number of characters
cout << "\n--> There are " << Character << " characters in line number "<< lineNumber <<".\n";

totalCharacters += Character;
if (Character > 0)
longest = Character;

lineNumber++;

if (inStream.eof()) break;

}

totallineNumber = lineNumber -1;
Average = totalCharacters / totallineNumber;

cout << "\n--> Total Lines of data is "<< totallineNumber <<".\n";
cout << "\n--> Total number of characters is "<< totalCharacters <<".\n";
cout << "\n--> The longest line has "<< longest <<" characters in it.\n";
cout << "\n--> The shortest line has "<< shortest <<" characters in it.\n";
cout << "\n--> "<< Average << " is The Average number of Characters per line.\n";
cout <<endl;
inStream.close();

}
  • 0

Advertisements


#2
ricci

ricci

    Member

  • Member
  • PipPip
  • 64 posts
Hi Kevin,

Your code in its current form is not necessarily going to correctly calculate the length of the longest line. What it will do is calculate the length of the last line of length greater than zero. This is not necessarily the longest line.

In order to calculate the length of the longest line, you should only store the length if it is longer than the length you had before. Start out with zero (like you are), and every time you find a line longer than what you have stored, store the new length instead.

Calculating the length of the shortest line is similiar. You should only store the length of a line if it is shorter than the length you had before. Start out by putting the largest number possible in your shortest variable. The largest number possible for an int is stored in a variable in Limits.h called INT_MAX. Then, every time you find a line shorter than what you have stored, store the new length instead.

Good Luck,
Ricci
  • 0

#3
Kevin39

Kevin39

    Member

  • Topic Starter
  • Member
  • PipPip
  • 18 posts
Thank you that did it. it works.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP