2016-09-28 124 views
-2

所以我正在用C++编写一个小的Rock,Paper,Scissors游戏结构,并且遇到了一些我不明白的错误。C++函数和奇怪的错误

首先是代码期望一个';'在NumberToWord函数中,但它不应该是因为它是一个函数。

另一个错误是它似乎不喜欢的其他错误之一。

也许我失去了一些东西,我不知道,但它应该是一个简单的修复。

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

感谢您的帮助!

Errors


第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

+1

您应该在您的问题中逐字地发布错误消息 - 如** text **,* not * image。 –

+4

你不能在'main()'函数体中定义另一个函数,除非它是一个lambda表达式。 –

+0

解决了一个问题,但是我仍然不能使用符合cout >>的函数来输出它 – CTOverton

回答

1

功能string numberToWord (int x)嵌套在main函数内。这是无效的C++。

GCC编译器确实支持嵌套函数作为扩展,但它不是标准和其他编译器(我知道)的一部分不接受它。只是不要那样做。将函数移出main(或者,如果有意义的话,使其成为lambda)。

1

问题很简单。 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; 
}; 
+0

现在唯一的问题是我仍然不能使用: – CTOverton

+0

cout <<“Compuer选择:”<< numberToWord(computerChoice)<< endl; – CTOverton

+0

@rext,你是什么意思“仍然不能使用”。不适合你? –