txt file statistics help

Ask for help with your homework/assignments in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

txt file statistics help

Postby BroncoAG » Wed Jul 25, 2012 12:58 pm

I have to write a code that will report all of the info for a txt like output the entire file contents and then the number of uppercase characters, lowercase characters, punction marks, alphabetic characters, white spaces and then if it couldn't be open print an error message, and then exit. I have to include the following loop to inspect each character:

Code: Select all
for (char c = file.get(); c != EOF; c = file.get())
{
     //The variable "c" now contains the next character from the file
}


Thanks for all the help I have been getting. I am taking this class online through my local college and we watch a recorded video online and then do the homework which doesn't have anything to do with the video we watched. It takes 2-3 days to get a response from the teacher. I'm almost done so I wont be taking a programming class online again. Any ways here is the code that I have so far. The problem that I am having is that I do not know how to make it to allow the user to input any .txt file and I also do not know how output the entire text file or to count any of the uppercase, lowercase, punction or white spaces. I only have the character count.

Here is my code so far

Code: Select all
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
using namespace std;

int main()
{
    fstream data_store;
    char line[100];
    int i;
    int lower_count = 0;
    int upper_count = 0;
    int alpha_count = 0;
    int space_count = 0;
    int punct_count = 0;
    data_store.open("hello.txt", ios::in);

    while (!data_store.eof())
    {
        data_store.getline(line, 100);
        for (int x = 0; x < strlen(line); x++)
        {
            char c = line[x];
            if (isalpha(c))
            {
                alpha_count++;
            }
            else if (ispunct(c))
            {
                punct_count++;
            }
            else if (isspace(c))
            {
                space_count++;
            }
            if ((line[i] >= 'a' && line[i] <= 'z'))
            {
                lower_count++;
                i++;
            }
            else if ((line[i] >= 'A' && line[i] <= 'Z'))
            {
                upper_count++;
                i++;
            }

        }

    }
    data_store.close();

    cout << "-- File contents --" << endl;

    cout << "-- End --" << endl;
    cout << "File statistics:" << endl;
    cout << "Uppercase characters: " << upper_count << endl;
    cout << "Lowercase characters: " << lower_count << endl;
    cout << "Punctuation characters: " << punct_count << endl;
    cout << "Alphabetic characters: " << alpha_count << endl;
    cout << "Whitespace characters: " << space_count << endl;
}
BroncoAG
 
Posts: 18
Joined: Wed Jul 18, 2012 8:43 pm

Re: txt file statistics help

Postby exomo » Thu Jul 26, 2012 3:53 am

The counting of whitespaces and punctuation works in your code. Just the counting of upper/lowercase letters is a bit strange (i is not even initialized, but you don't need a second counting variable anyway). You can count these just like the others, using isupper() and islower().

If you want to have the whole text in a variable just define a string and push_back() each character or each line. If you don't want a variable you could just print whatever you read.

To check if the file can be opened you can use something like this:
Code: Select all
data_store.open("hello.txt", ios::in);
        if(!data_store)
        {
            cout << "can't open file" << endl;
            return 1;
        }


How do you want the user to input the textfile? The easiest way is to just ask for a filename, read it into a string and pass this string to open(). (when using string objects remember to use c_str() on the string when passing to open())
Who needs a signature anyway.
User avatar
exomo
 
Posts: 881
Joined: Fri Sep 26, 2003 12:30 pm
Location: germany->baden

Re: txt file statistics help

Postby BroncoAG » Thu Jul 26, 2012 7:19 am

I have made some changes to the code since I had originally posted. I am still having trouble. Everything seems to work except for I still can't output the contents of the txt file. Here is what I have so far.

Code: Select all
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
using namespace std;

int main()
{
    fstream file;
    int lower_count = 0;
    int upper_count = 0;
    int alpha_count = 0;
    int space_count = 0;
    int punct_count = 0;
    string filename;


    cout << "Enter a file name" << endl;
    cin >> filename;

    file.open(filename.c_str(), ios::in);

    if(!file)
    {
        cout << "Couldn't open " << filename << endl;
        return 1;
    }
    else
    {
        for (char c = file.get(); c !=EOF; c = file.get())
        {
            if (isalpha(c))
            {
                alpha_count++;
            }
            if (ispunct(c))
            {
                punct_count++;
            }
            if (isspace(c))
            {
                space_count++;
            }
            if (islower(c))
            {
                lower_count++;
            }
            if (isupper(c))
            {
                upper_count++;
            }
        }

    }
    file.close();

    cout << "-- File contents --" << endl;
    cout << filename << endl;
    cout << "-- End --" << endl;
    cout << "File statistics:" << endl;
    cout << "Uppercase characters: " << upper_count << endl;
    cout << "Lowercase characters: " << lower_count << endl;
    cout << "Punctuation characters: " << punct_count << endl;
    cout << "Alphabetic characters: " << alpha_count << endl;
    cout << "Whitespace characters: " << space_count << endl;
}
BroncoAG
 
Posts: 18
Joined: Wed Jul 18, 2012 8:43 pm

Re: txt file statistics help

Postby exomo » Thu Jul 26, 2012 7:52 am

Your combination of if and else is only correct if a char is either a letter (isalpha) or uppercase (isupper), but not both. If isalpha() returns true none of the following conditions is checked. It should work if you remove the elses.
To output the content of the file use a new string and push each c, like
Code: Select all
string filecontent = "";

for(char c ...)
{
    filecontent.push_back(c);
   ...
Who needs a signature anyway.
User avatar
exomo
 
Posts: 881
Joined: Fri Sep 26, 2003 12:30 pm
Location: germany->baden

Re: txt file statistics help

Postby BroncoAG » Thu Jul 26, 2012 8:18 am

I got it all figured out. Thanks for your help.
BroncoAG
 
Posts: 18
Joined: Wed Jul 18, 2012 8:43 pm


Return to Homeworks

Who is online

Users browsing this forum: No registered users and 1 guest