【问题标题】:How can I use strcat() within an if-else statement to change based on a user inputs "yes" or "no"?如何在 if-else 语句中使用 strcat() 来根据用户输入“是”或“否”进行更改?
【发布时间】:2021-12-12 10:08:38
【问题描述】:

我正在尝试通过开发一个基本系统来制作一个基本的选择你自己的冒险风格的游戏,程序通过该系统接受用户字符输入并使用 if-else 语句以特定模式附加字符串来做出决定。在下面的程序中,我尝试在一组条件后使用 strcat 来产生不同的输出,但是我的输出一直失败得很惨。有人能提供的任何帮助都会令人难以置信。

#include <iostream>
#include<cstring>
#include<string>
#include<iomanip>
#include<ios>
#include<limits>
using namespace std;

int main()
{
    char str1[50]= "F*ck";
    char str2[50]= "You";
    char str3[50]= "Me";

    char answer[50];

    cout<< "Do you like rock music yes or no?";
    cin>> answer;

    if (answer== "no"){
        cout<< strcat(str1,str2);
    } else (answer== "yes");{
        cout<< strcat(str1,str3);
    } 
    return 0;
}

【问题讨论】:

    标签: c++ string if-statement strcat cmath


    【解决方案1】:

    当您使用== 运算符比较C 风格的字符串时,您实际上是在比较字符串的地址,而不是内容,这在这种情况下没有帮助。尝试改用 strcmp() 库例程,您可能会更进一步。

    【讨论】:

      【解决方案2】:

      if (answer== "no") 您正在比较answer内存地址 和字符串文字"no",所以条件是false

      你应该像这样使用strcmp()

      if(strcmp(answer, "no") == 0)
      

      另外,您的else 语法错误。你应该使用else if

      将其更改为:

      else if (strcmp(answer, "yes") == 0)
      {
          std::cout<< strcat(str1,str3);
      }
      

      或者更好的是,将 C 风格的字符串扔出窗口并改用 std::string

      #include <iostream>
      #include <cstring>
      #include <string>
      #include <iomanip>
      #include <ios>
      #include <limits>
      // using namespace std; is bad
      int main()
      {
          std::string str1{"F*ck"};
          std::string str2{"You"};
          std::string str3{"Me"};
      
          std::string answer;
      
          std::cout<< "Do you like rock music yes or no?";
          std::cin>> answer;
      
          if (answer == "no")
          {
              std::cout<< (str1 + str2);
          } 
          else if(answer == "yes")
          {
              std::cout << (str1 + str3);
          } 
          return 0;
      }
      

      【讨论】:

        【解决方案3】:

        您无法使用 operator== 比较 C 样式字符串的内容。您需要改用strcmp()

        if (strcmp(answer, "no") == 0)
        

        另外,else (answer== "yes");{ 也是错误的。不仅因为比较问题,还因为您缺少必需的if,并且有错误的;。它应该是else if (strcmp(answer, "yes") == 0){

        话虽如此,您确实应该使用std::string 而不是char[],例如:

        #include <iostream>
        #include <string>
        using namespace std;
        
        int main() {
            const string str1 = "F*ck";
            const string str2 = "You";
            const string str3 = "Me";
        
            string answer;
        
            cout << "Do you like rock music yes or no?";
            cin >> answer;
        
            if (answer == "no"){
                cout << str1 << str2;
            } else if (answer == "yes"){
                cout << str1 << str3;
            }
        
            return 0;
        }
        

        【讨论】:

          【解决方案4】:

          我能够重写程序,它现在可以工作了。对于基于文本的选择您自己的冒险游戏将非常有帮助。新方案如下:

          #include <iostream>
          #include<cstring>
          #include<string>
          
          using namespace std;
          
          int main() {
          
              char str1[50]= "F*ck";
              char str2[50]= " You";
              char str3[50]= " Me";
          
              char answer[50];
          
          
              cout<< "Do you like rock music (yes or no)?"<<endl;
          
              cin>> answer;
          
          
              if (strcasecmp(answer,"no") == 0){
                  cout<< strcat(str1,str2)<<endl;
              } else if (strcasecmp(answer,"yes") == 0){
                  cout<< strcat(str1,str3)<<endl;
              } else {
                  cout<< strcat(str1," off")<<endl;
              }
          
              return 0;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-09-19
            • 2021-11-28
            • 1970-01-01
            • 1970-01-01
            • 2016-06-09
            • 2020-04-05
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多