【问题标题】:need help debugging character input需要帮助调试字符输入
【发布时间】:2013-07-28 05:53:27
【问题描述】:

我编写了一个程序,它一次读取输入一个单词,直到输入一个单独的“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;
}

但是当我输入一个空格和一个'q'时它不会终止。 但是如果 i 输入 '^Z' 它确实会终止,但康铜始终为 1,其他始终为 0。

【问题讨论】:

  • 请看看我的解决方案
  • 我的解决方案有什么问题。至少有一个评论会因为我的回答不赞成而受到赞赏

标签: c++ char cout cin


【解决方案1】:

问题是std::cin 默认会忽略所有制表符和空格。所以在你的代码中

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

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

【讨论】:

    【解决方案2】:

    假设要使用相同的代码结构,在while循环中使用

    while (cont && std::cin>> std::noskipws >>ch)
    

    std::noskipws :这将 cin 流的行为更改为不忽略空格。它还在流的末尾添加了一个空格。当心末端空间。幸运的是,您的代码处理了空格,因此您不会看到它的影响。

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

    不过我认为你可以降低代码的复杂度,你可能不需要这个语句

    【讨论】:

      【解决方案3】:

      希望对你有帮助

      #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;
      }
      

      strtok请参考this。这会计算所有单词,无论是用空格分隔还是在单独的行中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-20
        • 1970-01-01
        • 2013-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多