Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,304 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,047 people online right now. Registration is fast and FREE... Join Now!




First Post - Help w/ File Processing

 
Reply to this topicStart new topic

First Post - Help w/ File Processing

DominationXVI
post 15 Feb, 2008 - 04:29 PM
Post #1


New D.I.C Head

*
Joined: 2 Feb, 2008
Posts: 36

Hi everyone!

This program is supposed to count the number of words, characters, and lines in a text file and then output the contents of the file to the display. It seems to work correctly, except it comes up with the wrong number of words and lines. The file I am testing it with looks like this:

this is a simple
file
[indent]with

some data

There are 40 characters, including spaces, tabs, /n, and text, 5 lines, and 8 words. When I run my program, it always counts 1 line, 6 words, and 41 characters.
I just can't seem to figure out what is going on. Any help would be greatly appreciated.

CODE

#include <iostream>
#include <fstream>

using namespace std;

int algorithm(void);
int code(string);
void initialize(int& words, int& letters, int& lines);
void readBlanks(char& ch, int& wrdCount, int& chCount, ifstream& file);
void copyChar(char& ch, int& chCount, ifstream& file);
void display(int wrdCount, int chCount, int lineCount);

int main()
{

ifstream inFile;
int words, lines, letters;
char next;
int result;

initialize(words, letters, lines);

result = algorithm();
result = code("text.cpp");

inFile.open("This is the text file");

inFile.get(next);

while(!inFile.eof())
{

        while(next != '/n' && !inFile.eof())
        {
                readBlanks(next, words, letters, inFile);
                copyChar(next, letters, inFile);
        }
        cout << next;
        lines++;
        letters++;
        inFile.get(next);
}

display(words, letters, lines);

return 0;
}

void readBlanks(char& ch, int& wrdCount, int& chCount, ifstream& file)
{
        while(ch == 32 || ch == '/t')
        {
                cout << ch;
                chCount++;
                file.get(ch);
        }
        if(ch != '/n')
                wrdCount++;
        return;
}


void copyChar(char& ch, int& chCount, ifstream& file)
{
        while(ch != 32 && ch != '/t' && ch != '/n')
        {
                cout << ch;
                chCount++;
                file.get(ch);

                if(file.eof())
                        break;
        }
return;
}

void display(int wrdCount, int chCount, int lineCount)
{

cout << "There are " << chCount << " characters, " << wrdCount << " words, and " << lineCount << " lines in this file." << endl;
return;

}

void initialize(int& words, int& letters, int& lines)
{
        words = 0;
        lines = 0;
        letters = 0;
}


This post has been edited by DominationXVI: 15 Feb, 2008 - 04:32 PM
User is offlineProfile CardPM

Go to the top of the page

barnwillyb
post 15 Feb, 2008 - 04:41 PM
Post #2


D.I.C Head

**
Joined: 22 May, 2007
Posts: 55


My Contributions


QUOTE(DominationXVI @ 15 Feb, 2008 - 05:29 PM) *

Hi everyone!

This program is supposed to count the number of words, characters, and lines in a text file and then output the contents of the file to the display. It seems to work correctly, except it comes up with the wrong number of words and lines. The file I am testing it with looks like this:

this is a simple
file
[indent]with

some data

There are 40 characters, including spaces, tabs, /n, and text, 5 lines, and 8 words. When I run my program, it always counts 1 line, 6 words, and 41 characters.
I just can't seem to figure out what is going on. Any help would be greatly appreciated.

CODE

#include <iostream>
#include <fstream>

using namespace std;

int algorithm(void);
int code(string);
void initialize(int& words, int& letters, int& lines);
void readBlanks(char& ch, int& wrdCount, int& chCount, ifstream& file);
void copyChar(char& ch, int& chCount, ifstream& file);
void display(int wrdCount, int chCount, int lineCount);

int main()
{

ifstream inFile;
int words, lines, letters;
char next;
int result;

initialize(words, letters, lines);

result = algorithm();
result = code("text.cpp");

inFile.open("This is the text file");

inFile.get(next);

while(!inFile.eof())
{

        while(next != '/n' && !inFile.eof())
        {
                readBlanks(next, words, letters, inFile);
                copyChar(next, letters, inFile);
        }
        cout << next;
        lines++;
        letters++;
        inFile.get(next);
}

display(words, letters, lines);

return 0;
}

void readBlanks(char& ch, int& wrdCount, int& chCount, ifstream& file)
{
        while(ch == 32 || ch == '/t')
        {
                cout << ch;
                chCount++;
                file.get(ch);
        }
        if(ch != '/n')
                wrdCount++;
        return;
}


void copyChar(char& ch, int& chCount, ifstream& file)
{
        while(ch != 32 && ch != '/t' && ch != '/n')
        {
                cout << ch;
                chCount++;
                file.get(ch);

                if(file.eof())
                        break;
        }
return;
}

void display(int wrdCount, int chCount, int lineCount)
{

cout << "There are " << chCount << " characters, " << wrdCount << " words, and " << lineCount << " lines in this file." << endl;
return;

}

void initialize(int& words, int& letters, int& lines)
{
        words = 0;
        lines = 0;
        letters = 0;
}



ch == 32 should be ch == '32'
User is offlineProfile CardPM

Go to the top of the page

DominationXVI
post 15 Feb, 2008 - 04:49 PM
Post #3


New D.I.C Head

*
Joined: 2 Feb, 2008
Posts: 36

QUOTE(barnwillyb @ 15 Feb, 2008 - 05:41 PM) *

QUOTE(DominationXVI @ 15 Feb, 2008 - 05:29 PM) *

Hi everyone!

This program is supposed to count the number of words, characters, and lines in a text file and then output the contents of the file to the display. It seems to work correctly, except it comes up with the wrong number of words and lines. The file I am testing it with looks like this:

this is a simple
file
[indent]with

some data

There are 40 characters, including spaces, tabs, /n, and text, 5 lines, and 8 words. When I run my program, it always counts 1 line, 6 words, and 41 characters.
I just can't seem to figure out what is going on. Any help would be greatly appreciated.

CODE

#include <iostream>
#include <fstream>

using namespace std;

int algorithm(void);
int code(string);
void initialize(int& words, int& letters, int& lines);
void readBlanks(char& ch, int& wrdCount, int& chCount, ifstream& file);
void copyChar(char& ch, int& chCount, ifstream& file);
void display(int wrdCount, int chCount, int lineCount);

int main()
{

ifstream inFile;
int words, lines, letters;
char next;
int result;

initialize(words, letters, lines);

result = algorithm();
result = code("text.cpp");

inFile.open("This is the text file");

inFile.get(next);

while(!inFile.eof())
{

        while(next != '/n' && !inFile.eof())
        {
                readBlanks(next, words, letters, inFile);
                copyChar(next, letters, inFile);
        }
        cout << next;
        lines++;
        letters++;
        inFile.get(next);
}

display(words, letters, lines);

return 0;
}

void readBlanks(char& ch, int& wrdCount, int& chCount, ifstream& file)
{
        while(ch == 32 || ch == '/t')
        {
                cout << ch;
                chCount++;
                file.get(ch);
        }
        if(ch != '/n')
                wrdCount++;
        return;
}


void copyChar(char& ch, int& chCount, ifstream& file)
{
        while(ch != 32 && ch != '/t' && ch != '/n')
        {
                cout << ch;
                chCount++;
                file.get(ch);

                if(file.eof())
                        break;
        }
return;
}

void display(int wrdCount, int chCount, int lineCount)
{

cout << "There are " << chCount << " characters, " << wrdCount << " words, and " << lineCount << " lines in this file." << endl;
return;

}

void initialize(int& words, int& letters, int& lines)
{
        words = 0;
        lines = 0;
        letters = 0;
}



ch == 32 should be ch == '32'


32 is ascii value of a space, I don't want the actual character '32' (or number)
that loop only executes if ch is a space or ch is a tab.
User is offlineProfile CardPM

Go to the top of the page

letthecolorsrumble
post 15 Feb, 2008 - 04:59 PM
Post #4


Student of The Sun

Group Icon
Joined: 7 Nov, 2007
Posts: 550



Thanked 1 times
My Contributions


"\n and not /n"

"\t and not /t"
User is offlineProfile CardPM

Go to the top of the page

DominationXVI
post 15 Feb, 2008 - 05:34 PM
Post #5


New D.I.C Head

*
Joined: 2 Feb, 2008
Posts: 36

QUOTE(letthecolorsrumble @ 15 Feb, 2008 - 05:59 PM) *

"\n and not /n"

"\t and not /t"


Damn, i'm an idiot...lol

Thanks for your help, works perfectly now.

Anyway, since my code is already here, should I have done anything differently? I mean, there must be many ways to do this type of thing, many of which are
probably easier than my approach. Any tips or pointers?
User is offlineProfile CardPM

Go to the top of the page

letthecolorsrumble
post 16 Feb, 2008 - 06:29 AM
Post #6


Student of The Sun

Group Icon
Joined: 7 Nov, 2007
Posts: 550



Thanked 1 times
My Contributions


Well, the logic would have been probably nearly the same, but the one thing that I would have done differently would be the use of classes.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 02:06AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month