2015-10-11 137 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 –

相關問題