1
我創建了一個函數(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;
}
請正確縮進您的代碼。另外,您可能希望將'<< std :: endl'添加到打印輸出的末尾。 –
C++再次成爲C++並讓你做不好。 –