so i was bored and decided to make a dice simulator, and i ran into a error.
heres the code

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int total[12];
int dice1;
int dice2;
int aitotal[12];
int aidice1;
int aidice2;
void Roll(){
    srand(time(0));
    dice1 = 1+ (rand() %6);
    dice2 = 1+ (rand() %6);

}

int DiceTotal(){
total[12] = dice1+dice2;

return total[12];}

void PrintDice(){
cout<<"Your dice numbers are: "<<dice1<<" and "<<dice2<<endl;
cout<<"Your dice total is: "<<total[12]<<endl<<endl;}

void AiRoll(){
    srand(time(0));
    aidice1 = 1+ (rand() %6);
    aidice2 = 1+ (rand() %6);
    cout<<"Ai's dices are "<<aidice1<<" and "<<aidice2<<endl<<endl;
}

int AiDiceTotal(){
aitotal[12] = aidice1+aidice2;
cout<<"Ai total is"<<aitotal[12]<<endl;
return aitotal[12];}
void Compare();


bool open= true;
int main()
{
    do{
Roll();
DiceTotal();
cout<<"To start game, press 1"<<endl;
int go;
cin>>go;
if(go==1){
PrintDice();
AiRoll();
AiDiceTotal();
Compare();
}
else{cout<<"Wrong in put idiot!"<<endl;
}
}while(open==true);
}

void Compare(){if(DiceTotal>AiDiceTotal){
cout<<"You win!!"<<endl;}
else if(AiDiceTotal>DiceTotal)
{cout<<"You lose!!"<<endl;
}
else if(DiceTotal==AiDiceTotal){
cout<<"TIEE!!"<<endl;}
else{
cout<<"Errror"<<endl;}
}

i wanted the output to be
dice number 6, 2
dice total 8
instead i get
dice numbers 8 2
dice total 8

ai is fine.
btw sorry for sloppy coding and no comments i made it really quickly.

Recommended Answers

All 5 Replies

Don't apologize for sloppy coding. It only takes a minute or two to fix formatting.

Why is it so many people think that seeding the random generator should be done immediately before calling rand()? Doesn't anyone read the info about how srand() works?

Why do you have all those arrays of 12 and load the 13th entry in the array (which doesn't exist)?

Reformat and think about how you are using the arrays.

In addition to what WaltP said, your compare function isn't working as you expect it to. Take a look at what you're comparing -- DiceTotal and AiDiceTotal -- and check whether this is what you intended.

Thanks, didnt payed attention to how i was comparing functions .-.
I dont get why the arrays were in the way though.
And no i really dont get how srand works, lol.

I dont get why the arrays were in the way though.

What are the legal values of x when using total[x] -- defined as int total[12];

And no i really dont get how srand works, lol.

ha ha, that's funny!!! I wish there was a way to find out. I can't think of a thing. Can you??? 8)

Do you understand the issues WaltP raised, now? If not, please ask.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.