21 dice game

ok so im doing a dice game where you try to reach 21 and if you go over 21 then your score gets reduced to 13. it is not working right. it keeps the first random # all of the way through the program and it doesnt loop at all. can you help me?

my code is


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
#include <iostream>
#include <cstdlib>
#include <ctime>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::numeric_limits;
using std::streamsize;

int main()
{
    do
    {
    cout << "p1 hit enter to roll\n\n\n\n";
    cin.get(); 
    string p1, p2;
    int p1s, p2s;
    srand(static_cast<unsigned int>(time(0)));
    int randomNumber = rand();
    int die = (randomNumber % 6) + 1;
    cout << "p1 you rolled a " << die << "\n";
    cout << "p1 score = " << die << "\n\n\n";
    p1s = p1s + die;
    cout << "p2 hit enter to roll\n\n\n\n";
    cin.get(); 
    (randomNumber % 6) + 1;
    cout << "p2 you rolled a " << die << "\n";
    cout << "p2 score = " << die << "\n\n\n";
    p2s = p2s + die;
    cin.get();
    }
    while (p1s < 21 || p2s < 21);
    if (p1s > 21 || p2s < 21)
       {
            p1s = 13 || p2s = 13;
       }
    
Last edited on
There is a lot wrong with your code. It would take too much to explain it all at once, so I'll only say a few things for now.

1. The random number generator only needs to be seeded once. Right now it will be seeded every time you loop.
2. Both p1 and p2 will end up rolling the same number because randomNumber doesn't change inbetween.
3. The condition in your do-while loop will not work as you want it to, nor will your if statement. In the do-while loop, it will still loop if only one of the players is over 21.
4. line 27 doesn't do anything.
i re-wrote the code to do what u wanted
hope this helps

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
#include <iostream>
#include <cstdlib> 
#include <ctime>                               //dont  need it
using namespace std;

int main()
{   int repeat, p1s=0, p2s=0;
    srand((unsigned) time(0));  //but we dont need to seed rand  to run the code 
	 cout<<"score 21 to win \nif your score reaches more than 21 it will be reset to 13\n\n\n";   // telling the rules
	 do
	 {
	 cout << "p1 hit enter to roll\n\n\n\n";
	 cin.get();
	 //string p1, p2;                                        un used

	 int randomNumber = rand();
	 int die = (randomNumber % 6) + 1;
	 cout << "p1 you rolled a " << die << "\n";
	 p1s += die;
	 cout << "p1 score = " << p1s << "\n\n\n";    //here p1s makes betr sense compaired to die
	 cout << "p2 hit enter to roll\n\n\n\n";
	 cin.get();
	 randomNumber = rand();                       //calling rand again for p2
	 die = (randomNumber % 6) + 1;
	 cout << "p2 you rolled a " << die << "\n";
	 p2s = p2s + die;
	 cout << "p2 score = " << p2s << "\n\n\n";
	 cin.get();
	 if (p1s == 21 && p2s == 21)                  //setting repeat condition
	  repeat=0;
	 if(p1s>21)                                   //reseting score
	  p1s=13;
	 if(p2s>21)
	  p2s=13;
	 }while (repeat!=0);
	 if(p1s>p2s)
		cout<<"p1 wins "<<p1s<<":"<<p2s;
	 else if(p2s>p1s)
		cout<<"p2 wins "<<p2s<<":"<<p1s;
		cout<<"its a tie!!";
	 return 0;
 }
@soumyaxyz
that code dont work. it doesnt show a winner.
my code still isnt looping and i dont know y.

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
#include <iostream>
#include <cstdlib>
#include <ctime>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::numeric_limits;
using std::streamsize;

int main()
{    
     srand(static_cast<unsigned int>(time(0)));
    int p1s, p2s, repeat;
    do
    {
    cout << "p1 hit enter to roll\n\n\n\n";
    cin.get(); 
    int randomNumber = rand();
    int die = (randomNumber % 6) + 1;
    cout << "p1 you rolled a " << die << "\n";
    cout << "p1 score = " << die << "\n\n\n";
    p1s = p1s + die;
    cout << "p2 hit enter to roll\n\n\n\n";
    cin.get();
    randomNumber = rand(); 
    die = (randomNumber % 6) + 1;
    cout << "p2 you rolled a " << die << "\n";
    cout << "p2 score = " << die << "\n\n\n";
    p2s = p2s + die;
    cin.get();
    }
    while (repeat = 0);
}
cainen172 wrote:
my code still isnt looping and i dont know y.
because of line 33 which is an assignment but you want to compare it: while (repeat == 0); // note the ==

if you wrote while (0 = repeat); the compiler would have complained. So it's safer to place the constant left.

EDIT: I see that you don't change repeat at all. so it wont work at all. How often do you want to repeat?
Last edited on
i want it to repeat until someone reaches 21. i have edited it again... but it doesnt add the new die to the past die.

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
 #include <iostream>
#include <cstdlib>
#include <ctime>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::numeric_limits;
using std::streamsize;

int main()
{    
     srand(static_cast<unsigned int>(time(0)));
    int p1s, p2s, repeat;
    do
    {
    repeat = 0;
    cout << "p1 hit enter to roll\n\n\n\n";
    cin.get(); 
    int randomNumber = rand();
    int die = (randomNumber % 6) + 1;
    cout << "p1 you rolled a " << die << "\n";
    cout << "p1 score = " << die << "\n\n\n";
    p1s += die;
    cout << "p2 hit enter to roll\n\n\n\n";
    cin.get();
    randomNumber = rand(); 
    die = (randomNumber % 6) + 1;
    cout << "p2 you rolled a " << die << "\n";
    cout << "p2 score = " << die << "\n\n\n";
    p2s += die;
    cin.get();
    }
    while (repeat < 2);
        if (p1s = 21)
    {
    cout << "p1 wins" ;
    system ("PAUSE");
    return 0;
    }
    else if (p2s = 21)
    {
    cout << "p2 wins" ;
    system ("PAUSE");
    return 0;
    }
    if (p2s > 21)
    {
            cout << "p2 scored above 21. score is reduced to 13.";
            p2s = 13;
    }
    else if (p1s > 21)
    {
            cout << "p1 scored above 21. score is reduced to 13.";
            p1s = 13;
    }
}
Last edited on
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
#include <iostream>
#include <cstdlib> 
#include <ctime>                               //dont  need it
using namespace std;
int main()
{   int repeat, p1s=0, p2s=0;
	 srand((unsigned) time(0));
	 srand;
	 cout<<"score 21 to win \nif your score reaches more than 21 it will be reset to 13\n\n\n";   // telling the rules
	 do
	 {
	 cout << "p1 hit enter to roll\n\n\n\n";
	 cin.get();
	 //string p1, p2;

	 int randomNumber = rand();
	 int die ;//= (randomNumber % 6) + 1;
	 cin>>die;
         if(p1s>21)                                   //reseting score
	  p1s=13;
	 cout << "p1 you rolled a " << die << "\n";
	 p1s += die;
	 cout << "p1 score = " << p1s << "\n\n\n";    //here p1s makes betr sense compaired to die
	 cout << "p2 hit enter to roll\n\n\n\n";
	 cin.get();
	 randomNumber = rand();                       //calling rand again for p2
	 //die = (randomNumber % 6) + 1;
	 cin>>die;
	 if(p2s>21)
	  p2s=13;
	 cout << "p2 you rolled a " << die << "\n";
	 p2s = p2s + die;
	 cout << "p2 score = " << p2s << "\n\n\n";
	 cin.get();
	 if (p1s == 21 || p2s == 21)                  //setting repeat condition it shod be a or
	  repeat=0;
	 }while (repeat!=0);
	 if(p1s>p2s)
		cout<<"p1 wins "<<p1s<<":"<<p2s;
	 else if(p2s>p1s)
		cout<<"p2 wins "<<p2s<<":"<<p1s;
	 else
		cout<<"its a tie!!";
	 return 0;
 }



i have corrected the error and tested it :)
since we have no control ovr the rand() i have comented it out an used cin>> to test

also i rearanged the reseting of scores to make the outputs program more realistic

also u can replace
1
2
3
4
 
if (p1s == 21 || p2s == 21)                  //setting repeat condition 
	  repeat=0;
	 }while (repeat!=0);

with
 
 }while ((p1s != 21 && p2s != 21) );                 //checking repeat condition  here it will be and 


in the 2nd version we dont need the int repeat variable


i didnt have time to go through latest code
i'll go through it aftr coming back from college
but in the meantime time try nor to use system("...") calls every one hates hate those
http://www.cplusplus.com/forum/beginner/1988/
http://www.gidnetwork.com/b-61.html
Last edited on
OH MY GOSH!!!!!!!!!!! even that code doesnt work. here i know. first i need to know why my p1s isnt adding in MY code.






the thing is that whenever i need help with a code. people usualy just rewrite the code for me. i kinda wanted to do this without copying what someone just rewrote.
@cainen172: I think sometimes you just have to accept that the way you are trying to solve you're problem is incorrect; and learn from other peoples methods.

I've added some comments to your code that should set you on the right path:
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
#include <iostream>
#include <cstdlib>
#include <ctime>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::numeric_limits;
using std::streamsize;

int main()
{    
    srand(static_cast<unsigned int>(time(0)));
    int p1s, p2s, repeat; // initialize these variables

  do
  {
    repeat = 0;
    cout << "p1 hit enter to roll\n\n\n\n";
    cin.get(); 
  
    int randomNumber = rand();
    int die = (randomNumber % 6) + 1;

    cout << "p1 you rolled a " << die << "\n";
    cout << "p1 score = " << die << "\n\n\n";
    p1s += die;
    cout << "p2 hit enter to roll\n\n\n\n";
    cin.get();

    randomNumber = rand(); 
    die = (randomNumber % 6) + 1;
    cout << "p2 you rolled a " << die << "\n"; // use endl to flush not \n
    cout << "p2 score = " << die << "\n\n\n";
    p2s += die;
    
    cin.get();
    // repeat is always going to be 0, this is infinite loop
  } while (repeat < 2);

  // What if they are both 21?
  if (p1s = 21) // == not =
  {
    cout << "p1 wins" ;
    system ("PAUSE");
    return 0;
  
  }
  else if (p2s = 21) // == not =
  {
    cout << "p2 wins" ;
    system ("PAUSE");
    return 0;
  }

  if (p2s > 21)
  {
    cout << "p2 scored above 21. score is reduced to 13."; // add endl
    p2s = 13;
  }
  else if (p1s > 21)
  {
     cout << "p1 scored above 21. score is reduced to 13."; // add endl
     p1s = 13;
  }

}
To follow on: This is how I would do your basic structure
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int ps1 = 0, ps2 = 0;

while (ps1 != 21 && ps2 != 21) {
  // Get the user scores and add them.

  if (ps1 > 21) {
   ps1 = 13;
  } else if (ps2 > 21) {
   ps2 = 13;
  }

}

if (ps1 == 21 && ps2 == ps1) {
 // Both players win
} else if (ps1 == 21) {
 // Player 1 wins
} else if (ps2 == 21) {
 // Player 2 wins
}
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
#include <iostream>
#include <cstdlib>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::numeric_limits;
using std::streamsize;

int main()
{    
     srand(static_cast<unsigned int>(time(0)));
    int p1s, p2s, repeat;    
    repeat = 0;
    p1s = 0;
    p2s = 0;
    do
    {

    cout << "p1 hit enter to roll\n\n\n\n";
    cin.get(); 
    int randomNumber = rand();
    int die = (randomNumber % 6) + 1;
    cout << "p1 you rolled a " << die << "" << endl;
    cout << "p1 score = " << die << "\n\n\n";
    p1s += die;
    cout << "p2 hit enter to roll\n\n\n\n";
    cin.get();
    randomNumber = rand(); 
    die = (randomNumber % 6) + 1;
    cout << "p2 you rolled a " << die << "" << endl;
    cout << "p2 score = " << die << "\n\n\n";
    p2s += die;
    cin.get();
    }
    while (repeat < 1);
       int ps1 = 0, ps2 = 0;

while (ps1 != 21 && ps2 != 21) {


  if (ps1 > 21) {
   ps1 = 13;
  } else if (ps2 > 21) {
   ps2 = 13;
  }

}

if (ps1 == 21 && ps2 == ps1) {
 cout << "its a tie!!!";
} else if (ps1 == 21) {
 cout << "p1 wins!!!";
} else if (ps2 == 21) {
 cout << "p2 wins!!!";
}
}


i am trying the codes but it still dont work.
it says:

p1 rolled a 5

p1 score = 5


p2 rolled a 2

p1 score = 2


p1 rolled a 4

p1 score = 4
Remove lines 37 to 48 for now.
line 25 and 32 are outputting the value of die, not ps1 or ps2.
Topic archived. No new replies allowed.