我创建了一个函数(displayChoice)来生成一个随机数,然后创建了另一个函数(getComputerChoice)来获取该数字并将该随机整数分配给一个字符串。当我打印该字符串时,什么也没有出现。我不知道为什么会这样。
#include <iostream>
#include <limits>
#include <ctime>
#include <cstdlib>
int getComputerChoice();
std::string displayChoice(int dChoice);
int main()
{
std::string comp = displayChoice(getComputerChoice());
std::cout << comp;
}
int getComputerChoice()
{
srand(time(NULL));
int ranCompChoice = (rand() % 6) + 1;
return ranCompChoice;
}
std::string displayChoice(int dChoice)
{
std::string ChoiceString;
if (dChoice == 1)
{
ChoiceString == "rock";
}
else if (dChoice == 2)
{
ChoiceString == "paper";
}
else if (dChoice == 3)
{
ChoiceString == "scissors";
}
else if (dChoice == 4)
{
ChoiceString == "lizard";
}
else
{
ChoiceString == "Spock";
}
return ChoiceString;
}
最佳答案
您必须使用赋值运算符。
例如ChoiceString = "Spock";