所以我正在尝试编写这个代码,该代码应该生成两个不同数字的总和,并根据所述总和确定玩家是赢了还是丢了。然后根据玩家的状态继续计算玩家的预算。我一直得到类型为“int(*)()”的“”参数与int类型的参数不兼容“错误,我不知道为什么。
#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
//prototyping functions
int sum();
int bla(int, int, int, int);
void main()
{
//declaring variables
int budget, bet, new_budget;
string yes_no;
//inputting budget
cout << "Please enter your budget: ";
cin >> budget;
//inputting bet
cout << "Please enter your bet: ";
cin >> bet;
//validating bet and budget
if (bet > budget)
{
cout << "Error, bet cannot be larger than budget. Please reenter your budget and bet, respectively: ";
cin >> budget;
cin >> bet;
}
else //calling functions
{
double m = sum();
double n = bla(sum, budget, bet, new_budget);
}
//asks player if they would like play again
cout << "Would you like to play again? Enter Y for Yes or N for No";
cin >> yes_no;
//repeats program
if (yes_no == "Y" && (new_budget > 0))
{
cout << "Please enter your budget: ";
cin >> budget;
cout << "Please enter your bet: ";
cin >> bet;
if (bet > budget)
{
cout << "Error, bet cannot be larger than budget. Please reenter your budget and bet, respectively: ";
cin >> budget;
cin >> bet;
}
else
{
int sum();
bool win_or_loss(int sum, double budget, double bet);
}
}
system("pause");
}
int sum() //calculates the sum of the dice
{
int x;
int y;
int sum;
x = rand() % 7;
y = rand() % 7;
sum = x + y;
cout << "The sum of the two dice is " << sum << endl;
return sum;
}
int bla(int sum, int budget, int bet, int new_budget) //calculates the new budget according to whether the player won or lost or neither
{
bool flag = false;
if ((sum == 7) || (sum == 11))
{
cout << "You have won" << endl;
new_budget = budget + bet;
flag = true;
}
else if ((sum == 2) || (sum == 3) || (sum == 12))
{
cout << "You have lost" << endl;
new_budget = budget - bet;
flag = false;
}
else
{
cout << "Neither win or loss, please generate new numbers" << endl;
int sum();
flag = false;
}
return new_budget;
}
答案 0 :(得分:0)
int bla(int, int, int, int);
bla
函数需要int
个参数,但
double n = bla(sum, budget, bet, new_budget);
您正在传递sum
作为参数,这是一个函数。当您的sum
函数返回int
时,您可以将其作为sum()
传递
double n = bla(sum(), budget, bet, new_budget);
旁注:在sum
内部,您已声明了名为sum
的变量。避免对函数和变量使用相同的名称。这会引起歧义。