【发布时间】:2020-04-07 16:22:51
【问题描述】:
所以我在空闲时间一直在学习 C++,我正在尝试构建一个简单的石头剪刀布游戏。当我尝试运行它时,我得到了 x、y 和 z 的“未在此范围内定义错误”。
#include <iostream>
#include <stdlib.h>
#include <string>
int main(){
string x,y,z;
srand (time(NULL));
int computer = rand() % 3 + 1;
int user = 0;
std::cout << "====================\n";
std::cout << "rock paper scissors!\n";
std::cout << "====================\n";
std::cout << "1) ✊\n";
std::cout << "2) ✋\n";
std::cout << "3) ✌️\n";
std::cout << "shoot! ";
std::cin >> user;
if(user== 1){
x = (computer == 3) ? "You win!" : "You lose.";
std::cout<<x;
}else if(user==2){
y = (computer == 1) ? "You win!" : "You lose.";
std::cout<<y;
}else{
if(user == 3){
z = (computer == 2) ? "You win!" : "You lose.";
std::cout<<z;
}
}
}
似乎是什么问题?
【问题讨论】:
-
另外:在 C++ 项目中,更喜欢
<cstdlib>而不是<stdlib.h>。 -
建议:在以后的问题中,将完整的错误信息复制到问题中。它使答案更容易说明问题所在(您可以更快地得到答案),并使未来的提问者更容易找到和理解问题及其答案。
-
@ReinstateMonica 如果您想知道为什么您应该更喜欢
<cstdlib>而不是<stdlib.h>,请提供更多文档。 stackoverflow.com/questions/2900785/…