C++ 需要帮助调试字符输入吗

C++ 需要帮助调试字符输入吗,c++,char,cout,cin,C++,Char,Cout,Cin,我编写了一个程序,每次读取输入一个单词,直到输入一个单独的“q” 然后,程序报告以元音开头的单词数、以辅音开头的单词数以及不符合这两个类别的单词数 #include <iostream> #include <cstdlib> int main() { char ch; bool cont = true; //for controlling the loop bool space = false; //see if there is a space in the inpu

我编写了一个程序,每次读取输入一个单词,直到输入一个单独的“q” 然后,程序报告以元音开头的单词数、以辅音开头的单词数以及不符合这两个类别的单词数

#include <iostream>
#include <cstdlib>

int main()
{
char ch;
bool cont = true; //for controlling the loop
bool space = false; //see if there is a space in the input
    int i = 0; //checking if the input is the first word
int consta, vowel, others;
consta = vowel = others = 0;
std::cout<<"Enter words (q to quit)\n";

while (cont && std::cin>>ch) //continue while cont is true and the input succeded
{
    if (i == 0) //check if this is the first word
    {
        if (isalpha(ch))
            if ((ch == 'a' ||ch == 'e' ||ch== 'i' ||ch== 'o' ||ch== 'u') || (ch == 'A' ||ch== 'E' ||ch== 'I' ||ch== 'O' ||ch== 'U'))
                ++vowel;
            else
                ++consta;
        else
            ++others;
        ++i; //add 1 to i so this if statement wont run again
    }


    if (space == true) //check if the last input was a space
    {
        if (!isspace(ch)) //check if the current input is not a space
        {
         if (ch != 'q') //and ch is not 'q'
         {
        if (isalpha(ch))
            if ((ch == 'a' ||ch == 'e' ||ch== 'i' ||ch== 'o' ||ch==   'u') || (ch == 'A' ||ch== 'E' ||ch== 'I' ||ch== 'O' ||ch== 'U'))
                ++vowel;
            else
                ++consta;
        else
            ++others;

        space = false;
        }

        }
        else
            cont = false;
    }
    if (isspace(ch)) //check if ch is a space
        space = true;
}

std::cout<<"\n"<<consta<<" words beginnig with constants\n";
std::cout<<vowel<<" words beginnig with vowels\n";
std::cout<<others<<" words beginning with others\n";

system("pause");
return 0;
}
#包括
#包括
int main()
{
char ch;
bool cont=true;//用于控制循环
bool space=false;//查看输入中是否有空格
int i=0;//检查输入是否是第一个字
int常量、元音、其他;
常量=元音=其他=0;
std::coutch)//在cont为true且输入成功时继续
{
if(i==0)//检查这是否是第一个单词
{
if(艾萨帕(ch))
如果((ch='a'| ch='e'| ch='i'| ch='o'| ch='u')|(ch='a'| ch='e'| ch='i'| ch='o'| ch='u'))
++元音;
其他的
++康斯塔;
其他的
++其他,;
++i、 //将1添加到i,这样该if语句就不会再次运行
}
if(space==true)//检查最后一个输入是否为空格
{
if(!isspace(ch))//检查当前输入是否不是空格
{
如果(ch!='q')//并且ch不是'q'
{
if(艾萨帕(ch))
如果((ch='a'| ch='e'| ch='i'| ch='o'| ch='u')|(ch='a'| ch='e'| ch='i'| ch='o'| ch='u'))
++元音;
其他的
++康斯塔;
其他的
++其他,;
空格=假;
}
}
其他的
cont=假;
}
if(isspace(ch))//检查ch是否为空间
空格=真;
}
希望这对你有帮助

#include<iostream>
#include<cstdlib>
#include<string>
int main()
{
    char ch[50];
    int consta, vowel, others;
    consta = vowel = others = 0;
    bool flag = false;
    std::cout<<"Enter words (q to quit)\n";
    while(1)
    {
        if(flag == true)
        {
            break;
        }
        gets(ch);
        char* pch;
        pch = strtok(ch," ");
        if(strcmp("q",pch)==0)
        {
            break;
        }
        while (pch != NULL)
        {
            if(strcmp("q",pch)==0)
            {
                flag = true;
                break;
            }
            if(isalpha(pch[0]))
            {
                if ((pch[0] == 'a' ||pch[0] == 'e' ||pch[0]== 'i' ||pch[0]== 'o' ||pch[0]== 'u') || (pch[0] == 'A' ||pch[0]== 'E' ||pch[0]== 'I' ||pch[0]== 'O' ||pch[0]== 'U'))
                    ++vowel;
                else
                    ++consta;
            }
            else
                ++others;
            pch = strtok (NULL, " ");
        }
    }
    std::cout<<"\n"<<consta<<" words beginnig with constants\n";
    std::cout<<vowel<<" words beginnig with vowels\n";
    std::cout<<others<<" words beginning with others\n";
    std::cin.ignore();
    return 0;
}
#包括
#包括
#包括
int main()
{
char-ch[50];
int常量、元音、其他;
常量=元音=其他=0;
布尔标志=假;

std::cout假设您希望在while循环中使用相同的代码结构

while (cont && std::cin>> std::noskipws >>ch)
noskipws:这将改变cin流的行为,使其不忽略空格。它还将在流的末尾添加一个空格。请注意结尾空格。幸运的是,您的代码处理空格,因此您不会看到它的影响

当您在代码中处理空格时,这将起作用。此外,默认情况下,cin的行为是忽略空格


但是,我认为您可以降低代码的复杂性,并且您可能不需要此语句

问题是,
std::cin
默认情况下会忽略所有制表符和空格

if (isspace(ch)) //check if ch is a space
    space = true;

永远不要将
空格设置为
true
。避免此问题的一种方法是使用
std::cin.get(ch)
而不是
std::cin>>ch

请查看我的解决方案。我的解决方案有任何问题。如果您没有对我的答案进行投票,至少可以发表评论