I'm trying to create a program but have no idea how to start. I do have one part of the code but not the rest.
These are the guidelines:
Write a C++ program that tabulates the characters in a text file. The input consists of a series of words and sentences delimited by white space. The program need not display the file as it is being read. The tallies must include the following:
1) The number of characters in the file. This is a count of every character that is returned by the "get" function (see note 2 below)
2) The number of words included in the file. A word is defined as a sequence of characters delimited by the white space. At least one of the characters must be alphabetic (lower and upper case are equivalent) to be counted as a word.
3) The number of sentences in the file. A sentence is defined as the number of occurences of the period character in the file which is the followed by at least two blanks, a newline character, or one blank and a newline character.
4) The number of lines included in the file. A line is counted when a newline character is detected in the file.
5) The line containing the most characters. The tally must include noth the relative line number in the file and the number of characters contained on that line. A character includes anything except a newline.
After the entire input file has been proccessed, the tallies are to be printed out with the appropriate headings. The number of characters, words, and the sentences should be declared as a long integer.
The code i have for counting word is:
int countwords();
int countwords()
{
int words=0;
int c;
bool havealpha=false;
while((c=in.get())!=EOF)
{
if(isalpha(c))
havealpha=true;
My question, if someone could help me out with your interpretation of this project. I do best when i can actually see examples of the syntax. I would really appreciate the help. You can use "int countwords()" or "void countwords()"
First a question, is the code right in my post. And for the problem I done know exactly how to write out the syntax for 1, 3-5. Wanted an example to work from.
I think you should use void if you are not returning any thing from
your function countwords(). Also because you are not returning any thing you wont be able to use:
1 2 3
int words=0;
int c;
bool havealpha=false;
anywhere outside countwords() function or else you would have to
declare them outside your funcion.
Something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
void countwords(int& w, int& ch, bool& ha){
while(ch*=in.get()!=EOF){
if(isalpha(ch*))ha*=true;
if(isspac(ch*)&&ha*)w*=w*+1;
ha*=false;
}}
int main (){
int words=0;
int c;
bool havealpha=false;
countwords(words , c , havealpha );
return 0;}
Your logic isn't quite right because you set havealpha=false after each character. You want to read the non-space characters and remember if you've seen an alpha. When you hit a space character, use havealpha to see if you have a word. Then reset havealpha:
1 2 3 4
if (isspac(c) {
if (havealpha) ++words;
havealpha = false;
}
It looks like you intend to write a separate function for each of the 5 things to tabulate. This will certainly be the easiest way to code it, but it will be inefficient because you read the file 5 times. A much more efficient way would be to read a character and then increment all of the appropriate counters depending on that character. This will be harder code to write but I thought I'd point it out.