gpt4 book ai didi

c++ - C++中的字符计数

转载 作者:搜寻专家 更新时间:2023-10-31 00:31:31 26 4
gpt4 key购买 nike

不确定为什么我的循环不起作用,每次我尝试输入时它都会卡住:/我希望输入能够显示我列出的不同类型的数量。

#include <iostream>
#include <string>
#include <iomanip>
#include <cctype>

using namespace std;
int main() {
char ch;
int puncCount = 0;
int letterCount = 0;
int digitCount = 0;
int spaceCount = 0;

cout << "The characters which you'd like!" << endl;
cout << "Type a line with a single 'Q' to stop \n" << endl;
cin.get(ch);

while (ch != 'q')
{

letterCount += isalpha(ch);
puncCount += ispunct(ch);
digitCount += isalnum(ch);
spaceCount += isspace(ch);

}

cout << "Letter count is" << letterCount << endl;
cout << "Puncuation count is" << puncCount << endl;
cout << "Digit count is" << digitCount << endl;
cout << "Space count is" << spaceCount << endl;
return 0;
}

最佳答案

您需要再次调用以在循环中获取输入:

while (ch != 'q')
{
// ...
cin.get(ch);
}

关于c++ - C++中的字符计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33906927/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com