向字符串数组中的元素添加数字值

Adding number values to elements in a string array

本文关键字:添加 数字 元素 字符串 数组      更新时间:2023-10-16

所以我的第一堂C++课已经 2 周了,我无可救药地坚持我目前的作业。我必须创建一个世界问题列表,并让用户按重要性顺序排列它们。

现在我正在尝试分配数字值,以便用户可以通过键入 1-5 来组织它们来轻松调用它们。我写的代码不起作用,我只是想看看我是否错过了什么。

int main()
{
    string topics[5]{"World Hunger", "Government Oppression", "Genocide", "Disease", "Poor Education"};
    int responses[2][5] = {{1,2,3,4,5},{1,2,3,4,5}};
    topics[5] = responses[5];
    return 0;
}

错误:从"int*"到"char"的转换无效

for(int i=0; i<5; ++i) {
    std::cout << i+1 << ". " << topics[i] << std::endl;
}
std::cin >> userChoice;
if(userChoice == topics[0] || userChoice == "1") {
    //etc
} else if(userChoice == topics[1] || userChoice == "2") {
    //etc
} else if(/*etc etc etc*/) { //....