C++ console application that analyzes the contents of a text file.

Help ensuring my code functions properly.

Page 1 of 1

13 Replies - 2013 Views - Last Post: 26 November 2010 - 01:25 PM Rate Topic: -----

#1 oroberi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 18-October 09

C++ console application that analyzes the contents of a text file.

Posted 16 April 2010 - 10:12 PM

This is what my assignment asks, but I do not think that I am getting the code quite right.

Create a C++ console application that analyzes the contents of a text file.

Your program should count the number of:

* lines (hint: use getline)
* words (hint: count the spaces)
* letters (hint: remember character arrays?)
* punctuation marks (period, comma, and semicolons).

Displays that data in a table.

This is what I have as far as my code.


// Joseph Eggemeyer wk7 EX1.cpp : main project file.

//Specification:

//Joseph Eggemeyer  
//Lab 7 Exercise 1
//This program creates a program that can analyze 
//the contents of a text file, count the number of 
//lines, words, letters, and punction marks, and 
//finally displays this data in a table.

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void TextFile();
void TextCount();
int CountLines(string lines);
int CountWords(string spaces);
int CountLetters(string letters);
int CountMarks(string punctMarks);


int main()
{
TextFile();
TextCount();

return 0;
}
void TextFile()
{
string lineBuffer;
ofstream outFileObj;
outFileObj.open("c:/textFile.txt");
cout << "Enter text and type 'quit' to end file\n";
getline(cin, lineBuffer);
while(lineBuffer != "quit")
{
outFileObj << lineBuffer << endl;
getline(cin, lineBuffer);
}
outFileObj.close();
}
void TextCount()
{
string lines;
string words;
string letters;
string punctMarks;

int count = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;

ifstream inFileObj("c:/textFile.txt");
if(inFileObj.is_open())
{
getline(inFileObj, lines);
getline(inFileObj, words);
getline(inFileObj, letters);
getline(inFileObj, punctMarks);

while(!inFileObj.eof())
{
cout << lines << endl;
count = count + CountLines(lines);
getline(inFileObj, lines);

cout << words << endl;
count1 = count1 + CountWords(words);
getline(inFileObj, words);

cout << letters << endl;
count2 = count2 + CountLetters(letters);
getline(inFileObj, letters);

cout << punctMarks << endl;
count3 = count3 + CountMarks(punctMarks);
getline(inFileObj, punctMarks);
}
cout << "Number of lines: " << count << endl;
cout << "Number of words: " << count1 << endl;
cout << "Number of letters: " << count2 << endl;
cout << "Number of punctuation marks: " << count3 << endl;

inFileObj.close();
}
else
cout << "File Error: Open Failed\n";
}

int CountLines(string lines)
{
int count = 0;

for(int i = 0; lines[i] != '\0'; ++i)
{
if(lines[i] >= 6)
++count;
}
return count;
}

int CountWords(string words)
{
int count1 = 0;

for(int i = 0; words[i] != '\0'; ++i)
{
if(words[i] >= 15)
++count1;
}
return count1;
}

int CountLetters(string letters)
{
int count2 = 0;

for(int i = 0; letters[i] != '\0'; ++i)
{
if(letters[i] >= 73)
++count2;
}
return count2;
}

int CountMarks(string punctMarks)
{
int count3 = 0;

for(int i = 0; punctMarks[i] != '\0'; ++i)
{
if(punctMarks[i] <= 3)
++count3;
}
return count3;
} 




I am also having an issue with getting it to open the file...

Is This A Good Question/Topic? 0
  • +

Replies To: C++ console application that analyzes the contents of a text file.

#2 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: C++ console application that analyzes the contents of a text file.

Posted 16 April 2010 - 10:51 PM

This example, using C++ stringstream, may give you a jump start ...

http://developers-he...index.php/topic,134.msg357.html#msg357
Was This Post Helpful? 1
  • +
  • -

#3 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: C++ console application that analyzes the contents of a text file.

Posted 17 April 2010 - 04:45 AM

You may need to run your prog in administrator mode to get it to open the file, or put the file in a different location BUT ONLY IF USING Windows Vista or W7 !

k, if you can do it without making multiple passes through the file, it would be a good thing (i think).

Oh, and last time I checked a C++ string doesn't end with a NULL byte (the \0)

This post has been edited by taylorc8: 17 April 2010 - 04:46 AM

Was This Post Helpful? 0
  • +
  • -

#4 oroberi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 18-October 09

Re: C++ console application that analyzes the contents of a text file.

Posted 18 April 2010 - 10:38 AM

View Posttaylorc8, on 17 April 2010 - 03:45 AM, said:

You may need to run your prog in administrator mode to get it to open the file, or put the file in a different location BUT ONLY IF USING Windows Vista or W7 !

k, if you can do it without making multiple passes through the file, it would be a good thing (i think).

Oh, and last time I checked a C++ string doesn't end with a NULL byte (the \0)


Ok, I think I have it working for the most part, but time is getting short. It does everything I need it to do, I just need to set it up so it puts the final data into a table. Sorry, just stressed, I am trying to finish this, and two others, and prepare for my final as well.

Can anyone give a quick way to put the final data into a table?

Thanks for any help or suggestions.


// Joseph Eggemeyer wk7 EX1.cpp : main project file.

//Specification:

//Joseph Eggemeyer  
//Lab 7 Exercise 1
//This program creates a program that can analyze 
//the contents of a text file, count the number of 
//lines, words, letters, and punction marks, and 
//finally displays this data in a table.

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void TextFile();
void TextCount();
int CountLines(string lines);
int CountWords(string spaces);
int CountLetters(string letters);
int CountMarks(string punctMarks);


int main()
{
TextFile();
TextCount();

return 0;
}
void TextFile()
{
string lineBuffer;
ofstream outFileObj;
outFileObj.open("c:/TestCount.txt");
cout << "Enter 'read' and enter to read file\n";
getline(cin, lineBuffer);
while(lineBuffer != "read")
{
outFileObj << lineBuffer << endl;
getline(cin, lineBuffer);
}
outFileObj.close();
}
void TextCount()
{
string lines;
string words;
string letters;
string punctMarks;

int count = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;

ifstream inFileObj("c:/TestCount.txt");
if(inFileObj.is_open())
{
getline(inFileObj, lines);
getline(inFileObj, words);
getline(inFileObj, letters);
getline(inFileObj, punctMarks);

while(!inFileObj.eof())
{
cout << lines << endl;
count = count + CountLines(lines);
getline(inFileObj, lines);

cout << words << endl;
count1 = count1 + CountWords(words);
getline(inFileObj, words);

cout << letters << endl;
count2 = count2 + CountLetters(letters);
getline(inFileObj, letters);

cout << punctMarks << endl;
count3 = count3 + CountMarks(punctMarks);
getline(inFileObj, punctMarks);
}
cout << "Number of lines: " << count << endl;
cout << "Number of words: " << count1 << endl;
cout << "Number of letters: " << count2 << endl;
cout << "Number of punctuation marks: " << count3 << endl;

inFileObj.close();
}
else
cout << "File Error: Open Failed\n";
}

int CountLines(string lines)
{
int count = 0;

for(int i = 0; lines[i] != '\0'; ++i)
{
if(lines[i] >= 6)
++count;
}
return count;
}

int CountWords(string words)
{
int count1 = 0;

for(int i = 0; words[i] != '\0'; ++i)
{
if(words[i] >= 15)
++count1;
}
return count1;
}

int CountLetters(string letters)
{
int count2 = 0;

for(int i = 0; letters[i] != '\0'; ++i)
{
if(letters[i] >= 73)
++count2;
}
return count2;
}

int CountMarks(string punctMarks)
{
int count3 = 0;

for(int i = 0; punctMarks[i] != '\0'; ++i)
{
if(punctMarks[i] <= 3)
++count3;
}
return count3;
} 



Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: C++ console application that analyzes the contents of a text file.

Posted 18 April 2010 - 10:44 AM

Google setw and setfill.
Was This Post Helpful? 0
  • +
  • -

#6 oroberi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 18-October 09

Re: C++ console application that analyzes the contents of a text file.

Posted 18 April 2010 - 12:32 PM

Actually, scratch that, it is not counting the punctuation properly, any idea on that, I fixed my other issue already.

Thanks.
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: C++ console application that analyzes the contents of a text file.

Posted 18 April 2010 - 12:34 PM

if(punctMarks[i] <= 3)
    ++count3;

explain what you think that code does?
Was This Post Helpful? 0
  • +
  • -

#8 oroberi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 18-October 09

Re: C++ console application that analyzes the contents of a text file.

Posted 18 April 2010 - 12:52 PM

I know that this is where my issue is, thats intended to count punctuation marks, and add it to the counter, but it is not working. The best I can assume, is that my string is not working correctly?
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: C++ console application that analyzes the contents of a text file.

Posted 18 April 2010 - 01:09 PM

http://www.cpprefere.../string/ispunct
Was This Post Helpful? 0
  • +
  • -

#10 oroberi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 18-October 09

Re: C++ console application that analyzes the contents of a text file.

Posted 18 April 2010 - 01:43 PM

cctype is not covered anywhere in my texts, so I am not sure if that is an acceptable usage for my class. Is there any other way to solve the issue using what I currently have?

Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#11 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: C++ console application that analyzes the contents of a text file.

Posted 18 April 2010 - 01:56 PM

Quote

Is there any other way to solve the issue using what I currently have?
Reimplement functionality as needed. Pointless from an engineering standpoint, given the existence of the standard library, but if you have to reimplement it, then you do.
Was This Post Helpful? 0
  • +
  • -

#12 Guest_Austin*


Reputation:

Re: C++ console application that analyzes the contents of a text file.

Posted 26 November 2010 - 12:20 PM

your directory would be "c:\filename" not "c:/filename"
Was This Post Helpful? 0

#13 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1335
  • View blog
  • Posts: 4,575
  • Joined: 19-February 09

Re: C++ console application that analyzes the contents of a text file.

Posted 26 November 2010 - 01:07 PM

You read the first line, of the text file, into string line,
then the second line into words,
the third line into letters etc.

  getline(inFileObj, lines);
  getline(inFileObj, words);
  getline(inFileObj, letters);
  getline(inFileObj, punctMarks);



So you don't count the words, letters or punctuation marks in the first line or the majority of subsequent lines.
Was This Post Helpful? 0
  • +
  • -

#14 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: C++ console application that analyzes the contents of a text file.

Posted 26 November 2010 - 01:25 PM

ARGH, ZOMBIE THREAD!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1