c++ - 将二维数组中的字符串与用户输入进行比较

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

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

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

最佳答案

首先你可以放

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

关于c++ - 将二维数组中的字符串与用户输入进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33062073/

相关文章:

android - 在 Android NDK 中使用 pngquant 库(libimagequant)

c++ - 通过 Shader DirectX 9 传递纹理

c++ - 单击聚焦小部件时的聚焦事件

c++ - 在数组的开头添加一个元素并移动其余元素但保持大小不变

javascript - 如何循环该数组以仅提取 orderTotal?

arrays - Golang中如何返回未知类型的数组?

python - Django 中每个客户(通过迭代)的订单值(value)计数和总和

c++ - for_each 算法导致 basic_string::_M_construct null 在 C++ 中无效

python - 在 python 中制作副本

c++ - 读取 std::string 行(支持空格和特殊键)