# Infinite loop problem

Page 1 of 1

## 4 Replies - 1630 Views - Last Post: 19 September 2009 - 08:34 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=126840&amp;s=4958e1fdab498851a2f8bd5fb48656d4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ninreznorgirl2

Reputation: 0
• Posts: 57
• Joined: 26-April 09

# Infinite loop problem

Posted 19 September 2009 - 06:02 PM

``` /************************* 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

Is This A Good Question/Topic? 0

## Replies To: Infinite loop problem

### #2 poncho4all

Reputation: 123
• Posts: 1,422
• Joined: 15-July 09

## Re: Infinite loop problem

Posted 19 September 2009 - 06:27 PM

Look at your while, closely do you think it is posible that lenght is ever 0?

### #3 ninreznorgirl2

Reputation: 0
• Posts: 57
• Joined: 26-April 09

## Re: Infinite loop problem

Posted 19 September 2009 - 06:32 PM

i was wondering that myself, but how come it would work for all the other ones, and not this one. Thats what i don't get why its just this one that is doing the infinite loop.

### #4 poncho4all

Reputation: 123
• Posts: 1,422
• Joined: 15-July 09

## Re: Infinite loop problem

Posted 19 September 2009 - 06:59 PM

Which other ones?

### #5 eker676

• Software Engineer

Reputation: 379
• Posts: 1,833
• Joined: 18-April 09

## Re: Infinite loop problem

Posted 19 September 2009 - 08:34 PM

I don't see any input statement before trying to calculate the length of the word.

I suggest adding something to get a string.

```string text = "";
cout << "Enter some text: ";
getline(cin, text);

```