【问题标题】:Comparing strings in a 2D array to user input将二维数组中的字符串与用户输入进行比较
【发布时间】:2016-01-08 19:23:00
【问题描述】:

我试图让用户选择一个团队(名称包含在 2D 数组中),输入所需的名称/团队,然后使用 for 循环将用户输入的单词与 2D 数组的单词进行比较,并使用该循环访问数组中的每个字符串:

#include <string>
#include <iostream>
#include <string.h>

int main(int argc, char* argv[]){

    char cPlaychar[10][15] = { "BlackFurs", "EppiGods", "FairyDusters",  
                               "Dwarvin", "Bloods", "Cryptics", "ArcAngels",
                               "DarkVillians", "Heroiteks", "Mass", };
    char cKeyInput[15];
    int results[10];

    std::cout << "Please select a character
                  by typing the name and pressing enter" << std::endl;
    std::cin >> cKeyInput;

    for(int Array = 0; Array < 10; Array++){
       switch (Array){
       case '0': 
           results[0] = strcmp(cPlaychar[Array], cKeyInput);
           if (results[0] = 0){
               std::cout << "you have picked the first char";
           }
           break;
       case '1': results[1] = strcmp(cPlaychar[Array], cKeyInput);
           if (results[1] = 0){
               std::cout << "you have picked the secound char";
           }
           break;
       case '2':results[2] = strcmp(cPlaychar[Array], cKeyInput);
           if (results[2] = 0){
               std::cout << "you have picked the third char";
           }
           break;
       case '3':results[3] = strcmp(cPlaychar[Array], cKeyInput);
           if (results[3] = 0){
               std::cout << "you have picked the fourth char";
           }
           break;
       case '4':results[4] = strcmp(cPlaychar[Array], cKeyInput);
           if (results[4] = 0){
               std::cout << "you have picked the fith char";
           } 
           break;
       case '5':results[5] = strcmp(cPlaychar[Array], cKeyInput);
           if (results[5] = 0){
               std::cout << "you have picked the sixth char";
           }
           break;
       case '6':results[6] = strcmp(cPlaychar[Array], cKeyInput);
           if (results[6] = 0){
               std::cout << "you have picked the seventh char";
           }
           break;
       case '7':results[7] = strcmp(cPlaychar[Array], cKeyInput);
           if (results[7] = 0){
               std::cout << "you have picked the eighth char";
           }
           break;
       case '8':results[8] = strcmp(cPlaychar[Array], cKeyInput);
           if (results[8] = 0){
               std::cout << "you have picked the ninth char";
           }
           break;
       case '9':results[9] = strcmp(cPlaychar[Array], cKeyInput);
           if (results[9] = 0){
               std::cout << "you have picked the tenth char";
           }
           break;
       } // end of switch
} // end of for
system("pause");
return 0;
}

【问题讨论】:

  • 能否就您的问题添加一个具体问题?

标签: c++ arrays for-loop multidimensional-array


【解决方案1】:

你可以先放

using namespace std ; 

在开头并删除所有 (std::)

第二个在if (results[ ] = 0) 这必须是if (results[ ] == 0)

第三个我不知道你为什么用这个方法,你可以这样轻松搞定

string names[] = {"a","b"} ;
string num[]   = { "first", "second" } ;

string input;
cin >> input;

for (int i = 0; i < 2; ++i)
{
  if (input == names[i])
    cout << "you choose" << num[i] << endl;
}

但是如果你想要二维数组,你可以这样做

char names[2][2] = { "a", "b" };
char num[2][7]   = { "first", "second" };

char input[2];
cin >> input ;

for (int i = 0; i < 2; ++i)
{
  if (!strcmp(input,names[i]))
    cout << "you choose " << num[i] << endl;
}

【讨论】:

  • std:: 替换为using namespace std 被认为是一种不好的做法,因为当您使用多个命名空间时,它可能会导致函数名称冲突的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
相关资源
最近更新 更多