/************************* Homework 1 ****************************
Name : Kayanne Goerke
Date : September 15,2009
Homework# : Homework 1
Source File : hw1.cpp
Action : Reads words from a standard input, either keyboard or
from a file via DOS and outputs a table showing the
frequencies of words of various lengths.
Note : Words longer than 15 characters are treated as 15 character
word, and the array is no larger then 16.
-----------------------------------------------------------------*/
#include <iostream>
using namespace std;
#include<ctype.h>
#include<iomanip>
const int SIZE=16;
int LengthOfWord( );
void Display(int WordLength[]);
int main()
{
int length = 0;
int WordLength[SIZE] = {0};
cout << "Please enter a sentence:";
length = LengthOfWord( );
while (length != 0)
{
++ WordLength[length];
length = LengthOfWord( ); //initializes the length from the value passed from the function
if (length > 15) //if word is longer then fifteen, count as fifteen
length = 15;
}
Display(WordLength); //Displays the values of the WordLength array.
return 0;
}
/********************* Display **************************************
Action : Makes a table to display the word frequencies and lengths
Parameters :
IN : The word length array which is displayed on the screen output
OUT :
Returns : Nothing
Precondition : The WordLength array points to an int array.
---------------------------------------------------------------------*/
void Display(int WordLength[])
{
int i; //average;
cout<< " Word Length"<<setw(20)<<"Frequency"<<"\n";
cout<<"--------------"<<setw(20)<<"--------------"<<"\n";
for (i=1; i<16; i++)
{
for(i=1; i<SIZE; i++)
cout<<setw(10)<<i<<setw(15)<<" "<<WordLength[i]<<"\n";
}
//average=WordLength[i]/
//cout<<average<<"\n";
}
/****************** LengthOfWord **********************
Action : Counts the letters and number in an a word and returns
the value.
Parameters :
IN :
OUT :
Returns : The letter count of a word.
Precondition :
-------------------------------------------------------*/
int LengthOfWord( )
{
char ch;
int EndOfWord=0;
int LetterCount=0;
cin.get(ch);
while( (!EndOfWord) && (!cin.eof())) //If the flag is set or its the end of the file, then stop loop
{
while((isspace(ch)) || (ispunct(ch))) //Punctuation and whitespaces end a word
cin.get(ch);
if (isalnum(ch))
{
cin.get(ch);
++LetterCount;
}
if((ch=='-')&& isalnum(cin.peek())) //A hyphen within a word is counted as the word length
{
cin.get(ch);
LetterCount++;
}
if((ch=='-') && (cin.peek()=='\n')) //A hyphen at the end of a word with a new line ends word
{
cin.get(ch);
}
if ((ch=='\'') && isalnum(cin.peek())) //An apostrophe counts as Length as long as its in word
{
cin.get(ch);
LetterCount++;
}
if (isspace(ch) || ispunct(ch) || cin.eof()) //A white space, punctuation, or end of file at the end
EndOfWord=1; //trips the flag to end the function
}
return LetterCount;
}
based on the code, I was given five files, and out of the five, I have four working. The fifth one is counting everything right, but it is not displaying the information. Through the debugger, it counts everything but does not return zero to stop the while loop so I think I am getting an infinite loop, but it works with any other program that I put through it
the one i have been trying to count is this:
hy-
pen
!!wishy-washy!!
after wishy-washy it just stops.
help anyone?? it can't be much more advanced then what i have up there. thanks!
This post has been edited by ninreznorgirl2: 19 September 2009 - 06:12 PM

New Topic/Question
Reply




MultiQuote





|