|
-
May 5th, 2009, 04:22 PM
#1
Counting number of words + occurrences
Hello,
First off, I'm using borland c++ builder 4 (I know, my school's outdated)
I need to count the number of words in a text file (which contains the US Constitution, and it's amendments). I need to output the total number of: words in the entire text file, words in preamble, words in each article (out put separately), and words in each Amendment (output separately). I also need to count the number of occurrences of a word that the user defines and output that (the first part is more important at this time).
I don't have much done, but what I do have is code to count it once in it's entirety and output that.
Code:
#pragma hdrstop
#include <condefs.h>
#include <conio.h>
#include <iostream.h>
//---------------------------------------------------------------------------
#pragma argsused
#define NULL 0
FILE *fpt;
void main()
{
char name[20],c;
int newv = 0;
clrscr();
printf("Enter the name of file to be checked:- ");
gets(name);
fpt=fopen(name,"r");
if (fpt==NULL)
{
printf("ERROR - can/'t open file %s",name);
getch();
exit(0);
}
else
{
while ((c=getc(fpt))!=EOF)
{
switch(1)
{
case 1:
if (c==' ')
{
point: // do
// new = new+1-1;
while((c=getc(fpt))==' ');
if (c!=' ')
newv = newv +1;
if(c==' ')newv--;
}
// case 3:
if(c==' '){
goto point;}
}
}
}
cout << "Document: " << name << " contains: "<< newv << " words";
getch();
}
I will be awaiting your assistance, thanks.
rockage
-
May 5th, 2009, 04:28 PM
#2
Re: Counting number of words + occurrences
You've got a goto in that program. Not cool.
This task would be much easier in C++ than C. Easier to do safely, anyway. For your basic assume-no-stupendously-huge-words implementation, C isn't all that much harder. You should be using fscanf() to pull each word in turn, though.
-
May 5th, 2009, 04:40 PM
#3
Re: Counting number of words + occurrences
Thanks for the post; I am just starting out in c++ taking a class and this is the assignment, no offense to my teacher but he kind of just threw us out there and I have no idea what to do other than what I have.
-
May 5th, 2009, 06:25 PM
#4
Re: Counting number of words + occurrences
First of all, you are completely misusing switch-case.
Example of how it should be used:
Code:
if(someVariable==1)
{
//do Something #1
}
else if(someVariable==2)
{
//do Something #2
}
else if(someVariable==3)
{
//do Something #3
}
else
{
//do something different
}
Is equivilent to:
Code:
switch(someVariable)
{
case 1: //do the code below when someVariable==1
//do Something #1
break;
case 2: //do the code below when someVariable==2
//do Something #2
break;
case 3: //do the code below when someVariable==3
//do Something #3
break;
default: //do the code below when someVariable any other value
//do something different
}
Second, while goto's are supported in C/C++ and are closer to how machine code works, they are generally considered poor coding form, as they get really confusing if they span longer than one page on the screen. This is nicknamed "spagetti code".
-
May 5th, 2009, 08:44 PM
#5
Re: Counting number of words + occurrences
Thanks for your advice and psuedocode, but I still don't know what to do for this program.
-
May 5th, 2009, 08:57 PM
#6
Re: Counting number of words + occurrences
Simplest approach to counting words: You have a function to read words from a file (character chunks separated by whitespace, anyway). Two actually; this is both cin's default behavior, and that of fscanf with the %s format specifier. All you have to do is count how many times you're able to do this before you run out of file.
Getting the counts for each individual subsection requires identifying when each section starts. Hopefully the teacher can give you some guidance on how to do that---it's nontrivial, but hopefully not too hard.
Checking against a user-defined "watchword" is easy, just compare every word you see as you see it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|