2015-10-11 131 views
1

我试图让用户选择一个团队(名称包含在二维数组中),输入所需的名称/团队,然后使用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; 
} 
+0

您能否就您的问题添加具体问题? – Ziezi

回答

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

将'std ::'替换为'using namespace std'被认为是不好的做法,因为当您使用多个名称空间时,可能会导致名称与函数名称冲突的问题。 –

+1

omg非常感谢那么简单=)Saif –

+0

@Tomas wlcome:D –

相关问题