简体   繁体   English

为什么我的函数不返回字符串?

[英]Why doesn't my function return a string?

I created a function (displayChoice) to generate a random number and then created another function (getComputerChoice) to take that number and assign that random integer to a string. 我创建了一个函数(displayChoice)生成一个随机数,然后创建了另一个函数(getComputerChoice)来获取该数字并将该随机整数分配给一个字符串。 When I print that string, nothing appears. 当我打印该字符串时,什么也没有出现。 I am lost as to why this happens. 我不知道为什么会这样。

#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;
}

You have to use the assignment operator. 您必须使用赋值运算符。

eg ChoiceString = "Spock"; 例如ChoiceString = "Spock";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM