【发布时间】:2016-05-02 05:07:18
【问题描述】:
#include<iostream>
#include<cstdlib>
#include<string>
#include<time.h>
using namespace std;
//Functions
// player strategy
int strategy(int user1Strat, int user2Strat);
// player total score per round
int currentScore();
// Display game result
void printResults();
int main()
{
int total_player1 = 0; // player 1 current score
int total_player2 = 0; // player 2 current score
int player1_strat= 0; //player 1 strategy for each turn
int player2_strat = 0; // player 2 strategy for each turn
// seed the random number generator.
srand(static_cast<int> (time(NULL)));
// get strategy for each player using functions <strategy>
strategy(player1_strat, player2_strat);
cout << player1_strat << endl << player2_strat << endl;
system("pause");
return 0;
}
int strategy(int user1Strat, int user2Strat)
{
int x,
y;
cout << "Enter player1's roll until strategy: ";
cin >> user1Strat;
cout << "Enter player2's roll until strategy: ";
cin >> user2Strat;
x = user1Strat;
y = user2Strat;
return x, y;
}
在函数main 中调用函数strategy 时,它会按应有的方式执行,但是一旦我要求返回值,它就会返回
Enter player1's roll until strategy: 10
Enter player2's roll until strategy: 5
0
0
press any key to contiue...
有谁知道为什么会发生这种情况或导致它的原因,是我在策略功能中的错误吗?还是在调用它时?
【问题讨论】:
-
这不是在 C++ 中从函数返回多个值的方式。查找“通过引用返回”。
-
您对从函数返回值的理解是有缺陷的。请阅读有关该主题的教科书。
-
另外,您也不会从函数中获取任何返回值。 X 和 y 不会神奇地出现在您想要的一些随机变量上。基础教程/书籍将在这里为您提供帮助。
-
strategy函数应该返回一个std::pair<int,int>,而不是单个int。