使用getche()函数的C ++短语计数器 [英] C++ phrase counter with getche() function
本文介绍了使用getche()函数的C ++短语计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include<iostream>
using namespace std;
#include<conio.h>
int main()
{
int chcount = 0, wdcount =0, count = 0;
char ch='a';
cout << "Enter your text : ";
while ( ch != '\r' )
{
ch = getche();
if ( ch !=' ' )
{
chcount++;
count++;
}
else if (count > 2)
{
wdcount++;
count=0;
}
}
cout<<"\nCount of words is: "<<wdcount+1<<"\nCount of charcters is: "<<chcount-1<<"\n";
system("pause");
return 0;}
此代码计算大小大于两个字符的字词,用户输入的短语中的所有字符数(忽略空格)。问题是为什么字计数器初始值被认为+1,字符计数器初始值被认为-1(你可以看到cout wdcount + 1和chcount-1)?
this code counts the words with size greater than two characters and the number of all characters (ignore spaces) in a phrase typed in by the user. The question is why the word counter initial value is considered +1 and character counter initial value considered -1 (as you can see the cout wdcount+1 and chcount-1)?
推荐答案
在一些尝试后,我终于得到了正确的答案,我喜欢发布它来帮助任何人感兴趣...感谢所有为您的帮助
After some tries I finally got the correct answer I liked to post it to help anyone interested .. thanks all for your help
#include<iostream>
using namespace std;
#include<conio.h>
int main()
{
int chcount = 0, wdcount =0, count = 0;
char ch=' ';
cout << "Enter your text : ";
while ( ch != '\r' )
{
if ( ch !=' ' )
{
chcount++;
count++;
}
else if (count > 2)
{
wdcount++;
count=0;
}
ch = getche();
}
if (count >2) //validate that last word is counted
wdcount++;
cout<<"\nCount of words is: "<<wdcount<<"\nCount of charcters is: "<<chcount<<"\n";
system("pause");
return 0;
}
这篇关于使用getche()函数的C ++短语计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文