将2D数组中的字符串与用户输入进行比较

时间:2015-10-11 06:02:31

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

我正在尝试让用户选择一个团队(名称包含在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;
}

1 个答案:

答案 0 :(得分:0)

首先你可以把

React.createElement(
  'div',
  { className: 'static-modal' },
  React.createElement(
    Modal,
    { title: 'Modal title', bsStyle: 'primary', backdrop: false, animation: false, container: mountNode, onRequestHide: handleHide },
    React.createElement(
      'div',
      { className: 'modal-body' },
      'One fine body...'
    ),
    React.createElement(
      'div',
      { className: 'modal-footer' },
      React.createElement(
        Button,
        null,
        'Close'
      ),
      React.createElement(
        Button,
        { bsStyle: 'primary' },
        'Save changes'
      )
    )
  )
);

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

using namespace std ; 中的第二个,必须是if (results[ ] = 0)

第三,我不知道你为什么使用这种方法,你可以像这样轻松地做到这一点

if (results[ ] == 0)

但是如果你想要2d阵列就可以做到这一点

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