Dice roll program

Hello fellow programmers, I just recently joined this website to help me gain some understanding with what I'm doing wrong with my programs. I decided to get back into C++ after about a year off and decided to look at one of my programs. I ran my dice roll program and it does not want to add correctly nor does it tell you the correct message of telling you that you either won, lost, or that you need to keep going. Thank you in advance for taking the time to read my code and giving advice.

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>

using namespace std;

// Function Prototypes
void greeting();
void greeting2();
int die1();
int die2();
int dice (int, int);
int results (int);

// Main Funtion
int main ()
{
    
   int counter;
   char secondstart;
   
   greeting ();
   int firstdie = die1();
   greeting2 ();
   int secondie = die2();
   int total = dice(firstdie, secondie);
   results (total);

   
   //cout << firstdie << endl;
   //cout << secondie << endl;
   cout << total << endl;
   
   
system ("PAUSE");
return 0;
    
    
}

//The following function outputs a basic greeting to the user
void greeting ()
{
     char starter;
     
     cout << "Please press X and then ENTER to roll the die." << endl;
     cin >> starter;
}

//This is the second greeting message to prompt the user to enter X again
void greeting2 ()
{
     char starter2;
     
     cout << "Please press X again to roll the second die." << endl;
     cin >> starter2;
}
     

//The following function simulates a die roll
int die1()
{
    for (int counter = 0; counter < 2; counter++)
    {
      srand(time(NULL));
      for (int i = 0; i <6; i++)
        {
          cout << rand()% 6+1 << endl;
          return rand()%6+1;
        }
    }      
}

//The following function simulates the second die roll
int die2()
{
    for (int counter = 0; counter < 2; counter++)
    {
      srand(time(NULL));
      for (int i = 0; i < 6; i++)
        {
          cout << rand()% 6+1 << endl;
          return rand()%6+1;
        }
    }      
}

//The following adds the 1st and 2nd roll together
int dice (int fdie, int sdie)
{
    
    int total = fdie + sdie;
    cout << "Your total so far is: " << total << endl;
    return total;
}

int results (int total)
{
    if (total = 7 && 11)
   {
     cout << "You won! Congratulations!" << endl;
   }
   else if (total = 2 && 3 && 12)
   {
        cout << "You lose. Try again." << endl;
   }
   else
   {
       cout << "You may continue to roll." << endl;
     greeting ();
     int firstdie = die1();
     greeting2 ();
     int secondie = die2();
     int total = dice(firstdie, secondie);
   }  
   
   //cout << firstdie << endl;
   //cout << secondie << endl;
   cout << total << endl;
}


/*
Please press X and then ENTER to roll the die.
x
4
Please press X again to roll the second die.
x
1
Your total so far is: 8
You won! Congratulations!
1
8
*/
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
#include <iostream>
#include <ctime>
#include <cstdlib>

//The following function outputs a basic greeting to the user
void greeting(int pnum) {
	if(pnum == 1) {
		std::cout << "Please press \"ENTER\" to roll the die.";
	} else {
		std::cout << "Please press \"ENTER\" again to roll the second die.";
	}
	std::cin.ignore();
}

//The following function simulates a die roll
int dieroll(void) {
	int ran;
	srand(time(NULL));
	ran = rand()%6+1;
	std::cout << "You rolled a " << ran << "." << std::endl;
	return ran;
}

//The following adds the 1st and 2nd roll together
int dice(int fdie, int sdie) {
    std::cout << "Your total so far is: " << sdie + fdie << std::endl;
    return sdie + fdie;
}

// Checks if player won / lost / can keep rolling.
int results(int total) {
	if (total == 7 || total == 11) {
		std::cout << "You won! Congratulations!";
		return 0;
	} else if (total == 2 || total == 3 || total == 12) {
                std::cout << "You lose. Try again.";
                return 0;
   	} else {
		std::cout << "You may continue to roll." << std::endl << std::endl;
		return 1;
	}
}

// Main Funtion
int main(void) {
	int counter, total, seconddie, firstdie;
	char secondstart;
	
	do {
		greeting(1);
   		firstdie = dieroll();
   		greeting(2);
	   	seconddie = dieroll();
	   	total = dice(firstdie, seconddie);
	} while(results(total)!=0);
	
	std::cin.ignore();
	return 0;
}


What I had to change / is not good:
1. It's not good to use using namespace std;.
2. It's not good to use system();.
3. To check is something is equal to something else, use ==.
4. You had some errors with your if statement which I fixed.
5. You have to use ||(or) instead of &&(and) in this case.
6. Just usestd::cin.ignore(); instead of creating a character and etc... (Save time and memory.)
7. You need a while loop to keep the game running if the person is still rolling.
8. Got rid of #include <iomanip> because there is no use for it.
9. You only need one dice roll function. Also you were printing another number on the screen than the one your were returning.
10. Saved some memory on your greeting functions.
11. Fixed tabs / spaces in your program.

What you did right:
1. Good comments. Keep it up!

In the end:
1. Made your program more user friendly.
2. Turned 135 lines of broken code into 59 lines of quality code.
3. Saved memory.
4. Saved time (program isn't slower than it was).
Last edited on
great fix -

With item 1 bad. Yes it is important you are aware of the danger but don't be afraid of using namespace std;

I agree with the comments from duoas here at this link

http://www.cplusplus.com/forum/beginner/14325/

Working at a level like this program should cause no conflicts and requires less keystrokes. BUT if you are going out into the world with your programming later - good habits are good programming.

Anything small and at the house - use it.
Wow, thanks for all the help guys! The whole thing about me using namespace std is more of a habit for me and that's why it's in there lol. I was really surprised to see usandfriend's results because it's so much freaking shorter than the mess I had. Again, thanks again for the help!
Topic archived. No new replies allowed.