gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:26:49 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com