角色计数程序无限循环
Character counting program infinite loop
我似乎正在陷入我正在为课堂上处理的角色计数程序上的无限循环。请看一下,让我知道我需要改进的地方。我已经在这件事上工作了一段时间,并继续遇到相同的情况。谢谢。
#include <iostream>
#include <ctype.h>
using namespace std;
int WordLength();
void DisplayCount(int wordCount[]);
int main()
{
int L;
int Num_of_Char[16]={0};
cout<<"Please enter some text:n";
L=WordLength();
while (L)
{
L=WordLength();
Num_of_Char[L]+=1;
}
DisplayCount(Num_of_Char);
}
/***************************************WordLength*******************************************
* Action: Analyzes the text that has been entered and decides what is and isn't *
* a word (mainly by separating words by whitespaces and not accepting *
* most punctuation as part of a word with the exception of hyphens which *
* carry a partial word to the next line as well as apostrophes. *
* *
* Parameters: *
* IN: *
* *
* OUT: *
* *
* *
* Returns: The length of each word. *
* *
* Precondition: *
*********************************************************************************************/
int WordLength()
{
char ch;
int End_Of_Word=0, Length=0;
ch=cin.get();
while((!cin.eof())||(!End_Of_Word))
{
while (!isspace(ch)) //Spaces are delimeters of words
{
if(isalnum(ch)) //if current character is a alpha numeric character it is counted as part of the word
{
++Length;
cin.get(ch);
}
if ((ch==''')&&(isalnum(cin.peek()))) //apostrophes are counted as part of the word
{
++Length;
}
if ((ch=='-')&&(isalnum(cin.peek()=='n'))) //the hyphen as part of the word if followed by newline
{
++Length;
}
if((isspace(ch))||(ispunct(ch))||(ch=='n'))
{
++End_Of_Word;
}
}
}
return Length;
}
/***************************************DisplayCount*****************************************
* Action: Displays how many words have a specific character count between 1 and *
* 15 characters. Then displays the average word character size. *
* *
* Parameters: *
* IN: wordArray, which points to the array that holds the count of each word's*
* character size. *
* *
* OUT: Displays the array contents in a grid style as well as an average *
* word size based on the contents of the array. *
* *
* Returns: *
* *
* Precondition: wordArray points to an int array *
*********************************************************************************************/
void DisplayCount(int wordArray[])
{
double sum = 0;
cout<<"tWord Lengthtt"<<"Frequencyn";
cout<<"t-----------tt"<<"---------n";
for(int i=1; i<16; i++)
{
cout<<"t "<<i<<"ttt "<<wordArray[i]<<endl; //Displays the contents of each element
sum+=(i*wordArray[i]); //Keeps a running total of contents of array
}
cout<<"tAverage word length: "<<sum/(15)<<endl; //Displays the average word length
}
仅在某些条件下执行字符输入,可确保在其他情况下不会进行。
例如,在代码
中 while (!isspace(ch)) //Spaces are delimeters of words
{
if(isalnum(ch)) //if current character is a alpha numeric character it is counted as part of the word
{
++Length;
cin.get(ch);
}
如果ch
不是一个空间,则外循环将继续迭代,如果碰巧ch
不是字母数字,则cin.get
将不执行,然后继续进行外循环的下一个迭代,等等在。
实现在这种情况下发生的事情的一种好方法是在源代码级调试器中运行该程序。Windows中的Visual Studio具有不错的功能。然后,您只需单步执行程序执行即可注意,每个步骤都很好。
一个无关但重要的问题:C角色分类功能(例如isalnum
)不接受EOF
以外的负面参数。因此,对于国际,使用char
值的简单调用通常会具有不确定的行为,并且可能会在调试构建中崩溃。因此,始终将char
的实际参数投入到unsigned char
。
另一个无关但重要的事情:void main
是非标准的,会导致您的程序被许多编译器(包括G )拒绝,并且不仅仅是一个字符,而不仅仅是int main
。换句话说,写void main
非常愚蠢。AFAIK出于未知的原因&ndash;但这意味着您可以遇到void main
,例如在Visual Studio生成的代码中。
相关文章:
- 程序在尝试猜测它选择的随机数时进入无限循环?
- 在这个无限循环中,当输入 0 时终止,当输入 int 范围之外的任何内容时,程序行为不正常
- 在控制台上输出我从文件中读取的整数,将程序插入无限循环
- 如何找出为什么我的程序一直有一个无限循环?
- Atmel Studio 7:ASF 库提供的 USART 中断处理程序仅在 main() 中遇到无限循环时工作一次?
- 对程序使用 switch 语句,导致选项的无限循环
- 无限循环使程序停止工作
- 为什么当用户输入除整数之外的任何内容时,我的程序会无限循环输出?C++
- 如何在 Windows 上的C++控制台应用程序中捕获 ctrl-c 事件,而不会进入无限循环
- 基本菜单驱动程序C ,无限循环
- 简单的TicTacToe.程序处于无限循环中
- 这是一个无限循环吗?程序提前完成
- 为什么我的程序在输入字符时有一个无限循环
- 角色计数程序无限循环
- 为什么我在程序集中的嵌套 for 循环会导致无限循环
- 如何在我的程序中突破无限循环
- 我的程序中的无限循环
- 使程序在无限循环中等待时使用更少的 CPU
- 从C++应用程序运行批处理脚本,并检查它是否具有无限循环
- 程序进入无限循环