我正在为有用户播放器和计算机播放器的剪刀石头布编写程序。我相信一切都取决于bool值返回函数。它需要接受两个参数(计算机的选择,玩家的选择),并查看它们是否等于打印“ Tie”。但是,我收到一个错误,指出我的两个参数的标识符未声明。

我尝试将其更改为int函数而不是bool。并以我的bool语句为主,但没有用

  #include <iostream>
  #include <iomanip>
  #include <string>
  #include <cstdlib>
  #include <ctime>
  #include <cmath>

 using namespace std;

int getComputerChoice();
int getPlayerChoice();
bool isTie (int, int);

 int main()
  {

char choice;
int compChoice;
int plaChoice;


do
{
    cout << "ROCK PAPER SCISSORS MENU" << endl;
    cout << "--------------------------" << endl;
    cout << "p)Play Game" << endl;
    cout << "q)Quit" << endl;
    cout << "Please enter your choice : " << endl;
    cin >> choice;


    while (choice != 'p' && choice != 'q')//or if//why &&
    {
        cout << "Invalid selection. Try again." << endl << endl << endl;
        cin >> choice;
    }

    switch (choice)
    {
    case 'p':
        compChoice = getComputerChoice();

        plaChoice = getPlayerChoice();

        if (plaChoice == 1)
        {
            cout << "You chose: Rock" << endl;
        }
        else if (plaChoice == 2)
        {
            cout << "You chose: Paper" << endl;
        }
        else
        {
            cout << "You chose: Scissors" << endl;
        }


        if (compChoice == 1)
        {
            cout << "The computer chose: Rock" << endl;
        }
        else if (compChoice == 2)
        {
            cout << "The computer chose: Paper" << endl;
        }
        else
        {
            cout << "The computer chose: Scissors" << endl;
        }


        if (isTie(compChoice, plaChoice))
        {
            cout << "It is a Tie!";
        }



        break;


    case 'q':
        cout << "You have chosen to quit the program. Thank you for using the program!" << endl;
        break;

    }

} while (choice != 'q');


 return 0;
}



  int getComputerChoice()
   {
int comp = 0;
int rando = 0;
srand((unsigned int)time(NULL));

rando = rand() % 3 + 1;

switch (rando)
{
case 1:
     comp = 1;
    break;

case 2:
    comp = 2;
    break;

case 3:
    comp= 3;
    break;

    return comp;
}
  }


   int getPlayerChoice()
  {

int player;
cout << "Rock, Paper or Scissors?" << endl;
cout << "1) Rock" << endl;
cout << "2) Paper" << endl;
cout << "3) Scissors" << endl;
cout << "Please enter your choice: " << endl;
cin >> player;

while (player != 1 && player != 2 && player != 3)
{
    cout << "Invalid" << endl;
    cin >> player;
}
return player;
    }

  bool isTie(compu, playa)
   {
if (compu == playa)
    return true;
else
    return false;

     }


这些是我收到的错误消息
compu':未声明的标识符
playa':未声明的标识符
'isTie':重新定义;以前的定义是“功能”
参见“ isTie”的声明
'isTie':函数样式的初始值设定项似乎是函数定义

最佳答案

isTie是具有2个参数的函数。
从您的代码中,我可以看到它期望2 integers

因此,您需要将函数签名更新为:

bool isTie(int compu, int playa)

10-07 19:15
查看更多