problem in function
Feb 5, 2014 at 4:34am UTC
this program is designed to get input in the buffer via DOS input redirection.
the problem i'm having is that when the function WordLength is called it becomes unresponsive and does not get to the second function. i'm presuming it is falling into an infinite loop somewhere, but I cant find where. can someone please point out what i'm doing wrong?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
#include <iostream>
#include <iomanip>
using namespace std;
void Display (int Count[]);
void WordLength (int Count[]);
float AverageLength (int Count[]);
const int SIZE=16;
int main()
{
int Count[16]={0};
WordLength (Count);
Display (Count);
return 0;
}
void Display (int *Count)
{
cout<<"Word Length Frequency" <<endl;
cout<<"----------- ---------" <<endl;
cout<<" 1 " <<Count[1]<<endl;
cout<<" 2 " <<Count[2]<<endl;
cout<<" 3 " <<Count[3]<<endl;
cout<<" 4 " <<Count[4]<<endl;
cout<<" 5 " <<Count[5]<<endl;
cout<<" 6 " <<Count[6]<<endl;
cout<<" 7 " <<Count[7]<<endl;
cout<<" 8 " <<Count[8]<<endl;
cout<<" 9 " <<Count[9]<<endl;
cout<<" 10 " <<Count[10]<<endl;
cout<<" 11 " <<Count[11]<<endl;
cout<<" 12 " <<Count[12]<<endl;
cout<<" 13 " <<Count[13]<<endl;
cout<<" 14 " <<Count[14]<<endl;
cout<<" 15 " <<Count[15]<<endl<<endl<<endl;
cout<<"Average word length: " <<AverageLength(Count);
}
float AverageLength(int Count[])
{
float Temp=0, Temp2=0, Temp3=0, Ans;
int i;
for (i=1; Count[i]<SIZE; ++i)
{
Temp=i*Count[i];
Temp2+=Temp;
}
for (i=1; Count[i]<SIZE; ++i)
Temp3+=Count[i];
Ans= Temp2/Temp3;
return Ans;
}
void WordLength (int Count[])
{
char Letter;
int Length;
cin.get(Letter);
while (!cin.eof())
{
Length=0;
while (isspace(Letter))
cin.get(Letter);
while (Letter!='\n' )
{
if (Letter=='-' )
{
cin.get(Letter);
if (Letter==0)
cin.get(Letter);
}
else if (Letter=='\n' )
{
if (Length>15)
++Count[15];
else
++Count[Length];
return ;
}
else
{
++Length;
if (ispunct(Letter))
--Length;
cin.get(Letter);
}
}
if (Length>15)
++Count[15];
else
++Count[Length];
}
}
Topic archived. No new replies allowed.