C++带有getche()函数的短语计数器
C++ phrase counter with getche() function
#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)?
经过一些尝试,我终于得到了正确的答案,我喜欢发布它来帮助任何感兴趣的人..谢谢大家的帮助
#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;
}
相关文章:
- 循环在计数器中不起作用
- python集合的C++等价物是什么.计数器
- Python 集合.计数器,如何避免重复查找
- 请解释字谜的代码,我看不懂计数器数组,每个值已经是0
- std::shared_ptr 自定义参考计数器
- 英特尔 TBB 程序不会终止,可能会误用参考计数器
- 如何在 c++ 中定义一个将被另一个短语替换的短语?
- 不正确的比较和交换计数器输出用于快速排序功能
- OpenSSL API,使用GCM(伽罗瓦计数器模式)进行AES加密
- C++ 在循环中添加计数器变量并再次初始化其值
- 试图创建一个体面的基本刽子手游戏。猜出的字母不会取代短语中的字母
- 需要帮助才能在C++中创建类计数器
- 漂亮的计数器习语的错误或格式错误的静态订单惨败?
- 几乎总是自动和带计数器的循环
- 线程使用计数器C++
- 为什么使用 2 个嵌套循环 O(n^2) 复杂度来解决二和问题,当只改变循环计数器逻辑时运行得更快?
- 对于循环计数器不递增
- C++输出的就地计数器
- 用c++交换短语中单词的前两个字母
- C++带有getche()函数的短语计数器