Skip to content
Permalink
12f6c4aad5
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
90 lines (79 sloc) 2.2 KB
// Random number generators
// Rolling a dice 100 times
// Chris Bass
// November 2014
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <string>
using namespace std;
void diceGame()
{
int count1 = 0; // count of 1s rolled
int count2 = 0; // count of 2s rolled
int count3 = 0; // count of 3s rolled
int count4 = 0; // count of 4s rolled
int count5 = 0; // count of 5s rolled
int count6 = 0; // count of 6s rolled
srand(time(0)); // seed the pseudo random number generator
for (int roll = 1; roll <= 100; roll++) // 100 rolls
{
int diceRoll;
diceRoll = 1 + rand() % 6; // store a random number between 1 and 6
switch (diceRoll)
{
case 1:
++count1;
break;
case 2:
++count2;
break;
case 3:
++count3;
break;
case 4:
++count4;
break;
case 5:
++count5;
break;
case 6:
++count6;
break;
default:
break;
}
}
ifstream inputFileStream;
inputFileStream.open("output.txt");
int highscore;
if (inputFileStream.good()) {
inputFileStream >> highscore;
} else {
cout << "cannot find previous highscore textfile, lets create a new one!" << endl;
highscore = 0;
}
cout << "You have rolled " << count1 << " ones" << endl;
cout << "You have rolled " << count2 << " twos" << endl; // count of 1s rolled
cout << "You have rolled " << count3 << " threes" << endl; // count of 1s rolled
cout << "You have rolled " << count4 << " fours" << endl; // count of 1s rolled
cout << "You have rolled " << count5 << " fives" << endl; // count of 1s rolled
cout << "You have rolled " << count6 << " sixes" << endl; // count of 1s rolled
cout << "Your score this game is: " <<
count1 * 1 + count2 * 2 + count3 * 3 +
count4 * 4 + count5 * 5 + count6 * 6 << endl;
int currentScore = count1 * 1 + count2 * 2 + count3 * 3 +
count4 * 4 + count5 * 5 + count6 * 6;
cout << "Your Score is " <<currentScore<< endl;
{
cout << "The previous highscore is: " << highscore << endl;
ofstream outputFileStream;
if (currentScore > highscore) {
outputFileStream.open("output.txt");
outputFileStream << currentScore << endl;
outputFileStream.close();
cout << "The new highscore is: " << currentScore << endl;
}
}
}