I need another set of eyes please. I need to take a random number function (roll
ID: 3547289 • Letter: I
Question
I need another set of eyes please. I need to take a random number function (roll) that keeps a running total (total). If the number picked (i) is even, that number is added to the total (total). If the number (i) is odd, then it subtracts from the total (total). The program ends once the total (total) reaches 25.
So far, I'm stuck with the variable reference I think.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <cstdlib>
int rollDice();
using namespace std;
int main()
{
int total=0, roll;
srand(time(0));
cout << setw(10) << "Roll" << setw(10) << "Total" << endl;
cout << setw(10) << "----" << setw(10) << "-----" << endl;
while (total<25)
{
roll = rollDice();
cout << setw(10) << roll << setw(10) << total << endl;
}
return 0;
}
// =============== rollDice ===============
int rollDice(){
for (int i=1; i<10; i++){ // roll 0-10 times, incremended
cout << 1+(rand()%12) << endl; // 2 dice with 6 sides (1-12)
if(i%2==0){
int total,n;
total= i+n; // if even, add to total
}
else{
int total,n;
total= i-n; // if odd, subtract from total
}
}
return 0;
}
Explanation / Answer
Working Proof : http://ideone.com/SedC82
#include <iostream>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <cstdlib>
int rollDice();
using namespace std;
int total=0;
int main()
{
int roll;
srand(time(0));
cout << setw(10) << "Roll" << setw(10) << "Total" << endl;
cout << setw(10) << "----" << setw(10) << "-----" << endl;
while (total<25)
{
roll = rollDice();
cout << setw(10) << roll << setw(10) << total << endl;
}
return 0;
}
// =============== rollDice ===============
int rollDice()
{
int i;
i=1+(rand()%12);// 2 dice with 6 sides (1-12)
if(i%2==0){
total= total+i; // if even, add to total
}
else{
total= total-i; // if odd, subtract from total
}
return i;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.