Could I have a code review please. Newbie and my first program
Apr 25, 2010 at 8:14pm UTC
This program was just to learn, loops, if else's, rand and to see if I could get it all to work, (which it does). However I need the code review to tell me if there is a better way to build this program, switch instead of if else ect? Whether Rand is the best method here? (I'm sure I must have done something wrong somewhere).
The program rolls two dice, totals them and outputs how many times each number was rolled. (I was going to try to go for percentages but have no idea on using floats as yet!). Anyway here's my program source.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <conio.h> //switch
using namespace std;
int main(int argc, char *argv[])
{
int die_roll_1;
int die_roll_2;
int dice;
int counter = 0;
int runs = 0;
int number_1 = 0, number_2 = 0, number_3 = 0, number_4 = 0, number_5 = 0, number_6 = 0;
int number_7 = 0, number_8 = 0, number_9 = 0, number_10 = 0, number_11 = 0, number_12 = 0;
cout << "How many rolls of the dice do you want? " ;
cin >> runs;
// Re-seed the random-number generator
time_t now;
time(&now);
srand(now);
do {
die_roll_1 = rand() % 6 + 1;
// cout << die_roll_1 << " ";
die_roll_2 = rand() % 6 + 1;
// cout << die_roll_2 << " = ";
dice = die_roll_1 + die_roll_2;
// cout << dice << endl;
counter++; // adds one to counter
if (dice == 2)
// cout << "It's 2!" << endl;
number_2++;
else if (dice == 3 )
// cout << "It's 3" << endl;
number_3++;
else if (dice == 4 )
// cout << "It's 4" << endl;
number_4++;
else if (dice == 5 )
// cout << "It's 5" << endl;
number_5++;
else if (dice == 6 )
// cout << "It's 6" << endl;
number_6++;
else if (dice == 7 )
// cout << "It's 7" << endl;
number_7++;
else if (dice == 8 )
// cout << "It's 8" << endl;
number_8++;
else if (dice == 9 )
// cout << "It's 9" << endl;
number_9++;
else if (dice == 10 )
// cout << "It's 10" << endl;
number_10++;
else if (dice == 11 )
// cout << "It's 11" << endl;
number_11++;
else
// cout << "It's 12" << endl;
number_12++;
} while (counter != runs); // compares till they equal
cout << "Number 1 = " << number_1 << " times." << endl;
cout << "Number 2 = " << number_2 << " times." << runs / number_2 << endl;
cout << "Number 3 = " << number_3 << " times." << endl;
cout << "Number 4 = " << number_4 << " times." << endl;
cout << "Number 5 = " << number_5 << " times." << endl;
cout << "Number 6 = " << number_6 << " times." << endl;
cout << "Number 7 = " << number_7 << " times." << endl;
cout << "Number 8 = " << number_8 << " times." << endl;
cout << "Number 9 = " << number_9 << " times." << endl;
cout << "Number 10 = " << number_10 << " times." << endl;
cout << "Number 11 = " << number_11 << " times." << endl;
cout << "Number 12 = " << number_12 << " times." << endl;
system("PAUSE" );
return 0;
}
Thanks in advance, Leppie
Apr 25, 2010 at 9:00pm UTC
It look pretty good.
How about using an array for your 'number_x' counts? That way you don't need the big
if ..
else stuff.
1 2 3 4 5
int numbers[ 12 ] = { 0 }; // all elements are initialized to zero
...
number[ dice - 1 ]++; // same as number_2++ if dice==2
You can use a loop to print out how many times each number roked also.
Good job! Hope this helps.
Apr 25, 2010 at 9:13pm UTC
Thank you for your advice, I haven't looked at array's yet but I will get there. I'll make it my next step. Thanks again, Leppie
Topic archived. No new replies allowed.