Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C+中的字符计数+;_C++ - Fatal编程技术网

C++ C+中的字符计数+;

C++ C+中的字符计数+;,c++,C++,不确定为什么我的循环不起作用,每次我尝试输入时它都会粘住:/ 我希望有一个输入,只是显示了不同类型的计数,我已经列出 #include <iostream> #include <string> #include <iomanip> #include <cctype> using namespace std; int main() { char ch; int puncCount = 0; int letterCount = 0; int digi

不确定为什么我的循环不起作用,每次我尝试输入时它都会粘住:/ 我希望有一个输入,只是显示了不同类型的计数,我已经列出

#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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
char ch;
int puncCount=0;
int-letterCount=0;
int-digitCount=0;
int spaceCount=0;

不能重复调用以获取循环中的字符。此外,如果这是您的目标,请检查“q”和“q”。

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

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

@XTyke没问题。别忘了检查“Q”。如果这个答案足以解决您的问题@XTyke,那么您应该将它标记为您问题的答案。谢谢。