[英]C++ Functions and strange errors
所以我正在用 C++ 编写一个小的 Rock, Paper, Scissors 游戏结构,但我遇到了一些我不明白的错误。
函数string numberToWord (int x)
不能在函数 main 中。 由于编译器的工作方式,它必须是一个单独的方法。 我只是将它移出然后它工作正常。
所以我正在用 C++ 编写一个小的 Rock, Paper, Scissors 游戏结构,但我遇到了一些我不明白的错误。
第一个是代码需要一个 ';' 在 NumberToWord 函数中,但不应该,因为它是一个函数。
另一个错误是它似乎不喜欢的 else 语句中的一个随机错误。
也许我错过了一些东西,我不知道,但这应该是一个简单的修复。
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main()
{
int seed = static_cast <int> (time(0)); //Sets the random seed
srand(seed);
int winCount = 0;
string numberToWord (int x) {
string outputChoice;
if (x == 0) { outputChoice = "Rock"; }
else if (x == 1) { outputChoice = "Paper"; }
else if (x == 2) { outputChoice = "Scissors"; }
return outputChoice;
}
while (winCount < 3) {
int computerChoice = rand() % 4;
int userChoice;
cout << userChoice << endl;
cout << "Please Enter 0 for Rock, 1 for Paper, or 2 for Scissors: "; //Asks for user input
cin >> userChoice; //Inputs user input to variable
if (userChoice == computerChoice) {
cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
cout << "You Choose: " << numberToWord(userChoice) << endl;
cout << "Draw!" << endl;
}
else if ((userChoice == 1) && (computerChoice == 2)) { //Rock v Paper
cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
cout << "You Choose: " << numberToWord(userChoice) << endl;
cout << "Compuer wins!" << endl;
}
else if ((userChoice == 1) && (computerChoice == 3)) { //Rock v Scissors
cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
cout << "You Choose: " << numberToWord(userChoice) << endl;
cout << "You win!" << endl;
winCount += 1;
}
else if ((userChoice == 2) && (computerChoice == 1)) { //Paper v Rock
cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
cout << "You Choose: " << numberToWord(userChoice) << endl;
cout << "You win!" << endl;
winCount += 1;
}
else if ((userChoice == 2) && (computerChoice == 3)) { //Paper v Scissors
cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
cout << "You Choose: " << numberToWord(userChoice) << endl;
cout << "Compuer wins!" << endl;
}
else if ((userChoice == 3) && (computerChoice == 1)) { //Scissors v Rock
cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
cout << "You Choose: " << numberToWord(userChoice) << endl;
cout << "Compuer wins!" << endl;
}
else if ((userChoice == 3) && (computerChoice == 2)) { //Scissors v Paper
cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
cout << "You Choose: " << numberToWord(userChoice) << endl;
cout << "You win!" << endl;
winCount += 1;
}
}
return 0;
}
感谢您的任何帮助!
第2部分
简单地说,程序不喜欢'<<'。 我在许多其他程序中很好地使用它来处理变量,但是这次当我使用字符串变量时,它会引发错误。 我查找了 C++ 字符串变量,看起来我做对了,所以我不知道错误的原因。
参考:
http://www.cplusplus.com/doc/tutorial/basic_io/
http://www.cplusplus.com/doc/tutorial/variables/
void displayOutput(int comp, int user, string winner) {
string compOutputChoice = "";
string userOutputChoice = "";
/*
if (comp == 0) { compOutputChoice = "Rock"; }
else if (comp == 1) { compOutputChoice = "Paper"; }
else if (comp == 2) { compOutputChoice = "Scissors"; }
if (user == 0) { userOutputChoice = "Rock"; }
else if (user == 1) { userOutputChoice = "Paper"; }
else if (user == 2) { userOutputChoice = "Scissors"; }
*/
cout << "Compuer Choose: " << compOutputChoice << endl;
cout << "You Choose: " << userOutputChoice << endl;
//cout << winner << endl;
return;
}
错误:
错误(活动)无运算符“<<” 32
错误(活动)无运算符“<<” 33
错误 C2679 二进制“<<”:未找到采用“std::string”类型的右侧操作数的运算符(或没有可接受的转换)32
错误 C2679 二进制“<<”:未找到采用“std::string”类型的右侧操作数的运算符(或没有可接受的转换)33
函数string numberToWord (int x)
嵌套在main
函数中。 那不是有效的 C++。
GCC 编译器确实支持嵌套函数作为扩展,但它不是标准的一部分,其他编译器(我知道的)不接受它。 不要那样做。 将函数从main
移出(或者,如果有意义,将其设为 lambda)。
问题很简单。 numberToWord 不能是 main 的内部函数。 如果您使用的是较新的 C++,请将其移至 main 之外或将其更改为 lambda。
auto numberToWord = [](int x) -> string {
string outputChoice;
if (x == 0) { outputChoice = "Rock"; }
else if (x == 1) { outputChoice = "Paper"; }
else if (x == 2) { outputChoice = "Scissors"; }
return outputChoice;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.