我创建了2个函数,每个函数都会掷骰子,然后我在main函数中输出每个骰子的结果。如何将每个骰子的结果一起添加? (我对主要功能中的一行做了评论)
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int diceroll1();
int diceroll2();
int main() {
cout << "you rolled a.\n";
cout << diceroll1(); cout << endl;
cout << "and a\n";
cout << diceroll2(); cout << endl;
cout << "for a total of:\n";
cout << diceroll1 + diceroll2 // idk how to sum these two values, and output them, as they are going to be different values each time.
system("PAUSE");
return 0;
}
int diceroll1() {
int roll1;
srand(time(0));
const int numberOfRolls = 1;
for (int i = 0; i < numberOfRolls; i++) {
roll1 = 1 + (rand() % 6);
return roll1;
}
}
int diceroll2() {
int roll2;
srand(1+time(0));
const int numberOfRolls = 1;
for (int i = 0; i < numberOfRolls; i++) {
roll2 = 1 + (rand() % 6);
return roll2;
}
}
答案 0 :(得分:0)
你可以这样做:
diceroll1() + diceroll2()
因为编译器将其视为值diceroll1()返回+ diceroll2()的值;