6 Replies - 3684 Views - Last Post: 20 April 2010 - 04:35 PM Rate Topic: -----

#1 Gonzoyola  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-April 10

VS2008 C++ (Reading and Counting Elements in a Text File)

Posted 20 April 2010 - 02:05 PM

Hello Everyone,

I've been in a CIS170c class for the past 8 weeks. Unfortunately staying caught up has been difficult with my accelerated learning environment and teaching methods that have been unorthodox compared to the rest of my learning experience at my school. I have been employing a number of archived topics here to help me try and understand the things that I haven't been able to learn from my instructor and it is only with regret now that I haven't introduced myself to the community sooner. Hopefully I can stay an active, and maybe even eventually a contributing member to these boards as I stay determined to learn how to break into programming.

Before I post my particular question let me say that one should assume I have very very basic knowledge of how anything works so the more in depth the explanation the better it will help me. I tried leafing through a tutorial here and there but found it rather frustrating so I am taking up learning what I can from here.


I am dealing with an exercise that seems to be pretty familiar with a number of people wherein you create a program that can read into a file and output the number of lines, words, letters, and punctuation to the screen. I'm actively looking into it myself, but it already seems apparent that I seem to be having problems opening the file I'm trying to read from.



(I hope this comes out right)


#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
	ifstream inFile("C:\\Users\Kyle\Desktop\file.txt"); // I also tried using just ifstream inFile("file.txt") earlier as I believ that I saw an example where that was applicable, but after implementing my if statement to test the file I tried a more detailed location

	  string line = " ";
	  int lineCount = 0;
	  int wordCount = 0;
	  int letterCount = 0;
	  int punctuation = 0;

	  if (!inFile.is_open()){
		  cout << "The file did not open.";
	  }

	  while (!inFile.eof() ) {
		  getline (inFile, line);
		  lineCount++;
	  }

	  cout << "Lines: " << lineCount;
       return 0;
}



I plan on working my way through the rest of what I have there while I browse around for any other helpful information. I will probably plan on using this thread to work my way through these File I/O exercises and then be saving them for my reference. If I'm lucky maybe I can jumble together a simpler tutorial on the subject.

Is This A Good Question/Topic? 0
  • +

Replies To: VS2008 C++ (Reading and Counting Elements in a Text File)

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: VS2008 C++ (Reading and Counting Elements in a Text File)

Posted 20 April 2010 - 02:11 PM

So...did you have a question? Sounds like you were just introducing yourself (Hello!)
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,438
  • Joined: 23-August 08

Re: VS2008 C++ (Reading and Counting Elements in a Text File)

Posted 20 April 2010 - 02:12 PM

Read this tutorial for starters
Was This Post Helpful? 0
  • +
  • -

#4 Gonzoyola  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-April 10

Re: VS2008 C++ (Reading and Counting Elements in a Text File)

Posted 20 April 2010 - 03:16 PM

Hello Oler,
Yes it was primarily an introduction but the question was buried in there, haha. basically where my file was at right there, the problem seemed to be that I was opening the file wrong, which now I have fixed that, and thanks to the tutorial, I see the folly of testing with EOF. I have had a lot of people that I discuss C++ with, and seen a lot of posts on here, discredit a lot of the methods that my classes syllabus has tried to teach us.

Here is an updated version of what I have now, I will be looking into counting words and letters now, which hopefully will be much simpler, but any help is appreciated none the less.

/* Edit - Much thanks for directing me to that very helpful tutorial */
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
	ifstream inFile("C:\\Users\\Kyle\\My Documents\\File.txt");

	  string line = " ";
	  int lineCount = 0;
	  int wordCount = 0;
	  int letterCount = 0;
	  int punctuation = 0;


	  while (inFile >> line) {
		 getline (inFile, line);
		 lineCount++;
	  }
	  while (inFile >> line)/* This is where I am going to start working on counting words, although I am suspicious that a nested loop is
 what I should be going for here. Any help is appreciated. */

	  cout << "Lines: " << lineCount << endl;
       return 0;
}	

This post has been edited by Gonzoyola: 20 April 2010 - 03:17 PM

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,438
  • Joined: 23-August 08

Re: VS2008 C++ (Reading and Counting Elements in a Text File)

Posted 20 April 2010 - 03:20 PM

You've retrieved your line into a variable, right? So now you process that variable before you go read another line.
Was This Post Helpful? 0
  • +
  • -

#6 Gonzoyola  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-April 10

Re: VS2008 C++ (Reading and Counting Elements in a Text File)

Posted 20 April 2010 - 03:47 PM

Thank you for the immediate responses Jack, although I have a shaky grip on your terminology.

Let me explain to you my understanding of what I have done, and maybe you can think of somewhere to direct me to put me on the right path.

I have created a simple text file which right now currently says:

This
File
Has
Five
Lines

My first and only problem so far was the difficulty getting the file to open, now I have gotten it to open and it successfully displays that I have 5 lines. I am currently going through the video tutorial available to my class but it doesn't seem to contain anything in particular which will help me. What I have picked up is that the words are counted by counting the spaces which I am looking into right now. I believe I will have a go at browsing the tutorials section on my own again but I rarely ever seem to find the ones most relevant to my topic on my own.
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,438
  • Joined: 23-August 08

Re: VS2008 C++ (Reading and Counting Elements in a Text File)

Posted 20 April 2010 - 04:35 PM

You are on the right track...your file should have more than one word per line I would think.

A text file is made up of lines. You need to count those. Those lines are made up of characters. You need to look at each character and count each differently, depending on what it is. Write a sentence on paper, and use pen and paper to do this to get the feel of what you're going to do in code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1