【问题标题】:How do I stop accepting a string at Enter in C++如何在 C++ 中的 Enter 处停止接受字符串
【发布时间】:2015-07-23 00:37:24
【问题描述】:

我正在尝试制作一个程序来查找 Hangman 游戏中最常见的角色。我希望用户在标准输入中输入电影名称列表(由 Enter 分隔)。电影名称的长度可变。

但是,在输入 MOVIE_NAME_SIZE 的所有字符之前,程序不会停止接受输入。我尝试检查“\0”和“\n”。它似乎不起作用。我做错了什么?另外,有没有更简单的方法可以做到这一点?

附: - 很抱歉,如果以前有人问过这个问题。我看了看,有人建议使用 getline() 方法,但这是针对整个字符串的,而我需要访问单个字符。

char movieList[MAX_MOVIES][MAX_NAME_SIZE];
char CapitalAlphabetList[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
char smallAlphabetList[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char numberList[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
int alphabetCount[26] = {0};
int numberCount[10] = {0};
int i, j, k;

for(i = 0; i < MAX_MOVIES; i++)
{
    for(j = 0; j < MAX_NAME_SIZE; j++)
    {
        cin >> movieList[i][j];

        if(movieList[i][j] == '\0')
            break;

        for(k = 0; k < 26; k++)
            if((movieList[i][j] == smallAlphabetList[k]) || (movieList[i][j] == CapitalAlphabetList[k]))
                alphabetCount[k]++;

        for(k = 0; k < 10; k++)
            if(movieList[i][j] == numberList[k])
                numberCount[k]++;

    }
}

【问题讨论】:

  • 您为什么不检查"STOP""NONE" 而不是将cin 抓取的任何内容作为最后一个字符。在临时变量上使用cin 然后检查size == 0 怎么样?
  • @Matt 我不明白。你能详细说明一下吗?用户不会输入"STOP""NONE"。它们是关键字吗?
  • 它们是你必须输入才能让它停止的东西。如果您希望它在没有输入时停止,我会将cin 捕获到一个字符串中,然后检查字符串的长度。如果您不使用std::string,这更像是一个C 问题。唯一类似于 C++ 的就是您对 cin 的调用
  • @Matt 谢谢!我会试试的。但是,不应该存在替代(更简单)的解决方案吗?因为字符串总是可能无意中包含多个 STOP 或 NONE(电影名称的一部分),但始终只有一个换行符。
  • 没有string,没有vector。见鬼,甚至 C 的 isalpha()isdigit() 都没有证据。再次看着这个,我的眼角抽搐了一下……至于getline(),为什么不读取该行,然后在内存中解析它?

标签: c++ string ascii


【解决方案1】:

您可以在代码中设置条件,例如:

if(string_name.length()==0)
break;

这也很好用。

【讨论】:

    【解决方案2】:

    我知道您提到不想使用getline,但您仍然可以访问字符串的单个字符。例如,如果您声明

    char exampleMovie[MAX_NAME_SIZE];
    

    在您的外循环之外,您可以将以下代码放入您的外循环中,以便为每个电影名称存储一个新的C-style string

    cin.getline(exampleMovie, MAX_NAME_SIZE);
    

    然后,在您的内部循环中(将从 1 循环到 strlen(exampleMovie) - 这将节省您对短名称电影的操作),您可以使用以下代码访问 exampleMovie 的每个字符:

    if (exampleMovie[j] == smallAlphabetList[k]) // etc....
    

    【讨论】:

      【解决方案3】:

      简而言之,这就是您可以在程序中使用strcmp()cin.getline() 在循环中在空的ENTER 按键处停止的方法。用这些线。这是因为cin 在按下回车时添加了'\0'

      §从 std::istream 格式化输入到字符数组中的输入将以空值结尾,如 C++11 27.7.2.2.3/9 中所述:

      #include <iostream>
      #include <string.h>
      using namespace std;
      
      int main () {
      
          char name[256]={' '};
          while( true ){
            memset(name,'\0',sizeof(name)); // reset char array
            cout << ">";
            cin.getline (name,256); // cin addes a \0 when empty enter is pressed.
            cout<<"\n"<<name<<"\n";
            if ( strcmp( name, "\0" ) == 0 )  break; // break at empty enter key
          }
      
      
        cout << " \nPress any key to continue\n";
        cin.ignore();
        cin.get()
        return 0;
      }
      

      必须更改程序,现在while() 循环处理输入,直到按下空的回车键。 然后,它会按照您的预期运行。

      固定代码:

      #include <iostream>
      #include <string.h>
      #include <stdlib.h>
      using namespace std;
      
      int main () {
          const int MAX_MOVIES = 100;
          const int MAX_NAME_SIZE = 100;
      
          char movieList[MAX_MOVIES][MAX_NAME_SIZE];
          char CapitalAlphabetList[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
          char smallAlphabetList[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
          char numberList[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
          int alphabetCount[26] = {0};
          int numberCount[10] = {0};
          int i=0, j=0, k=0 , l=0;
      
          memset(movieList,'\0',sizeof(movieList));
          cout<<"Movie Title Hangman \n\n";
      
          while( true ){
              for(l=0;l<i;l++)cout<<movieList[l]<<"\n";
      
              cout<<"Turn number "<<i<<" , enter movie >";
              memset(movieList[i],'\0',MAX_NAME_SIZE);
              cin.getline (movieList[i],256);
      
              cout<<"------------------------------------------\n";
      
              if( strcmp(movieList[i],"\0") ==0){
      
                  for (i=0;i<MAX_MOVIES ;i++){
                      for(j=0;j<MAX_NAME_SIZE ;j++){
      
                          for(k = 0; k < 26; k++){ 
                             if((movieList[i][j] == smallAlphabetList[k]) || (movieList[i][j] == CapitalAlphabetList[k]))
                              alphabetCount[k]++;
                          }
      
                          for(k = 0; k < 10; k++){ 
                                  if(movieList[i][j] == numberList[k])
                                      numberCount[k]++;
                          }
      
                      }
                  }
               break; 
      
              }
             i++;
          }
          cout<<"\n\n";
          cout<<"The most common characters are: \n\n";
          for(k = 0; k < 26; k++) cout<<""<<smallAlphabetList[k] <<" = "<< alphabetCount[k]<<"\t";
      
          cout<<"\n\n";
          for(k = 0; k < 10; k++) cout<<""<<numberList[k] <<" = "<<  numberCount[k]<<"\t";
      
      
        cout << " \nPress any key to continue\n";
        cin.ignore();
        cin.get()
        return 0;
      }
      

      【讨论】:

        【解决方案4】:

        读入一个字符串,然后查看该字符串中的字符。

        map 在计算中出现的次数要简单得多

        #include <vector>
        #include <map>
        #include <string>
        #include <iostream>
        
        std::map<char, int> letterCounts;
        std::map<char, int> numberCounts;
        
        for (std::string movie; std::getline(cin, movie); )
        {
            for (char c : movie)
            {
                if (std::isalpha(c)) 
                {
                    ++letterCounts[std::tolower(c)];
                }
                if (std::isdigit(c))
                {
                    ++numberCounts[c];
                }
            }
        }
        

        【讨论】:

        • 如果您不需要对map进行排序,在这种情况下使用unordered_map会更好
        猜你喜欢
        • 2015-07-27
        • 1970-01-01
        • 1970-01-01
        • 2015-12-13
        • 1970-01-01
        • 1970-01-01
        • 2014-12-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多