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?
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.
#include <iostream>
#include <cstdlib>
#include <ctime> //dont need it
usingnamespace 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;
elseif(p2s>p1s)
cout<<"p2 wins "<<p2s<<":"<<p1s;
cout<<"its a tie!!";
return 0;
}
#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<unsignedint>(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);
}
#include <iostream>
#include <cstdlib>
#include <ctime> //dont need it
usingnamespace 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;
elseif(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
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:
#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<unsignedint>(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;
}
elseif (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;
}
elseif (p1s > 21)
{
cout << "p1 scored above 21. score is reduced to 13."; // add endl
p1s = 13;
}
}